Completed
Push — master ( dfec5f...1a2fe9 )
by Mārtiņš
02:17
created

Search::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
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)
125
    {
126 2
        $identity = new Entity\StandardIdentity;
127
128 2
        $identity->setToken($token);
129 2
        $identity->setTokenAction($action);
130 2
        $identity->setTokenEndOfLife(time());
131
132 2
        $this->repository->load($identity, Entity\Identity::class);
133
134 2
        if ($identity->getId() === null) {
135 1
            $this->logger->notice('identity not found', [
136
                'input' => [
137 1
                    'token' => $token,
138
                ],
139
            ]);
140
141 1
            throw new IdentityNotFound;
142
        }
143
144 1
        $this->repository->load($identity);    
145
146 1
        return $identity;
147
    }
148
149
150
    /**
151
     * @param int $identityId
152
     *
153
     * @throws Palladium\Exception\IdentityNotFound if identity was not found
154
     *
155
     * @return Palladium\Entity\StandardIdentity
156
     */
157 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...
158
    {
159
        $identity = new Entity\StandardIdentity;
160
        $identity->setId($identityId);
161
162
        $this->repository->load($identity);
163
164
        if ($identity->getAccountId() === null) {
165
            $this->logger->notice('identity not found', [
166
                'input' => [
167
                    'id' => $identityId,
168
                ],
169
            ]);
170
171
            throw new IdentityNotFound;
172
        }
173
174
        return $identity;
175
    }
176
177
178
    /**
179
     * @param int $accountId
180
     * @param string $series
181
     *
182
     * @throws Palladium\Exception\IdentityNotFound if identity was not found
183
     *
184
     * @return Palladium\Entity\CookieIdentity
185
     */
186 2
    public function findCookieIdentity($accountId, $series)
187
    {
188 2
        $cookie = new Entity\CookieIdentity;
189 2
        $cookie->setStatus(Entity\Identity::STATUS_ACTIVE);
190 2
        $cookie->setAccountId($accountId);
191 2
        $cookie->setSeries($series);
192
193 2
        $this->repository->load($cookie);
194
195 2
        if ($cookie->getId() === null) {
196 1
            $this->logger->notice('identity not found', [
197
                'input' => [
198 1
                    'account' => $cookie->getAccountId(),
199 1
                    'series' => $cookie->getSeries(),
200
                ],
201
            ]);
202
203 1
            throw new IdentityNotFound;
204
        }
205
206 1
        return $cookie;
207
    }
208
209
210
    /**
211
     * @return Palladium\Entity\IdentityCollection
212
     */
213 1
    public function findIdentitiesByAccountId($accountId, $type = Entity\Identity::TYPE_ANY, $status = Entity\Identity::STATUS_ACTIVE)
214
    {
215 1
        $collection = new Entity\IdentityCollection;
216 1
        $collection->forAccountId($accountId);
217 1
        $collection->forType($type);
218
219 1
        return $this->fetchIdentitiesWithStatus($collection, $status);
220
    }
221
222
223
    /**
224
     * @return Palladium\Entity\IdentityCollection
225
     */
226 1
    public function findIdentitiesByParentId($parentId, $status = Entity\Identity::STATUS_ACTIVE)
227
    {
228 1
        $collection = new Entity\IdentityCollection;
229 1
        $collection->forParentId($parentId);
230
231 1
        return $this->fetchIdentitiesWithStatus($collection, $status);
232
    }
233
234
235
    /**
236
     * @return Palladium\Entity\IdentityCollection
237
     */
238 2
    private function fetchIdentitiesWithStatus(Entity\IdentityCollection $collection, $status)
239
    {
240 2
        $collection->forStatus($status);
241 2
        $this->repository->load($collection);
242
243 2
        return $collection;
244
    }
245
}
246