Completed
Push — master ( 734e62...750475 )
by Mārtiņš
03:48
created

Search   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 226
Duplicated Lines 33.63 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 88.16%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 16
lcom 1
cbo 8
dl 76
loc 226
ccs 67
cts 76
cp 0.8816
rs 10
c 3
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A findIdentityById() 19 19 2
A findNonceIdentityByIdentifier() 19 19 2
A findCookieIdentity() 0 22 2
A findIdentitiesByAccountId() 0 8 1
A findIdentitiesByParentId() 0 7 1
A fetchIdentitiesWithStatus() 0 7 1
A findStandardIdentityByIdentifier() 19 19 2
A findStandardIdentityByToken() 0 22 2
A findStandardIdentityById() 19 19 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
        return $identity;
145
    }
146
147
148
    /**
149
     * @param int $identityId
150
     *
151
     * @throws Palladium\Exception\IdentityNotFound if identity was not found
152
     *
153
     * @return Palladium\Entity\StandardIdentity
154
     */
155 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...
156
    {
157
        $identity = new Entity\StandardIdentity;
158
        $identity->setId($identityId);
159
160
        $this->repository->load($identity);
161
162
        if ($identity->getAccountId() === null) {
163
            $this->logger->notice('identity not found', [
164
                'input' => [
165
                    'id' => $identityId,
166
                ],
167
            ]);
168
169
            throw new IdentityNotFound;
170
        }
171
172
        return $identity;
173
    }
174
175
176
    /**
177
     * @param int $accountId
178
     * @param string $series
179
     *
180
     * @throws Palladium\Exception\IdentityNotFound if identity was not found
181
     *
182
     * @return Palladium\Entity\CookieIdentity
183
     */
184 2
    public function findCookieIdentity($accountId, $series)
185
    {
186 2
        $cookie = new Entity\CookieIdentity;
187 2
        $cookie->setStatus(Entity\Identity::STATUS_ACTIVE);
188 2
        $cookie->setAccountId($accountId);
189 2
        $cookie->setSeries($series);
190
191 2
        $this->repository->load($cookie);
192
193 2
        if ($cookie->getId() === null) {
194 1
            $this->logger->notice('identity not found', [
195
                'input' => [
196 1
                    'account' => $cookie->getAccountId(),
197 1
                    'series' => $cookie->getSeries(),
198
                ],
199
            ]);
200
201 1
            throw new IdentityNotFound;
202
        }
203
204 1
        return $cookie;
205
    }
206
207
208
    /**
209
     * @return Palladium\Entity\IdentityCollection
210
     */
211 1
    public function findIdentitiesByAccountId($accountId, $type = Entity\Identity::TYPE_ANY, $status = Entity\Identity::STATUS_ACTIVE)
212
    {
213 1
        $collection = new Entity\IdentityCollection;
214 1
        $collection->forAccountId($accountId);
215 1
        $collection->forType($type);
216
217 1
        return $this->fetchIdentitiesWithStatus($collection, $status);
218
    }
219
220
221
    /**
222
     * @return Palladium\Entity\IdentityCollection
223
     */
224 1
    public function findIdentitiesByParentId($parentId, $status = Entity\Identity::STATUS_ACTIVE)
225
    {
226 1
        $collection = new Entity\IdentityCollection;
227 1
        $collection->forParentId($parentId);
228
229 1
        return $this->fetchIdentitiesWithStatus($collection, $status);
230
    }
231
232
233
    /**
234
     * @return Palladium\Entity\IdentityCollection
235
     */
236 2
    private function fetchIdentitiesWithStatus(Entity\IdentityCollection $collection, $status)
237
    {
238 2
        $collection->forStatus($status);
239 2
        $this->repository->load($collection);
240
241 2
        return $collection;
242
    }
243
}
244