Completed
Push — master ( 4639e9...e17532 )
by Mārtiņš
02:47
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 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 $emailAddress
66
     *
67
     * @return Palladium\Entity\EmailIdentity
68
     */
69 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...
70
    {
71 2
        $identity = new Entity\EmailIdentity;
72 2
        $identity->setIdentifier($emailAddress);
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' => $emailAddress,
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(string $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