Completed
Push — master ( 8b4cd1...6cce77 )
by Mārtiņš
04:56
created

Search   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 161
Duplicated Lines 24.84 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 40
loc 161
ccs 58
cts 58
cp 1
rs 10
c 2
b 0
f 0

8 Methods

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