Completed
Push — master ( b75f88...ce2824 )
by Mārtiņš
02:02
created

Search::findIdentityById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 20
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 20
loc 20
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Palladium\Service;
4
5
6
/**
7
 * Class for finding identities based on various conditions
8
 */
9
10
use Palladium\Mapper as Mapper;
11
use Palladium\Entity as Entity;
12
use Palladium\Exception\UserNotFound;
13
use Palladium\Exception\IdentityNotFound;
14
15
use Palladium\Contract\CanCreateMapper;
16
use Psr\Log\LoggerInterface;
17
18
19
class Search
20
{
21
22
    private $mapperFactory;
23
    private $logger;
24
25
    /**
26
     * @param Palladium\Contract\CanCreateMapper $mapperFactory Factory for creating persistence layer structures
27
     * @param Psr\Log\LoggerInterface $logger PSR-3 compatible logger
28
     */
29 12
    public function __construct(CanCreateMapper $mapperFactory, LoggerInterface $logger)
30
    {
31 12
        $this->mapperFactory = $mapperFactory;
32 12
        $this->logger = $logger;
33 12
    }
34
35
36
    /**
37
     * Locates identity based on ID
38
     *
39
     * @param int $identityId
40
     *
41
     * @throws Palladium\Exception\IdentityNotFound if identity was not found
42
     *
43
     * @return Palladium\Entity\Identity
44
     */
45 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...
46
    {
47 2
        $identity = new Entity\Identity;
48 2
        $identity->setId($identityId);
49
50 2
        $mapper = $this->mapperFactory->create(Mapper\Identity::class);
51 2
        $mapper->fetch($identity);
52
53 2
        if ($identity->getAccountId() === null) {
54 1
            $this->logger->notice('identity not found', [
55
                'input' => [
56 1
                    'id' => $identityId,
57
                ],
58
            ]);
59
60 1
            throw new IdentityNotFound;
61
        }
62
63 1
        return $identity;
64
    }
65
66
67
    /**
68
     * Locates identity based on email address
69
     *
70
     * @param string $emailAddress
71
     *
72
     * @throws Palladium\Exception\IdentityNotFound if identity was not found
73
     *
74
     * @return Palladium\Entity\EmailIdentity
75
     */
76 2 View Code Duplication
    public function findEmailIdentityByEmailAddress(string $emailAddress)
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...
77
    {
78 2
        $identity = new Entity\EmailIdentity;
79 2
        $identity->setEmailAddress($emailAddress);
80
81 2
        $mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class);
82 2
        $mapper->fetch($identity);
83
84 2
        if ($identity->getId() === null) {
85 1
            $this->logger->notice('identity not found', [
86
                'input' => [
87 1
                    'email' => $emailAddress,
88
                ],
89
            ]);
90
91 1
            throw new IdentityNotFound;
92
        }
93
94 1
        return $identity;
95
    }
96
97
98 2
    public function findNonceIdentityByIdentifier(string $identifier)
99
    {
100 2
        $identity = new Entity\NonceIdentity;
101 2
        $identity->setIdentifier($identifier);
102
103 2
        $mapper = $this->mapperFactory->create(Mapper\NonceIdentity::class);
104 2
        $mapper->fetch($identity);
105
106 2
        if ($identity->getId() === null) {
107 1
            $this->logger->notice('identity not found', [
108
                'input' => [
109 1
                    'identifier' => $identifier,
110
                ],
111
            ]);
112
113 1
            throw new IdentityNotFound;
114
        }
115
116 1
        return $identity;
117
    }
118
119
120
    /**
121
     * @param string $token
122
     * @param int $action
123
     *
124
     * @throws Palladium\Exception\IdentityNotFound if identity was not found
125
     *
126
     * @return Palladium\Entity\EmailIdentity
127
     */
