Completed
Push — master ( 25598f...82bcfc )
by Mārtiņš
02:18
created

Search::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
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
     * @param Palladium\Contract\CanCreateMapper $mapperFactory Factory for creating persistence layer structures
27
     * @param Psr\Log\LoggerInterface $logger PSR-3 compatible logger
28
     */
29 10
    public function __construct(CanCreateMapper $mapperFactory, LoggerInterface $logger)
30
    {
31 10
        $this->mapperFactory = $mapperFactory;
32 10
        $this->logger = $logger;
33 10
    }
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($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 View Code Duplication
    public function findOneTimeIdentityByNonce(string $nonce)
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...
99
    {
100
        $identity = new Entity\OneTimeIdentity;
101
        $identity->setNonce($nonce);
102
103
        $mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class);
104
        $mapper->fetch($identity);
105
106
        if ($identity->getId() === null) {
107
            $this->logger->notice('identity not found', [
108
                'input' => [
109
                    'nonce' => $nonce,
110
                ],
111
            ]);
112
113
            throw new IdentityNotFound;
114
        }
115
116
        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_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...
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 $accountId
155
     * @param string $series
156
     *
157
     * @throws Palladium\Exception\IdentityNotFound if identity was not found
158
     *
159
     * @return Palladium\Entity\CookieIdentity
160
     */
161 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...
162
    {
163 2
        $cookie = new Entity\CookieIdentity;
164 2
        $cookie->setStatus(Entity\Identity::STATUS_ACTIVE);
165 2
        $cookie->setAccountId($accountId);
166 2
        $cookie->setSeries($series);
167
168 2
        $mapper = $this->mapperFactory->create(Mapper\CookieIdentity::class);
169 2
        $mapper->fetch($cookie);
170
171 2
        if ($cookie->getId() === null) {
172 1
            $this->logger->notice('identity not found', [
173
                'input' => [
174 1
                    'account' => $cookie->getAccountId(),
175 1
                    'series' => $cookie->getSeries(),
176
                ],
177
            ]);
178
179 1
            throw new IdentityNotFound;
180
        }
181
182 1
        return $cookie;
183
    }
184
185
186
    /**
187
     * @return Palladium\Entity\IdentityCollection
188
     */
189 1
    public function findIdentitiesByAccountId($accountId, $type = Entity\Identity::TYPE_ANY, $status = Entity\Identity::STATUS_ACTIVE)
190
    {
191 1
        $collection = new Entity\IdentityCollection;
192 1
        $collection->forAccountId($accountId);
193 1
        $collection->forType($type);
194
195 1
        return $this->fetchIdentitiesWithStatus($collection, $status);
196
    }
197
198
199
    /**
200
     * @return Palladium\Entity\IdentityCollection
201
     */
202 1
    public function findIdentitiesByParentId($parentId, $status = Entity\Identity::STATUS_ACTIVE)
203
    {
204 1
        $collection = new Entity\IdentityCollection;
205 1
        $collection->forParentId($parentId);
206
207 1
        return $this->fetchIdentitiesWithStatus($collection, $status);
208
    }
209
210
211
    /**
212
     * @return Palladium\Entity\IdentityCollection
213
     */
214 2
    private function fetchIdentitiesWithStatus(Entity\IdentityCollection $collection, $status)
215
    {
216 2
        $collection->forStatus($status);
217
218 2
        $mapper = $this->mapperFactory->create(Mapper\IdentityCollection::class);
219 2
        $mapper->fetch($collection);
220
221 2
        return $collection;
222
    }
223
}
224