Completed
Pull Request — master (#13)
by
unknown
17:25 queued 14:28
created

RememberMeService::setIpService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace JwPersistentUser\Service;
4
5
use JwPersistentUser\Model\ModuleOptions;
6
use JwPersistentUser\Model\SerieTokenInterface;
7
use JwPersistentUser\Mapper\SerieTokenMapperInterface;
8
9
use Zend\Math\Rand;
10
11
class RememberMeService
12
{
13
    /**
14
     * @var SerieTokenMapperInterface
15
     */
16
    protected $mapper;
17
18
    /**
19
     * @var ModuleOptions
20
     */
21
    protected $moduleOptions;
22
23
    /**
24
     * @var UserValidityInterface
25
     */
26
    protected $userValidityInterface;
27
28
    /**
29
     * @param int $userId
30
     * @return SerieTokenInterface
31
     */
32
    public function createNew($userId)
33
    {
34
        $serieTokenClass = $this->getModuleOptions()->getSerieTokenEntityClass();
35
        $serieToken = new $serieTokenClass;
36
37
        /** @var $serieToken SerieTokenInterface */
38
        $serieToken->setUserId($userId);
39 1
        $serieToken->setSerie($this->generateRandom());
40
        $serieToken->setToken($this->generateRandom());
41 1
        $serieToken->setExpiresAt($this->getNewExpireDate());
42 1
        $serieToken->setValidUntil($this->getUserValidityInterface()->getValidUntilForUser($userId));
43
44
        $this->getMapper()->persist($serieToken);
45 1
46 1
        return $serieToken;
47 1
    }
48 1
49 1
    /**
50
     * @param SerieTokenInterface $serieToken
51 1
     * @return SerieTokenInterface|null
52 1
     */
53 1
    public function getNextInSerie(SerieTokenInterface $serieToken)
54 1
    {
55
        $matchingSerieToken = $this->getMapper()->find($serieToken->getUserId(), $serieToken->getSerie());
56
        if (!$matchingSerieToken) {
57
            return null;
58 1
        } elseif ($matchingSerieToken->getValidUntil() && $matchingSerieToken->getValidUntil() < new \DateTime) {
59
            return null;
60 1
        } elseif (!$matchingSerieToken->getExpiresAt() || $matchingSerieToken->getExpiresAt() < new \DateTime) {
61
            return null;
62
        } elseif ($matchingSerieToken->getToken() !== $serieToken->getToken()) {
63
            $this->removeSerie($serieToken->getUserId(), $serieToken->getSerie());
64
            return null;
65
        }
66
67 4
        if ($this->moduleOptions->isTokenRefreshedAfterLogin()) {
68
            // Generate new token in the serie
69 4
            $matchingSerieToken->setToken($this->generateRandom());
70 4
        }
71 1
72 3
        $matchingSerieToken->setExpiresAt($this->getNewExpireDate());
73 1
74 2
        $this->getMapper()->persist($matchingSerieToken);
75 1
76 1
        return $matchingSerieToken;
77
    }
78
79
    /**
80 1
     * @return \DateTime
81 1
     */
82 1
    protected function getNewExpireDate()
83
    {
84 1
        return new \DateTime('+1 year');
85
    }
86
87
    /**
88
     * @param int $userId
89
     * @param string $serieId
90 2
     */
91
    public function removeSerie($userId, $serieId)
92 2
    {
93
        $serieToken = $this->getMapper()->find($userId, $serieId);
94
        if ($serieToken) {
95
            $this->getMapper()->remove($serieToken);
96
        }
97
    }
98
99 2
    /**
100
     * @note Should NOT contain semicolons.
101 2
     *
102 2
     * @return string
103 2
     */
104 2
    private function generateRandom()
105 2
    {
106
        return Rand::getString(10);
107
    }
108
109
    /**
110
     * @return SerieTokenMapper
111
     */
112 2
    public function getMapper()
113
    {
114 2
        return $this->mapper;
115
    }
116
117
    /**
118
     * @param SerieTokenMapper $mapper
119
     * @return $this
120 6
     */
121
    public function setMapper($mapper)
122 6
    {
123
        $this->mapper = $mapper;
0 ignored issues
show
Documentation Bug introduced by
It seems like $mapper of type object<JwPersistentUser\Service\SerieTokenMapper> is incompatible with the declared type object<JwPersistentUser\...ieTokenMapperInterface> of property $mapper.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
124
        return $this;
125
    }
126
127
    /**
128
     * @return ModuleOptions
129 6
     */
130
    public function getModuleOptions()
131 6
    {
132 6
        return $this->moduleOptions;
133
    }
134
135
    /**
136
     * @param ModuleOptions $moduleOptions
137
     * @return $this
138 1
     */
139
    public function setModuleOptions($moduleOptions)
140 1
    {
141
        $this->moduleOptions = $moduleOptions;
142
        return $this;
143
    }
144
145
    /**
146
     * @return UserValidityInterface
147 6
     */
148
    public function getUserValidityInterface()
149 6
    {
150 6
        return $this->userValidityInterface;
151
    }
152
153
    /**
154
     * @param UserValidityInterface $userValidityInterface
155
     * @return $this
156
     */
157 6
    public function setUserValidityInterface($userValidityInterface)
158
    {
159 6
        $this->userValidityInterface = $userValidityInterface;
160 6
        return $this;
161
    }
162
}
163