128 2 View Code Duplication
    public function findEmailIdentityByToken(string $token, $action = Entity\Identity::ACTION_NONE)
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...
129
    {
130 2
        $identity = new Entity\EmailIdentity;
131
132 2
        $identity->setToken($token);
133 2
        $identity->setTokenAction($action);
134 2
        $identity->setTokenEndOfLife(time());
135
136 2
        $mapper = $this->mapperFactory->create(Mapper\Identity::class);
137 2
        $mapper->fetch($identity);
138
139 2
        if ($identity->getId() === null) {
140 1
            $this->logger->notice('identity not found', [
141
                'input' => [
142 1
                    'token' => $token,
143
                ],
144
            ]);
145
146 1
            throw new IdentityNotFound;
147
        }
148
149 1
        return $identity;
150
    }
151
152
153
    /**
154
     * @param int $identityId
155
     *
156
     * @throws Palladium\Exception\IdentityNotFound if identity was not found
157
     *
158
     * @return Palladium\Entity\EmailIdentity
159
     */
160 View Code Duplication
    public function findEmailIdentityById(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...
161
    {
162
        $identity = new Entity\EmailIdentity;
163
        $identity->setId($identityId);
164
165
        $mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class);
166
        $mapper->fetch($identity);
167
168
        if ($identity->getAccountId() === null) {
169
            $this->logger->notice('identity not found', [
170
                'input' => [
171
                    'id' => $identityId,
172
                ],
173
            ]);
174
175
            throw new IdentityNotFound;
176
        }
177
178
        return $identity;
179
    }
180
181
182
    /**
183
     * @param int $accountId
184
     * @param string $series
185
     *
186
     * @throws Palladium\Exception\IdentityNotFound if identity was not found
187
     *
188
     * @return Palladium\Entity\CookieIdentity
189
     */
190 2 View Code Duplication
    public function findCookieIdentity($accountId, $series)
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...
191
    {
192 2
        $cookie = new Entity\CookieIdentity;
193 2
        $cookie->setStatus(Entity\Identity::STATUS_ACTIVE);
194 2
        $cookie->setAccountId($accountId);
195 2
        $cookie->setSeries($series);
196
197 2
        $mapper = $this->mapperFactory->create(Mapper\CookieIdentity::class);
198 2
        $mapper->fetch($cookie);
199
200 2
        if ($cookie->getId() === null) {
201 1
            $this->logger->notice('identity not found', [
202
                'input' => [
203 1
                    'account' => $cookie->getAccountId(),
204 1
                    'series' => $cookie->getSeries(),
205
                ],
206
            ]);
207
208 1
            throw new IdentityNotFound;
209
        }
210
211 1
        return $cookie;
212
    }
213
214
215
    /**
216
     * @return Palladium\Entity\IdentityCollection
217
     */
218 1
    public function findIdentitiesByAccountId($accountId, $type = Entity\Identity::TYPE_ANY, $status = Entity\Identity::STATUS_ACTIVE)
219
    {
220 1
        $collection = new Entity\IdentityCollection;
221 1
        $collection->forAccountId($accountId);
222 1
        $collection->forType($type);
223
224 1
        return $this->fetchIdentitiesWithStatus($collection, $status);
225
    }
226
227
228
    /**
229
     * @return Palladium\Entity\IdentityCollection
230
     */
231 1
    public function findIdentitiesByParentId($parentId, $status = Entity\Identity::STATUS_ACTIVE)
232
    {
233 1
        $collection = new Entity\IdentityCollection;
234 1
        $collection->forParentId($parentId);
235
236 1
        return $this->fetchIdentitiesWithStatus($collection, $status);
237
    }
238
239
240
    /**
241
     * @return Palladium\Entity\IdentityCollection
242
     */
243 2
    private function fetchIdentitiesWithStatus(Entity\IdentityCollection $collection, $status)
244
    {
245 2
        $collection->forStatus($status);
246
247 2
        $mapper = $this->mapperFactory->create(Mapper\IdentityCollection::class);
248 2
        $mapper->fetch($collection);
249
250 2
        return $collection;
251
    }
252
}
253