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

CookieAuthenticationService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 1
cts 2
cp 0.5
cc 1
eloc 2
nc 1
nop 1
crap 1.125
1
<?php
2
3
namespace JwPersistentUser\Service;
4
5
use JwPersistentUser\Authentication\Adapter\ForceLogin;
6
7
use Zend\Http\Request;
8
use Zend\Http\Response;
9
use Zend\ServiceManager\ServiceLocatorInterface;
10
use Zend\Stdlib\RequestInterface;
11
use Zend\Stdlib\ResponseInterface;
12
use Zend\EventManager\EventManagerAwareTrait;
13
use Zend\Authentication\AuthenticationService;
14
15
use ZfcUser\Mapper\User as ZfcUserMapper;
16
17
/**
18
 * Note that dependencies in this service are pulled directory out of the service locator for
19
 * a reason: performance. Would we NOT do this than we would instantiate those resources on
20
 * every request.
21
 */
22
class CookieAuthenticationService
23
{
24
    use EventManagerAwareTrait;
25
26
    /**
27
     * @var ServiceLocatorInterface
28
     */
29
    protected $serviceLocator;
30
31
    /**
32
     * @var AuthenticationService
33
     */
34
    protected $authService;
35
36
    /**
37
     * @var RememberMeService
38
     */
39
    protected $rememberMeService;
40
41
    /**
42
     * @var CookieService
43
     */
44
    protected $cookieService;
45
46
    /**
47
     * @var ZfcUserMapper
48
     */
49
    protected $userMapper;
50
51 1
    /**
52
     * @param ServiceLocatorInterface $serviceLocator
53 1
     */
54
    public function __construct(ServiceLocatorInterface $serviceLocator)
55
    {
56
        $this->serviceLocator = $serviceLocator;
57 1
    }
58 1
59
    /**
60
     * @param RequestInterface $request
61
     * @param ResponseInterface $response
62
     */
63
    public function loginFrom(RequestInterface $request, ResponseInterface $response)
64
    {
65 1
        if (!($request instanceof Request) || !($response instanceof Response)) {
66
            return;
67
        }
68
69 1
        $serieToken = $this->getCookieService()->read($request, $response);
70 1
        if (!$serieToken) {
71
            return;
72
        }
73
74
        // TODO use a session once we validated using Cookie. So we do not need to instantiate
75 1
        //      the AuthService on every following request.
76 1
77
        if ($this->getAuthService()->hasIdentity()) {
78
            return;
79
        }
80
81 1
        $user = $this->getUserMapper()->findById($serieToken->getUserId());
82
        if (!$user) {
83 1
            $this->invalidAttempt($response);
84
            return;
85 1
        }
86 1
87 1
        $nextSerieToken = $this->getRememberMeService()->getNextInSerie($serieToken);
88 1
        if (!$nextSerieToken) {
89
            $this->invalidAttempt($response);
90
            return;
91
        }
92
93
        $this->getCookieService()->writeSerie($response, $nextSerieToken);
94
95
        $this->getAuthService()->authenticate(new ForceLogin($nextSerieToken->getUserId()));
96
97
        $this->getEventManager()->trigger('login', $this, [
98
            'token' => $nextSerieToken,
99
        ]);
100
    }
101
102
    /**
103 1
     * @param Response $response
104
     */
105 1
    protected function invalidAttempt(Response $response)
106
    {
107
        $this->getAuthService()->clearIdentity();
108
109 1
        $this->getCookieService()->writeNull($response);
110
    }
111
112
    /**
113
     * @return AuthenticationService
114
     */
115
    public function getAuthService()
116 1
    {
117
        if ($this->authService === null) {
118 1
            $this->authService = $this->serviceLocator->get('zfcuser_auth_service');
119 1
        }
120
121
        return $this->authService;
122
    }
123
124
    /**
125 1
     * @param AuthenticationService $authService
126
     * @return $this
127 1
     */
128
    public function setAuthService($authService)
129
    {
130
        $this->authService = $authService;
131 1
        return $this;
132
    }
133
134
    /**
135
     * @return RememberMeService
136
     */
137
    public function getRememberMeService()
138 1
    {
139
        if ($this->rememberMeService === null) {
140 1
            $this->rememberMeService = $this->serviceLocator->get('JwPersistentUser\Service\RememberMe');
141 1
        }
142
143
        return $this->rememberMeService;
144
    }
145
146
    /**
147 1
     * @param RememberMeService $rememberMeService
148
     * @return $this
149 1
     */
150
    public function setRememberMeService($rememberMeService)
151
    {
152 1
        $this->rememberMeService = $rememberMeService;
153
        return $this;
154
    }
155
156
    /**
157
     * @return CookieService
158
     */
159 1
    public function getCookieService()
160
    {
161 1
        if ($this->cookieService === null) {
162 1
            $this->cookieService = $this->serviceLocator->get('JwPersistentUser\Service\Cookie');
163
        }
164
        return $this->cookieService;
165
    }
166
167
    /**
168 1
     * @param CookieService $cookieService
169
     * @return $this
170 1
     */
171
    public function setCookieService($cookieService)
172
    {
173
        $this->cookieService = $cookieService;
174 1
        return $this;
175
    }
176
177
    /**
178
     * @return ZfcUserMapper
179
     */
180 1
    public function getUserMapper()
181
    {
182 1
        if ($this->userMapper === null) {
183 1
            $this->userMapper = $this->serviceLocator->get('zfcuser_user_mapper');
184
        }
185
186
        return $this->userMapper;
187
    }
188
189
    /**
190
     * @param ZfcUserMapper $mapper
191
     */
192
    public function setUserMapper(ZfcUserMapper $mapper)
193
    {
194
        $this->userMapper = $mapper;
195
    }
196
}
197