Search::findIdentitiesByParentId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 3
c 2
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 1
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\IdentityNotFound;
12
use Palladium\Repository\Identity as Repository;
13
use Psr\Log\LoggerInterface;
14
15
16
class Search
17
{
18
19
    private $repository;
20
    private $logger;
21
22
    /**
23
     * @param Repository $repository Repository for abstracting persistence layer structures
24
     * @param LoggerInterface $logger PSR-3 compatible logger
25
     */
26 38
    public function __construct(Repository $repository, LoggerInterface $logger)
27
    {
28 38
        $this->repository = $repository;
29 38
        $this->logger = $logger;
30 38
    }
31
32
33
    /**
34
     * Locates identity based on ID
35
     *
36
     * @throws IdentityNotFound if identity was not found
37
     */
38 2
    public function findIdentityById(int $identityId): Entity\Identity
39
    {
40 2
        $identity = new Entity\Identity;
41 2
        $identity->setId($identityId);
42
43 2
        $this->repository->load($identity, Entity\Identity::class);
44
45 2
        if ($identity->getAccountId() === null) {
46 1
            $this->logger->notice('identity not found', [
47
                'input' => [
48 1
                    'id' => $identityId,
49
                ],
50
            ]);
51
52 1
            throw new IdentityNotFound;
53
        }
54
55 1
        return $identity;
56
    }
57
58
59
    /**
60
     * Locates identity based on email address
61
     *
62
     * @throws IdentityNotFound if identity was not found
63
     */
64 13
    public function findStandardIdentityByIdentifier(string $identifier): Entity\StandardIdentity
65
    {
66 13
        $identity = new Entity\StandardIdentity;
67 13
        $identity->setIdentifier($identifier);
68
69 13
        $this->repository->load($identity);
70
71 13
        if ($identity->getId() === null) {
72 2
            $this->logger->notice('identity not found', [
73
                'input' => [
74 2
                    'identifier' => $identifier,
75
                ],
76
            ]);
77
78 2
            throw new IdentityNotFound;
79
        }
80
81 11
        return $identity;
82
    }
83
84
85 5
    public function findNonceIdentityByIdentifier(string $identifier): Entity\NonceIdentity
86
    {
87 5
        $identity = new Entity\NonceIdentity;
88 5
        $identity->setIdentifier($identifier);
89
90 5
        $this->repository->load($identity);
91
92 5
        if ($identity->getId() === null) {
93 2
            $this->logger->notice('identity not found', [
94
                'input' => [
95 2
                    'identifier' => $identifier,
96
                ],
97
            ]);
98
99 2
            throw new IdentityNotFound;
100
        }
101
102 3
        return $identity;
103
    }
104
105
106
    /**
107
     * @throws IdentityNotFound if identity was not found
108
     */
109 8
    public function findStandardIdentityByToken(string $token, int $action = Entity\Identity::ACTION_NONE): Entity\StandardIdentity
110
    {
111 8
        $entry = $this->findIdentityByToken($token, $action);
0 ignored issues
show
Bug introduced by
It seems like $action can also be of type null; however, parameter $action of Palladium\Service\Search::findIdentityByToken() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

111
        $entry = $this->findIdentityByToken($token, /** @scrutinizer ignore-type */ $action);
Loading history...
112
113 8
        if ($entry->getId() === null) {
114 4
            $this->logger->notice('identity not found', [
115
                'input' => [
116 4
                    'token' => $token,
117
                ],
118
            ]);
119
120 4
            throw new IdentityNotFound;
121
        }
122
123
124 4
        $identity = new Entity\StandardIdentity;
125 4
        $identity->setId($entry->getId());
126
127 4
        $this->repository->load($identity);
128
129 4
        return $identity;
130
    }
131
132
133
    /**
134
     * @throws IdentityNotFound if identity was not found
135
     * @throws \Palladium\Exception\InvalidToken
136
     */
137 8
     public function findIdentityByToken(string $token, int $action = Entity\Identity::ACTION_NONE): Entity\Identity
138
     {
139 8
         $identity = new Entity\Identity;
140
141 8
         $identity->setToken($token);
142 8
         $identity->setTokenAction($action);
143 8
         $identity->setTokenEndOfLife(time());
144
145 8
         $this->repository->load($identity);
146
147 8
         return $identity;
148
     }
149
150
    /**
151
     * @throws IdentityNotFound if identity was not found
152
     */
153 5
    public function findStandardIdentityById(int $identityId): Entity\StandardIdentity
154
    {
155 5
        $identity = new Entity\StandardIdentity;
156 5
        $identity->setId($identityId);
157
158 5
        $this->repository->load($identity);
159
160 5
        if ($identity->getAccountId() === null) {
161 1
            $this->logger->notice('identity not found', [
162
                'input' => [
163 1
                    'id' => $identityId,
164
                ],
165
            ]);
166
167 1
            throw new IdentityNotFound;
168
        }
169
170 4
        return $identity;
171
    }
172
173
174
    /**
175
     * @throws IdentityNotFound if identity was not found
176
     */
177 8
    public function findCookieIdentity(int $accountId, string $series): Entity\CookieIdentity
178
    {
179 8
        $cookie = new Entity\CookieIdentity;
180 8
        $cookie->setStatus(Entity\Identity::STATUS_ACTIVE);
181 8
        $cookie->setAccountId($accountId);
182 8
        $cookie->setSeries($series);
183
184 8
        $this->repository->load($cookie);
185
186 8
        if ($cookie->getId() === null) {
187 3
            $this->logger->notice('identity not found', [
188
                'input' => [
189 3
                    'account' => $cookie->getAccountId(),
190 3
                    'series' => $cookie->getSeries(),
191
                ],
192
            ]);
193
194 3
            throw new IdentityNotFound;
195
        }
196
197 7
        return $cookie;
198
    }
199
200
201 1
    public function findIdentitiesByAccountId(int $accountId, int $type = Entity\Identity::TYPE_ANY, int $status = Entity\Identity::STATUS_ACTIVE): Entity\IdentityCollection
202
    {
203 1
        $collection = new Entity\IdentityCollection;
204 1
        $collection->forAccountId($accountId);
205 1
        $collection->forType($type);
206
207 1
        return $this->fetchIdentitiesWithStatus($collection, $status);
208
    }
209
210
211 1
    public function findIdentitiesByParentId(int $parentId, int $status = Entity\Identity::STATUS_ACTIVE): Entity\IdentityCollection
212
    {
213 1
        $collection = new Entity\IdentityCollection;
214 1
        $collection->forParentId($parentId);
215
216 1
        return $this->fetchIdentitiesWithStatus($collection, $status);
217
    }
218
219
220 2
    private function fetchIdentitiesWithStatus(Entity\IdentityCollection $collection, int $status): Entity\IdentityCollection
221
    {
222 2
        $collection->forStatus($status);
223 2
        $this->repository->load($collection);
224
225 2
        return $collection;
226
    }
227
}
228