Implement download entity and related stuff. Will build things like user quota on...
[rock.divinelegy.git] / Services / UserSession.php
1 <?php
2
3 namespace Services;
4
5 use Services\Http\IHttpRequest;
6 use DataAccess\IUserRepository;
7
8 class UserSession implements IUserSession
9 {
10 private $_userRepository;
11 private $_request;
12 private $_currentUser;
13
14 public function __construct(IHttpRequest $request, IUserRepository $repository)
15 {
16 $this->_request = $request;
17 $this->_userRepository = $repository;
18
19 $token = $this->findToken();
20 $this->_currentUser = $token ? $this->_userRepository->findByAuthToken($token) : null;
21 }
22
23 public function getCurrentUser()
24 {
25 return $this->_currentUser;
26 }
27
28 public function getCurrentUserQuota() {
29 ;
30 }
31
32 private function findToken()
33 {
34 if($this->_request->isPost())
35 {
36 $request = $this->_request->post();
37 if(!empty($request['token'])) return $request['token'];
38 }
39
40 if($this->_request->isGet())
41 {
42 $request = $this->_request->get();
43 if(!empty($request['token'])) return $request['token'];
44 }
45
46 //no good, try the body
47 $body = json_decode($this->_request->getBody(), true);
48 if(!empty($body['token'])) return $body['token'];
49
50 return null;
51 }
52 }