Completed
Push — master ( 13ce84...26c17f )
by Mārtiņš
02:10
created

Search   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 180
Duplicated Lines 47.78 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
lcom 1
cbo 7
dl 86
loc 180
c 2
b 0
f 0
ccs 63
cts 63
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A findIdentityById() 20 20 2
A findEmailIdentityByEmailAddress() 20 20 2
A findEmailIdentityByToken() 23 23 2
A findIdentitiesByAccountId() 0 8 1
A findIdentitiesByParentId() 0 7 1
A findCookieIdentity() 23 23 2
A fetchIdentitiesWithStatus() 0 9 1

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 indentities 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 10
    public function __construct(CanCreateMapper $mapperFactory, LoggerInterface $logger)
27
    {
28 10
        $this->mapperFactory = $mapperFactory;
29 10
        $this->logger = $logger;
30 10
    }
31
32
33
    /**
34
     * Locates identity based on ID
35
     *
36
     * @param int $identityId
37
     *
38
     * @throws Palladium\Exception\IdentityNotFound if identity was not found
39
     *
40
     * @return Palladium\Entity\Identity
41
     */
42 2 View Code Duplication
    public function findIdentityById($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...
43
    {
44 2
        $identity = new Entity\Identity;
45 2
        $identity->setId($identityId);
46
47 2
        $mapper = $this->mapperFactory->create(Mapper\Identity::class);
48 2
        $mapper->fetch($identity);
49
50 2
        if ($identity->getAccountId() === null) {
51 1
            $this->logger->notice('identity not found', [
52
                'input' => [
53 1
                    'id' => $identityId,
54
                ],
55
            ]);
56
57 1
            throw new IdentityNotFound;
58
        }
59
60 1
        return $identity;
61
    }
62
63
64
    /**
65
     * Locates identity based on email address
66
     *
67
     * @param string $emailAddress
68
     *
69
     * @throws Palladium\Exception\IdentityNotFound if identity was not found
70
     *
71
     * @return Palladium\Entity\EmailIdentity
72
     */
73 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...
74
    {
75 2
        $identity = new Entity\EmailIdentity;
76 2
        $identity->setEmailAddress($emailAddress);
77
78 2
        $mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class);
79 2
        $mapper->fetch($identity);
80
81 2
        if ($identity->getId() === null) {
82 1
            $this->logger->notice('identity not found', [
83
                'input' => [
84 1
                    'email' => $emailAddress,
85
                ],
86
            ]);
87
88 1
            throw new IdentityNotFound;
89
        }
90
91 1
        return $identity;
92
    }
93
94
95
    /**
96
     * @param string $token
97
     * @param int $action
98
     *
99
     * @throws Palladium\Exception\IdentityNotFound if identity was not found
100
     *
101
     * @return Palladium\Entity\EmailIdentity
102
     */
103 2 View Code Duplication
    public function findEmailIdentityByToken(string $token, $action = Entity\Identity::ACTION_ANY)
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...
104
    {
105 2
        $identity = new Entity\EmailIdentity;
106
107 2
        $identity->setToken($token);
108 2
        $identity->setTokenAction($action);
109 2
        $identity->setTokenEndOfLife(time());
110
111 2
        $mapper = $this->mapperFactory->create(Mapper\Identity::class);
112 2
        $mapper->fetch($identity);
113
114 2
        if ($identity->getId() === null) {
115 1
            $this->logger->notice('identity not found', [
116
                'input' => [
117 1
                    'token' => $token,
118
                ],
119
            ]);
120
121 1
            throw new IdentityNotFound;
122
        }
123
124 1
        return $identity;
125
    }
126
127
128
    /**
129
     * @param int $accountId
130
     * @param string $series
131
     *
132
     * @throws Palladium\Exception\IdentityNotFound if identity was not found
133
     *
134
     * @return Palladium\Entity\CookieIdentity
135
     */
136 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...
137
    {
138 2
        $cookie = new Entity\CookieIdentity;
139 2
        $cookie->setStatus(Entity\Identity::STATUS_ACTIVE);
140 2
        $cookie->setAccountId($accountId);
141 2
        $cookie->setSeries($series);
142
143 2
        $mapper = $this->mapperFactory->create(Mapper\CookieIdentity::class);
144 2
        $mapper->fetch($cookie);
145
146 2
        if ($cookie->getId() === null) {
147 1
            $this->logger->notice('identity not found', [
148
                'input' => [
149 1
                    'account' => $cookie->getAccountId(),
150 1
                    'series' => $cookie->getSeries(),
151
                ],
152
            ]);
153
154 1
            throw new IdentityNotFound;
155
        }
156
157 1
        return $cookie;
158
    }
159
160
161
    /**
162
     * @return Palladium\Entity\IdentityCollection
163
     */
164 1
    public function findIdentitiesByAccountId($accountId, $type = Entity\Identity::TYPE_ANY, $status = Entity\Identity::STATUS_ACTIVE)
165
    {
166 1
        $collection = new Entity\IdentityCollection;
167 1
        $collection->forAccountId($accountId);
168 1
        $collection->forType($type);
169
170 1
        return $this->fetchIdentitiesWithStatus($collection, $status);
171
    }
172
173
174
    /**
175
     * @return Palladium\Entity\IdentityCollection
176
     */
177 1
    public function findIdentitiesByParentId($parentId, $status = Entity\Identity::STATUS_ACTIVE)
178
    {
179 1
        $collection = new Entity\IdentityCollection;
180 1
        $collection->forParentId($parentId);
181
182 1
        return $this->fetchIdentitiesWithStatus($collection, $status);
183
    }
184
185
186
    /**
187
     * @return Palladium\Entity\IdentityCollection
188
     */
189 2
    private function fetchIdentitiesWithStatus(Entity\IdentityCollection $collection, $status)
190
    {
191 2
        $collection->forStatus($status);
192
193 2
        $mapper = $this->mapperFactory->create(Mapper\IdentityCollection::class);
194 2
        $mapper->fetch($collection);
195
196 2
        return $collection;
197
    }
198
}
199