1 | <?php |
||
15 | class WriteTokenToCookie |
||
16 | { |
||
17 | protected $sharedListeners = []; |
||
18 | |||
19 | /** |
||
20 | * @var ContainerInterface |
||
21 | */ |
||
22 | protected $serviceLocator; |
||
23 | |||
24 | 6 | public function __construct(ContainerInterface $serviceLocator) |
|
25 | { |
||
26 | 6 | $this->serviceLocator = $serviceLocator; |
|
27 | 6 | } |
|
28 | |||
29 | public function attachShared(SharedEventManagerInterface $events) |
||
30 | { |
||
31 | $events->attach( |
||
32 | 'ZfcUser\Authentication\Adapter\AdapterChain', |
||
33 | 'authenticate.success', |
||
34 | [$this, 'authenticate'] |
||
35 | ); |
||
36 | |||
37 | $events->attach( |
||
38 | 'ZfcUser\Authentication\Adapter\AdapterChain', |
||
39 | 'logout', |
||
40 | [$this, 'logout'] |
||
41 | ); |
||
42 | } |
||
43 | |||
44 | public function detachShared(SharedEventManagerInterface $events) |
||
45 | { |
||
46 | foreach ($this->sharedListeners as $key => $handle) { |
||
47 | if ($events->detach($handle[0], $handle[1])) { |
||
48 | unset($this->sharedListeners[$key]); |
||
49 | } |
||
50 | } |
||
51 | } |
||
52 | |||
53 | 3 | public function authenticate(AdapterChainEvent $e) |
|
54 | { |
||
55 | 3 | if (!$this->isValidRequestAndResponse()) { |
|
56 | 2 | return; |
|
57 | } |
||
58 | |||
59 | 1 | $serieToken = $this->getRememberMeService()->createNew($e->getIdentity()); |
|
60 | |||
61 | 1 | $this->getCookieService()->writeSerie($this->getResponse(), $serieToken); |
|
|
|||
62 | 1 | } |
|
63 | |||
64 | 3 | public function logout(AdapterChainEvent $e) |
|
65 | { |
||
66 | 3 | if (!$this->isValidRequestAndResponse()) { |
|
67 | 2 | return; |
|
68 | } |
||
69 | |||
70 | 1 | $serieToken = $this->getCookieService()->read($this->getRequest(), $this->getResponse()); |
|
71 | 1 | if ($serieToken) { |
|
72 | 1 | $this->getRememberMeService()->removeSerie($serieToken->getUserId(), $serieToken->getSerie()); |
|
73 | } |
||
74 | |||
75 | 1 | $this->getCookieService()->writeNull($this->getResponse()); |
|
76 | 1 | } |
|
77 | |||
78 | /** |
||
79 | * @return bool |
||
80 | */ |
||
81 | 6 | protected function isValidRequestAndResponse() |
|
86 | |||
87 | /** |
||
88 | * @return RememberMeService |
||
89 | */ |
||
90 | 2 | public function getRememberMeService() |
|
94 | |||
95 | /** |
||
96 | * @return ResponseInterface |
||
97 | */ |
||
98 | 4 | public function getResponse() |
|
102 | |||
103 | /** |
||
104 | * @return RequestInterface |
||
105 | */ |
||
106 | 6 | public function getRequest() |
|
107 | { |
||
110 | |||
111 | /** |
||
112 | * @return CookieService |
||
113 | */ |
||
114 | 2 | public function getCookieService() |
|
118 | } |
||
119 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.