1: <?php
2:
3: namespace PHPixie\AuthHTTP\Providers;
4:
5: class Session extends \PHPixie\Auth\Providers\Provider\Implementation
6: implements \PHPixie\Auth\Providers\Provider\Persistent
7: {
8: protected $httpContextContainer;
9: protected $sessionKey;
10:
11: public function __construct($httpContextContainer, $domain, $name, $configData)
12: {
13: $this->httpContextContainer = $httpContextContainer;
14:
15: parent::__construct($domain, $name, $configData);
16: }
17:
18: public function check()
19: {
20: $session = $this->session();
21: $userId = $session->get($this->sessionKey());
22: if($userId === null) {
23: return null;
24: }
25:
26: $user = $this->repository()->getById($userId);
27: if($user === null) {
28: $this->forget();
29: return null;
30: }
31:
32: $this->domain->setUser($user, $this->name);
33: return $user;
34: }
35:
36: public function persist()
37: {
38: $user = $this->domain->requireUser();
39: $this->session()->set($this->sessionKey(), $user->id());
40: }
41:
42: public function forget()
43: {
44: $this->session()->remove($this->sessionKey());
45: }
46:
47: protected function session()
48: {
49: $httpContext = $this->httpContextContainer->httpContext();
50: return $httpContext->session();
51: }
52:
53: protected function sessionKey()
54: {
55: if($this->sessionKey === null) {
56: $defaultKey = $this->domain->name().'UserId';
57: $this->sessionKey = $this->configData->get('key', $defaultKey);
58: }
59:
60: return $this->sessionKey;
61: }
62: }