Completed
Push — master ( 5d2ad9...beb91f )
by Mārtiņš
02:03
created

Search::findIdentityByToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Palladium\Service;
4
5
6
/**
7
 * Class for finding identities based on various conditions
8
 */
9
10
use Palladium\Entity as Entity;
11
use Palladium\Exception\UserNotFound;
12
use Palladium\Exception\IdentityNotFound;
13
14
use Palladium\Repository\Identity as Repository;
15
use Psr\Log\LoggerInterface;
16
17
18
class Search
19
{
20
21
    private $repository;
22
    private $logger;
23
24
    /**
25
     * @param Palladium\Repository\Identity $repository Repository for abstracting persistence layer structures
26
     * @param Psr\Log\LoggerInterface $logger PSR-3 compatible logger
27
     */
28 12
    public function __construct(Repository $repository, LoggerInterface $logger)
29
    {
30 12
        $this->repository = $repository;
31 12
        $this->logger = $logger;
32 12
    }
33
34
35
    /**
36
     * Locates identity based on ID
37
     *
38
     * @param int $identityId
39
     *
40
     * @throws Palladium\Exception\IdentityNotFound if identity was not found
41
     *
42
     * @return Palladium\Entity\Identity
43
     */
44 2 View Code Duplication
    public function findIdentityById(int $identityId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46 2
        $identity = new Entity\Identity;
47 2
        $identity->setId($identityId);
48
49 2
        $this->repository->load($identity, Entity\Identity::class);
50
51 2
        if ($identity->getAccountId() === null) {
52 1
            $this->logger->notice('identity not found', [
53
                'input' => [
54 1
                    'id' => $identityId,
55
                ],
56
            ]);
57
58 1
            throw new IdentityNotFound;
59
        }
60
61 1
        return $identity;
62
    }
63
64
65
    /**
66
     * Locates identity based on email address
67
     *
68
     * @param string $identifier
69
     *
70
     * @throws Palladium\Exception\IdentityNotFound if identity was not found
71
     *
72
     * @return Palladium\Entity\StandardIdentity
73
     */
74 2 View Code Duplication
    public function findStandardIdentityByIdentifier(string $identifier)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76 2
        $identity = new Entity\StandardIdentity;
77 2
        $identity->setIdentifier($identifier);
78
79 2
        $this->repository->load($identity);
80
81 2
        if ($identity->getId() === null) {
82 1
            $this->logger->notice('identity not found', [
83
                'input' => [
84 1
                    'identifier' => $identifier,
85
                ],
86
            ]);
87
88 1
            throw new IdentityNotFound;
89
        }
90
91 1
        return $identity;
92
    }
93
94
95 2 View Code Duplication
    public function findNonceIdentityByIdentifier(string $identifier)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97 2
        $identity = new Entity\NonceIdentity;
98 2
        $identity->setIdentifier($identifier);
99
100 2
        $this->repository->load($identity);
101
102 2
        if ($identity->getId() === null) {
103 1
            $this->logger->notice('identity not found', [
104
                'input' => [
105 1
                    'identifier' => $identifier,
106
                ],
107
            ]);
108
109 1
            throw new IdentityNotFound;
110
        }
111
112 1
        return $identity;
113
    }
114
115
116
    /**
117
     * @param string $token
118
     * @param int $action
119
     *
120
     * @throws Palladium\Exception\IdentityNotFound if identity was not found
121
     *
122
     * @return Palladium\Entity\StandardIdentity
123
     */
124 2
    public function findStandardIdentityByToken(string $token, $action = Entity\Identity::ACTION_NONE): Entity\StandardIdentity
125
    {
126 2
        $entry = $this->findIdentityByToken($token, $action);
127
128 2
        if ($entry->getId() === null) {
129 1
            $this->logger->notice('identity not found', [
130
                'input' => [
131 1
                    'token' => $token,
132
                ],
133
            ]);
134
135 1
            throw new IdentityNotFound;
136
        }
137
138
139 1
        $identity = new Entity\StandardIdentity;
140 1
        $identity->setId($entry->getId());
141
142 1
        $this->repository->load($identity);
143
144 1
        return $identity;
145
    }
