WriteTokenToCookie   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 65.12%

Importance

Changes 0
Metric Value
wmc 16
lcom 2
cbo 5
dl 0
loc 104
ccs 28
cts 43
cp 0.6512
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidRequestAndResponse() 0 5 2
A getRememberMeService() 0 4 1
A getResponse() 0 4 1
A __construct() 0 4 1
A attachShared() 0 14 1
A detachShared() 0 8 3
A authenticate() 0 10 2
A logout() 0 13 3
A getRequest() 0 4 1
A getCookieService() 0 4 1
1
<?php
2
3
namespace JwPersistentUser\Listener;
4
5
use Interop\Container\ContainerInterface;
6
use JwPersistentUser\Service\CookieService;
7
use JwPersistentUser\Service\RememberMeService;
8
use Zend\EventManager\SharedEventManagerInterface;
9
use Zend\Http\Request;
10
use Zend\Http\Response;
11
use Zend\Stdlib\RequestInterface;
12
use Zend\Stdlib\ResponseInterface;
13
use ZfcUser\Authentication\Adapter\AdapterChainEvent;
14
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);
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...
62 1
    }
63
64 3
    public function logout(AdapterChainEvent $e)
0 ignored issues
show
Unused Code introduced by
The parameter $e is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
    {
66 3
        if (!$this->isValidRequestAndResponse()) {
67 2
            return;
68
        }
69
70 1
        $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...
71 1
        if ($serieToken) {
72 1
            $this->getRememberMeService()->removeSerie($serieToken->getUserId(), $serieToken->getSerie());
73
        }
74
75 1
        $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...
76 1
    }
77
78
    /**
79
     * @return bool
80
     */
81 6
    protected function isValidRequestAndResponse()
82
    {
83 6
        return $this->getRequest() instanceof Request
84 6
            && $this->getResponse() instanceof Response;
85
    }
86
87
    /**
88
     * @return RememberMeService
89
     */
90 2
    public function getRememberMeService()
91
    {
92 2
        return $this->serviceLocator->get('JwPersistentUser\Service\RememberMe');
93
    }
94
95
    /**
96
     * @return ResponseInterface
97
     */
98 4
    public function getResponse()
99
    {
100 4
        return $this->serviceLocator->get('Response');
101
    }
102
103
    /**
104
     * @return RequestInterface
105
     */
106 6
    public function getRequest()
107
    {
108 6
        return $this->serviceLocator->get('Request');
109
    }
110
111
    /**
112
     * @return CookieService
113
     */
114 2
    public function getCookieService()
115
    {
116 2
        return $this->serviceLocator->get('JwPersistentUser\Service\Cookie');
117
    }
118
}
119