Test Setup Failed
Push — master ( 4e7eeb...a41b36 )
by Roel van
17:34
created

WriteTokenToCookie::attachShared()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 12
cp 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
crap 2
1
<?php
2
3
namespace JwPersistentUser\Listener;
4
5
use Interop\Container\ContainerInterface;
6
use Interop\Container\Exception\ContainerException;
7
use JwPersistentUser\Service\CookieService;
8
use JwPersistentUser\Service\RememberMeService;
9
10
use Zend\EventManager\Event;
11
use Zend\Http\Request;
12
use Zend\Http\Response;
13
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
14
use Zend\ServiceManager\Exception\ServiceNotFoundException;
15
use Zend\ServiceManager\Factory\DelegatorFactoryInterface;
16
use Zend\ServiceManager\ServiceLocatorInterface;
17
use Zend\Stdlib\RequestInterface;
18
use Zend\Stdlib\ResponseInterface;
19
use Zend\EventManager\SharedEventManagerInterface;
20
21
use ZfcUser\Authentication\Adapter\AdapterChain;
22
use ZfcUser\Authentication\Adapter\AdapterChainEvent;
23
24
class WriteTokenToCookie implements DelegatorFactoryInterface
25
{
26
    /**
27
     * @var []
28
     */
29
    protected $sharedListeners = [];
30
31
    /**
32
     * @var ContainerInterface
33
     */
34
    protected $serviceLocator;
35
36
    /**
37
     * @var RequestInterface
38
     */
39
    protected $request;
40
41
    /**
42
     * @var ResponseInterface
43
     */
44
    protected $response;
45
46
    /**
47
     * @var RememberMeService
48
     */
49
    protected $rememberMeService;
50
51
    /**
52
     * @var CookieService
53
     */
54
    protected $cookieService;
55
56
    public function __invoke(ContainerInterface $container, $name, callable $callback, array $options = null)
57
    {
58
        $this->serviceLocator = $container;
59
60
        /** @var AdapterChain $original */
61
        $original = call_user_func($callback);
62
63
        $original->getEventManager()->attach(
64
            'authenticate.success',
65 3
            [$this, 'authenticate']
66
        );
67 3
68 2
        $original->getEventManager()->attach(
69
            'logout',
70
            [$this, 'logout']
71 1
        );
72
73 1
        return $original;
74 1
    }
75
76
    public function authenticate(Event $e)
77
    {
78
        $e = $e->getTarget();
79 3
80
        if (!$this->isValidRequestAndResponse()) {
81 3
            return;
82 2
        }
83
84
        $serieToken = $this->getRememberMeService()->createNew($e->getIdentity());
85 1
86 1
        $this->getCookieService()->writeSerie($this->getResponse(), $serieToken);
0 ignored issues
show
Compatibility introduced by
$this->getResponse() of type object<Zend\Stdlib\ResponseInterface> is not a sub-type of object<Zend\Http\Response>. It seems like you assume a concrete implementation of the interface Zend\Stdlib\ResponseInterface to be always present.

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.

Loading history...
87 1
    }
88 1
89
    public function logout(Event $e)
90 1
    {
91 1
        $e = $e->getTarget();
0 ignored issues
show
Unused Code introduced by
$e is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
92
93
        if (!$this->isValidRequestAndResponse()) {
94
            return;
95
        }
96 6
97
        $serieToken = $this->getCookieService()->read($this->getRequest(), $this->getResponse());
0 ignored issues
show
Compatibility introduced by
$this->getRequest() of type object<Zend\Stdlib\RequestInterface> is not a sub-type of object<Zend\Http\Request>. It seems like you assume a concrete implementation of the interface Zend\Stdlib\RequestInterface to be always present.

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.

Loading history...
Documentation introduced by
$this->getResponse() is of type object<Zend\Stdlib\ResponseInterface>, but the function expects a null|object<Zend\Http\Response>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98 6
        if ($serieToken) {
99 6
            $this->getRememberMeService()->removeSerie($serieToken->getUserId(), $serieToken->getSerie());
100
        }
101
102
        $this->getCookieService()->writeNull($this->getResponse());
0 ignored issues
show
Compatibility introduced by
$this->getResponse() of type object<Zend\Stdlib\ResponseInterface> is not a sub-type of object<Zend\Http\Response>. It seems like you assume a concrete implementation of the interface Zend\Stdlib\ResponseInterface to be always present.

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.

Loading history...
103
    }
104
105
    /**
106
     * @return bool
107
     */
108
    protected function isValidRequestAndResponse()
109
    {
110
        return $this->getRequest() instanceof Request
111
            && $this->getResponse() instanceof Response;
112
    }
113
114 2
    public function detachShared(SharedEventManagerInterface $events)
115
    {
116 2
        foreach ($this->sharedListeners as $index => $listener) {
117
            if ($events->detach($this->eventIdentifier, $listener)) {
0 ignored issues
show
Bug introduced by
The property eventIdentifier does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
118
                unset($this->sharedListeners[$index]);
119 2
            }
120
        }
121
    }
122
123
    /**
124
     * @return RememberMeService
125
     */
126 6
    public function getRememberMeService()
127
    {
128 6
        if ($this->rememberMeService === null) {
129 6
            $this->rememberMeService = $this->serviceLocator->get('JwPersistentUser\Service\RememberMe');
130
        }
131
        return $this->rememberMeService;
132
    }
133
134
    /**
135 4
     * @param RememberMeService $rememberMeService
136
     * @return $this
137 4
     */
138
    public function setRememberMeService($rememberMeService)
139
    {
140
        $this->rememberMeService = $rememberMeService;
141
        return $this;
142
    }
143
144 6
    /**
145
     * @return ResponseInterface
146 6
     */
147 6
    public function getResponse()
148
    {
149
        if (!$this->response) {
150
            $this->response = $this->serviceLocator->get('Response');
151
        }
152
153 6
        return $this->response;
154
    }
155 6
156
    /**
157
     * @param ResponseInterface $response
158
     * @return $this
159
     */
160
    public function setResponse($response)
161
    {
162 6
        $this->response = $response;
163
        return $this;
164 6
    }
165 6
166
    /**
167
     * @return RequestInterface
168
     */
169
    public function getRequest()
170
    {
171 2
        if (!$this->request) {
172
            $this->request = $this->serviceLocator->get('Request');
173 2
        }
174
175
        return $this->request;
176 2
    }
177
178
    /**
179
     * @param RequestInterface $request
180
     * @return $this
181
     */
182
    public function setRequest($request)
183 6
    {
184
        $this->request = $request;
185 6
        return $this;
186 6
    }
187
188
    /**
189
     * @return CookieService
190
     */
191
    public function getCookieService()
192
    {
193
        if ($this->cookieService === null) {
194
            $this->cookieService = $this->serviceLocator->get('JwPersistentUser\Service\Cookie');
195
        }
196
        return $this->cookieService;
197
    }
198
199
    /**
200
     * @param CookieService $cookieService
201
     * @return $this
202
     */
203
    public function setCookieService($cookieService)
204
    {
205
        $this->cookieService = $cookieService;
206
        return $this;
207
    }
208
}
209