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\IdentityNotFound; |
12
|
|
|
use Palladium\Repository\Identity as Repository; |
13
|
|
|
use Psr\Log\LoggerInterface; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class Search |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
private $repository; |
20
|
|
|
private $logger; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param Repository $repository Repository for abstracting persistence layer structures |
24
|
|
|
* @param LoggerInterface $logger PSR-3 compatible logger |
25
|
|
|
*/ |
26
|
14 |
|
public function __construct(Repository $repository, LoggerInterface $logger) |
27
|
|
|
{ |
28
|
14 |
|
$this->repository = $repository; |
29
|
14 |
|
$this->logger = $logger; |
30
|
14 |
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Locates identity based on ID |
35
|
|
|
* |
36
|
|
|
* @throws IdentityNotFound if identity was not found |
37
|
|
|
*/ |
38
|
2 |
|
public function findIdentityById(int $identityId): Entity\Identity |
39
|
|
|
{ |
40
|
2 |
|
$identity = new Entity\Identity; |
41
|
2 |
|
$identity->setId($identityId); |
42
|
|
|
|
43
|
2 |
|
$this->repository->load($identity, Entity\Identity::class); |
44
|
|
|
|
45
|
2 |
|
if ($identity->getAccountId() === null) { |
46
|
1 |
|
$this->logger->notice('identity not found', [ |
47
|
|
|
'input' => [ |
48
|
1 |
|
'id' => $identityId, |
49
|
|
|
], |
50
|
|
|
]); |
51
|
|
|
|
52
|
1 |
|
throw new IdentityNotFound; |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
return $identity; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Locates identity based on email address |
61
|
|
|
* |
62
|
|
|
* @throws IdentityNotFound if identity was not found |
63
|
|
|
*/ |
64
|
2 |
|
public function findStandardIdentityByIdentifier(string $identifier): Entity\StandardIdentity |
65
|
|
|
{ |
66
|
2 |
|
$identity = new Entity\StandardIdentity; |
67
|
2 |
|
$identity->setIdentifier($identifier); |
68
|
|
|
|
69
|
2 |
|
$this->repository->load($identity); |
70
|
|
|
|
71
|
2 |
|
if ($identity->getId() === null) { |
72
|
1 |
|
$this->logger->notice('identity not found', [ |
73
|
|
|
'input' => [ |
74
|
1 |
|
'identifier' => $identifier, |
75
|
|
|
], |
76
|
|
|
]); |
77
|
|
|
|
78
|
1 |
|
throw new IdentityNotFound; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
return $identity; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
2 |
|
public function findNonceIdentityByIdentifier(string $identifier): Entity\NonceIdentity |
86
|
|
|
{ |
87
|
2 |
|
$identity = new Entity\NonceIdentity; |
88
|
2 |
|
$identity->setIdentifier($identifier); |
89
|
|
|
|
90
|
2 |
|
$this->repository->load($identity); |
91
|
|
|
|
92
|
2 |
|
if ($identity->getId() === null) { |
93
|
1 |
|
$this->logger->notice('identity not found', [ |
94
|
|
|
'input' => [ |
95
|
1 |
|
'identifier' => $identifier, |
96
|
|
|
], |
97
|
|
|
]); |
98
|
|
|
|
99
|
1 |
|
throw new IdentityNotFound; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
return $identity; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @throws IdentityNotFound if identity was not found |
108
|
|
|
*/ |
109
|
2 |
|
public function findStandardIdentityByToken(string $token, int $action = Entity\Identity::ACTION_NONE): Entity\StandardIdentity |
110
|
|
|
{ |
111
|
2 |
|
$entry = $this->findIdentityByToken($token, $action); |
|
|
|
|
112
|
|
|
|
113
|
2 |
|
if ($entry->getId() === null) { |
114
|
1 |
|
$this->logger->notice('identity not found', [ |
115
|
|
|
'input' => [ |
116
|
1 |
|
'token' => $token, |
117
|
|
|
], |
118
|
|
|
]); |
119
|
|
|
|
120
|
1 |
|
throw new IdentityNotFound; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
1 |
|
$identity = new Entity\StandardIdentity; |
125
|
1 |
|
$identity->setId($entry->getId()); |
126
|
|
|
|
127
|
1 |
|
$this->repository->load($identity); |
128
|
|
|
|
129
|
1 |
|
return $identity; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @throws IdentityNotFound if identity was not found |
135
|
|
|
*/ |
136
|
2 |
|
public function findIdentityByToken(string $token, int $action = Entity\Identity::ACTION_NONE): Entity\Identity |
137
|
|
|
{ |
138
|
2 |
|
$identity = new Entity\Identity; |
139
|
|
|
|
140
|
2 |
|
$identity->setToken($token); |
141
|
2 |
|
$identity->setTokenAction($action); |
142
|
2 |
|
$identity->setTokenEndOfLife(time()); |
143
|
|
|
|
144
|
2 |
|
$this->repository->load($identity); |
145
|
|
|
|
146
|
2 |
|
return $identity; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @throws IdentityNotFound if identity was not found |
151
|
|
|
*/ |
152
|
2 |
|
public function findStandardIdentityById(int $identityId): Entity\StandardIdentity |
153
|
|
|
{ |
154
|
2 |
|
$identity = new Entity\StandardIdentity; |
155
|
2 |
|
$identity->setId($identityId); |
156
|
|
|
|
157
|
2 |
|
$this->repository->load($identity); |
158
|
|
|
|
159
|
2 |
|
if ($identity->getAccountId() === null) { |
160
|
1 |
|
$this->logger->notice('identity not found', [ |
161
|
|
|
'input' => [ |
162
|
1 |
|
'id' => $identityId, |
163
|
|
|
], |
164
|
|
|
]); |
165
|
|
|
|
166
|
1 |
|
throw new IdentityNotFound; |
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
return $identity; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @throws IdentityNotFound if identity was not found |
175
|
|
|
*/ |
176
|
2 |
|
public function findCookieIdentity(int $accountId, string $series): Entity\CookieIdentity |
177
|
|
|
{ |
178
|
2 |
|
$cookie = new Entity\CookieIdentity; |
179
|
2 |
|
$cookie->setStatus(Entity\Identity::STATUS_ACTIVE); |
180
|
2 |
|
$cookie->setAccountId($accountId); |
181
|
2 |
|
$cookie->setSeries($series); |
182
|
|
|
|
183
|
2 |
|
$this->repository->load($cookie); |
184
|
|
|
|
185
|
2 |
|
if ($cookie->getId() === null) { |
186
|
1 |
|
$this->logger->notice('identity not found', [ |
187
|
|
|
'input' => [ |
188
|
1 |
|
'account' => $cookie->getAccountId(), |
189
|
1 |
|
'series' => $cookie->getSeries(), |
190
|
|
|
], |
191
|
|
|
]); |
192
|
|
|
|
193
|
1 |
|
throw new IdentityNotFound; |
194
|
|
|
} |
195
|
|
|
|
196
|
1 |
|
return $cookie; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
|
200
|
1 |
|
public function findIdentitiesByAccountId(int $accountId, int $type = Entity\Identity::TYPE_ANY, int $status = Entity\Identity::STATUS_ACTIVE): Entity\IdentityCollection |
201
|
|
|
{ |
202
|
1 |
|
$collection = new Entity\IdentityCollection; |
203
|
1 |
|
$collection->forAccountId($accountId); |
204
|
1 |
|
$collection->forType($type); |
205
|
|
|
|
206
|
1 |
|
return $this->fetchIdentitiesWithStatus($collection, $status); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
|
210
|
1 |
|
public function findIdentitiesByParentId(int $parentId, int $status = Entity\Identity::STATUS_ACTIVE): Entity\IdentityCollection |
211
|
|
|
{ |
212
|
1 |
|
$collection = new Entity\IdentityCollection; |
213
|
1 |
|
$collection->forParentId($parentId); |
214
|
|
|
|
215
|
1 |
|
return $this->fetchIdentitiesWithStatus($collection, $status); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
|
219
|
2 |
|
private function fetchIdentitiesWithStatus(Entity\IdentityCollection $collection, int $status): Entity\IdentityCollection |
220
|
|
|
{ |
221
|
2 |
|
$collection->forStatus($status); |
222
|
2 |
|
$this->repository->load($collection); |
223
|
|
|
|
224
|
2 |
|
return $collection; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|