146
147
148
    /**
149
     * @param string $token
150
     * @param int $action
151
     *
152
     * @throws Palladium\Exception\IdentityNotFound if identity was not found
153
     *
154
     * @return Palladium\Entity\Identity
155
     */
156 2
     public function findIdentityByToken(string $token, $action = Entity\Identity::ACTION_NONE): Entity\Identity
157
     {
158 2
         $identity = new Entity\Identity;
159
160 2
         $identity->setToken($token);
161 2
         $identity->setTokenAction($action);
162 2
         $identity->setTokenEndOfLife(time());
163
164 2
         $this->repository->load($identity);
165
166 2
         return $identity;
167
     }
168
169
    /**
170
     * @param int $identityId
171
     *
172
     * @throws Palladium\Exception\IdentityNotFound if identity was not found
173
     *
174
     * @return Palladium\Entity\StandardIdentity
175
     */
176 View Code Duplication
    public function findStandardIdentityById(int $identityId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
    {
178
        $identity = new Entity\StandardIdentity;
179
        $identity->setId($identityId);
180
181
        $this->repository->load($identity);
182
183
        if ($identity->getAccountId() === null) {
184
            $this->logger->notice('identity not found', [
185
                'input' => [
186
                    'id' => $identityId,
187
                ],
188
            ]);
189
190
            throw new IdentityNotFound;
191
        }
192
193
        return $identity;
194
    }
195
196
197
    /**
198
     * @param int $accountId
199
     * @param string $series
200
     *
201
     * @throws Palladium\Exception\IdentityNotFound if identity was not found
202
     *
203
     * @return Palladium\Entity\CookieIdentity
204
     */
205 2
    public function findCookieIdentity($accountId, $series)
206
    {
207 2
        $cookie = new Entity\CookieIdentity;
208 2
        $cookie->setStatus(Entity\Identity::STATUS_ACTIVE);
209 2
        $cookie->setAccountId($accountId);
210 2
        $cookie->setSeries($series);
211
212 2
        $this->repository->load($cookie);
213
214 2
        if ($cookie->getId() === null) {
215 1
            $this->logger->notice('identity not found', [
216
                'input' => [
217 1
                    'account' => $cookie->getAccountId(),
218 1
                    'series' => $cookie->getSeries(),
219
                ],
220
            ]);
221
222 1
            throw new IdentityNotFound;
223
        }
224
225 1
        return $cookie;
226
    }
227
228
229
    /**
230
     * @return Palladium\Entity\IdentityCollection
231
     */
232 1
    public function findIdentitiesByAccountId($accountId, $type = Entity\Identity::TYPE_ANY, $status = Entity\Identity::STATUS_ACTIVE)
233
    {
234 1
        $collection = new Entity\IdentityCollection;
235 1
        $collection->forAccountId($accountId);
236 1
        $collection->forType($type);
237
238 1
        return $this->fetchIdentitiesWithStatus($collection, $status);
239
    }
240
241
242
    /**
243
     * @return Palladium\Entity\IdentityCollection
244
     */
245 1
    public function findIdentitiesByParentId($parentId, $status = Entity\Identity::STATUS_ACTIVE)
246
    {
247 1
        $collection = new Entity\IdentityCollection;
248 1
        $collection->forParentId($parentId);
249
250 1
        return $this->fetchIdentitiesWithStatus($collection, $status);
251
    }
252
253
254
    /**
255
     * @return Palladium\Entity\IdentityCollection
256
     */
257 2
    private function fetchIdentitiesWithStatus(Entity\IdentityCollection $collection, $status)
258
    {
259 2
        $collection->forStatus($status);
260 2
        $this->repository->load($collection);
261
262 2
        return $collection;
263
    }
264
}
265