1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sinergi\Users\Session; |
4
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
6
|
|
|
use Psr\Http\Message\RequestInterface; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
use Sinergi\Users\Container; |
10
|
|
|
use Sinergi\Users\User\UserEntityInterface; |
11
|
|
|
|
12
|
|
|
class SessionController |
13
|
|
|
{ |
14
|
|
|
private $container; |
15
|
|
|
private $session; |
16
|
|
|
|
17
|
|
View Code Duplication |
public function __construct(ContainerInterface $container) |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
if ($container instanceof Container) { |
20
|
|
|
$this->container = $container; |
21
|
|
|
} else { |
22
|
|
|
$this->container = new Container($container); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return SessionEntity|null |
28
|
|
|
*/ |
29
|
|
|
public function getSession() |
30
|
|
|
{ |
31
|
|
|
if (null !== $this->session) { |
32
|
|
|
return $this->session; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$headers = $this->getRequest()->headers(); |
|
|
|
|
36
|
|
|
|
37
|
|
|
if (isset($headers['x-session']) && $sessionId = $headers['x-session']) { |
38
|
|
|
$session = $this->getSessionRepository()->find($sessionId); |
39
|
|
|
if ($session instanceof SessionEntity && $session->isValid()) { |
|
|
|
|
40
|
|
|
return $this->session = $session; |
41
|
|
|
} |
42
|
|
|
} else if ($sessionId = $this->getRequest()->cookies() |
|
|
|
|
43
|
|
|
->get(SessionEntity::COOKIE_NAME) |
44
|
|
|
) { |
45
|
|
|
$session = $this->getSessionRepository()->find($sessionId); |
46
|
|
|
if ($session instanceof SessionEntity && $session->isValid()) { |
|
|
|
|
47
|
|
|
return $this->session = $session; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param SessionEntity|null $session |
56
|
|
|
*/ |
57
|
|
|
public function setSession(SessionEntity $session = null) |
58
|
|
|
{ |
59
|
|
|
$this->session = $session; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function createSession(UserEntityInterface $user, $isLongSession = false): SessionEntityInterface |
63
|
|
|
{ |
64
|
|
|
$session = $this->container->get(SessionEntityInterface::class); |
65
|
|
|
$session->setUser($user); |
66
|
|
|
$session->setIsLongSession($isLongSession); |
67
|
|
|
|
68
|
|
|
/** @var SessionRepositoryInterface $sessionRepository */ |
69
|
|
|
$sessionRepository = $this->container->get(SessionRepositoryInterface::class); |
70
|
|
|
$sessionRepository->save($session); |
71
|
|
|
|
72
|
|
|
return $this->session = $session; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function deleteSession() |
79
|
|
|
{ |
80
|
|
|
$session = $this->getSession(); |
81
|
|
|
if ($session instanceof SessionEntity) { |
|
|
|
|
82
|
|
|
$this->getDoctrine()->getEntityManager()->remove($session); |
|
|
|
|
83
|
|
|
$this->getDoctrine()->getEntityManager()->flush($session); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
$this->deleteSessionCookie(); |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param SessionEntity $session |
92
|
|
|
*/ |
93
|
|
|
public function createSessionCookie(SessionEntity $session = null) |
94
|
|
|
{ |
95
|
|
|
if ($session === null) { |
96
|
|
|
$session = $this->session; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($session !== null) { |
100
|
|
|
$config = $this->getConfig(); |
|
|
|
|
101
|
|
|
$cookie = new ResponseCookie( |
102
|
|
|
SessionEntity::COOKIE_NAME, |
103
|
|
|
$session->getId(), |
104
|
|
|
$session->getExpirationDatetime(), |
105
|
|
|
$config->get('cookie.path'), |
106
|
|
|
$config->get('cookie.domain'), |
107
|
|
|
$config->get('cookie.secure'), |
108
|
|
|
$config->get('cookie.httponly') |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$this->getResponse()->cookies()->set( |
|
|
|
|
112
|
|
|
$cookie->getName(), |
113
|
|
|
$cookie |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
|
|
public function deleteSessionCookie() |
122
|
|
|
{ |
123
|
|
|
$this->getResponse()->cookies()->set(SessionEntity::COOKIE_NAME, null); |
|
|
|
|
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
|
|
public function extendSession() |
132
|
|
|
{ |
133
|
|
|
$session = $this->getSession(); |
134
|
|
|
if ($session !== null) { |
135
|
|
|
|
136
|
|
|
$session->createExpirationDatetime(); |
137
|
|
|
$this->getEntityManager()->flush($this->session); |
|
|
|
|
138
|
|
|
|
139
|
|
|
$this->createSessionCookie($this->session); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return SessionRepository |
147
|
|
|
*/ |
148
|
|
|
private function getSessionRepository() |
149
|
|
|
{ |
150
|
|
|
return $this->getEntityManager() |
|
|
|
|
151
|
|
|
->getRepository(SessionEntity::class); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.