1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Passport\Repositories; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2019 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use DateTimeImmutable; |
22
|
|
|
use Limoncello\Passport\Contracts\Entities\ClientInterface; |
23
|
|
|
use Limoncello\Passport\Contracts\Entities\ScopeInterface; |
24
|
|
|
use Limoncello\Passport\Contracts\Repositories\ClientRepositoryInterface; |
25
|
|
|
use Limoncello\Passport\Exceptions\RepositoryException; |
26
|
|
|
use function assert; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @package Limoncello\Passport |
30
|
|
|
*/ |
31
|
|
|
abstract class ClientRepository extends BaseRepository implements ClientRepositoryInterface |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
2 |
|
public function index(): array |
37
|
|
|
{ |
38
|
2 |
|
return parent::indexResources(); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
* |
44
|
|
|
* @throws RepositoryException |
45
|
|
|
* |
46
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
47
|
|
|
*/ |
48
|
25 |
|
public function create(ClientInterface $client): ClientInterface |
49
|
|
|
{ |
50
|
|
|
try { |
51
|
|
|
$now = $this->ignoreException(function (): DateTimeImmutable { |
52
|
25 |
|
return new DateTimeImmutable(); |
53
|
25 |
|
}); |
54
|
25 |
|
$schema = $this->getDatabaseSchema(); |
55
|
|
|
$values = [ |
56
|
25 |
|
$schema->getClientsIdentityColumn() => $client->getIdentifier(), |
57
|
25 |
|
$schema->getClientsNameColumn() => $client->getName(), |
58
|
25 |
|
$schema->getClientsDescriptionColumn() => $client->getDescription(), |
59
|
25 |
|
$schema->getClientsCredentialsColumn() => $client->getCredentials(), |
60
|
25 |
|
$schema->getClientsIsConfidentialColumn() => $client->isConfidential(), |
61
|
25 |
|
$schema->getClientsIsScopeExcessAllowedColumn() => $client->isScopeExcessAllowed(), |
62
|
25 |
|
$schema->getClientsIsUseDefaultScopeColumn() => $client->isUseDefaultScopesOnEmptyRequest(), |
63
|
25 |
|
$schema->getClientsIsCodeGrantEnabledColumn() => $client->isCodeGrantEnabled(), |
64
|
25 |
|
$schema->getClientsIsImplicitGrantEnabledColumn() => $client->isImplicitGrantEnabled(), |
65
|
25 |
|
$schema->getClientsIsPasswordGrantEnabledColumn() => $client->isPasswordGrantEnabled(), |
66
|
25 |
|
$schema->getClientsIsClientGrantEnabledColumn() => $client->isClientGrantEnabled(), |
67
|
25 |
|
$schema->getClientsIsRefreshGrantEnabledColumn() => $client->isRefreshGrantEnabled(), |
68
|
25 |
|
$schema->getClientsCreatedAtColumn() => $now, |
69
|
|
|
]; |
70
|
|
|
|
71
|
25 |
|
$identifier = $client->getIdentifier(); |
72
|
25 |
|
if (empty($scopeIdentifiers = $client->getScopeIdentifiers()) === true) { |
73
|
25 |
|
$this->createResource($values); |
|
|
|
|
74
|
|
|
} else { |
75
|
|
|
$this->inTransaction(function () use ($identifier, $values, $scopeIdentifiers) { |
76
|
1 |
|
$this->createResource($values); |
|
|
|
|
77
|
1 |
|
$this->bindScopeIdentifiers($identifier, $scopeIdentifiers); |
|
|
|
|
78
|
1 |
|
}); |
79
|
|
|
} |
80
|
24 |
|
$client->setCreatedAt($now); |
81
|
|
|
|
82
|
24 |
|
return $client; |
83
|
1 |
|
} catch (RepositoryException $exception) { |
84
|
1 |
|
$message = 'Client creation failed.'; |
85
|
1 |
|
throw new RepositoryException($message, 0, $exception); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritdoc |
91
|
|
|
* |
92
|
|
|
* @throws RepositoryException |
93
|
|
|
*/ |
94
|
17 |
|
public function bindScopes(string $identifier, iterable $scopes): void |
95
|
|
|
{ |
96
|
|
|
$getIdentifiers = function (iterable $scopes): iterable { |
97
|
17 |
|
foreach ($scopes as $scope) { |
98
|
|
|
/** @var ScopeInterface $scope */ |
99
|
17 |
|
assert($scope instanceof ScopeInterface); |
100
|
17 |
|
yield $scope->getIdentifier(); |
101
|
|
|
} |
102
|
17 |
|
}; |
103
|
|
|
|
104
|
17 |
|
$this->bindScopeIdentifiers($identifier, $getIdentifiers($scopes)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $identifier |
109
|
|
|
* @param iterable $scopeIdentifiers |
110
|
|
|
* |
111
|
|
|
* @return void |
112
|
|
|
* |
113
|
|
|
* @throws RepositoryException |
114
|
|
|
*/ |
115
|
18 |
|
public function bindScopeIdentifiers(string $identifier, iterable $scopeIdentifiers): void |
116
|
|
|
{ |
117
|
|
|
try { |
118
|
18 |
|
$schema = $this->getDatabaseSchema(); |
119
|
18 |
|
$this->createBelongsToManyRelationship( |
120
|
18 |
|
$identifier, |
121
|
18 |
|
$scopeIdentifiers, |
122
|
18 |
|
$schema->getClientsScopesTable(), |
123
|
18 |
|
$schema->getClientsScopesClientIdentityColumn(), |
124
|
18 |
|
$schema->getClientsScopesScopeIdentityColumn() |
125
|
|
|
); |
126
|
1 |
|
} catch (RepositoryException $exception) { |
127
|
1 |
|
$message = 'Binding client scopes failed.'; |
128
|
1 |
|
throw new RepositoryException($message, 0, $exception); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @inheritdoc |
134
|
|
|
* |
135
|
|
|
* @throws RepositoryException |
136
|
|
|
*/ |
137
|
2 |
|
public function unbindScopes(string $identifier): void |
138
|
|
|
{ |
139
|
|
|
try { |
140
|
2 |
|
$schema = $this->getDatabaseSchema(); |
141
|
2 |
|
$this->deleteBelongsToManyRelationshipIdentifiers( |
142
|
2 |
|
$schema->getClientsScopesTable(), |
143
|
2 |
|
$schema->getClientsScopesClientIdentityColumn(), |
144
|
2 |
|
$identifier |
145
|
|
|
); |
146
|
1 |
|
} catch (RepositoryException $exception) { |
147
|
1 |
|
$message = 'Unbinding client scopes failed.'; |
148
|
1 |
|
throw new RepositoryException($message, 0, $exception); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @inheritdoc |
154
|
|
|
* |
155
|
|
|
* @throws RepositoryException |
156
|
|
|
*/ |
157
|
18 |
|
public function read(string $identifier): ?ClientInterface |
158
|
|
|
{ |
159
|
|
|
try { |
160
|
18 |
|
return $this->readResource($identifier); |
161
|
1 |
|
} catch (RepositoryException $exception) { |
162
|
1 |
|
$message = 'Reading client failed.'; |
163
|
1 |
|
throw new RepositoryException($message, 0, $exception); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @inheritdoc |
169
|
|
|
* |
170
|
|
|
* @throws RepositoryException |
171
|
|
|
*/ |
172
|
16 |
|
public function readScopeIdentifiers(string $identifier): array |
173
|
|
|
{ |
174
|
|
|
try { |
175
|
16 |
|
$schema = $this->getDatabaseSchema(); |
176
|
16 |
|
return $this->readBelongsToManyRelationshipIdentifiers( |
177
|
16 |
|
$identifier, |
178
|
16 |
|
$schema->getClientsScopesTable(), |
179
|
16 |
|
$schema->getClientsScopesClientIdentityColumn(), |
180
|
16 |
|
$schema->getClientsScopesScopeIdentityColumn() |
181
|
|
|
); |
182
|
1 |
|
} catch (RepositoryException $exception) { |
183
|
1 |
|
$message = 'Reading client scope identifiers failed.'; |
184
|
1 |
|
throw new RepositoryException($message, 0, $exception); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @inheritdoc |
190
|
|
|
* |
191
|
|
|
* @throws RepositoryException |
192
|
|
|
*/ |
193
|
16 |
|
public function readRedirectUriStrings(string $identifier): array |
194
|
|
|
{ |
195
|
|
|
try { |
196
|
16 |
|
$schema = $this->getDatabaseSchema(); |
197
|
16 |
|
return $this->readHasManyRelationshipColumn( |
198
|
16 |
|
$identifier, |
199
|
16 |
|
$schema->getRedirectUrisTable(), |
200
|
16 |
|
$schema->getRedirectUrisValueColumn(), |
201
|
16 |
|
$schema->getRedirectUrisClientIdentityColumn() |
202
|
|
|
); |
203
|
1 |
|
} catch (RepositoryException $exception) { |
204
|
1 |
|
$message = 'Reading client redirect URIs failed.'; |
205
|
1 |
|
throw new RepositoryException($message, 0, $exception); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @inheritdoc |
211
|
|
|
*/ |
212
|
3 |
|
public function update(ClientInterface $client): void |
213
|
|
|
{ |
214
|
|
|
try { |
215
|
|
|
$now = $this->ignoreException(function (): DateTimeImmutable { |
216
|
3 |
|
return new DateTimeImmutable(); |
217
|
3 |
|
}); |
218
|
3 |
|
$schema = $this->getDatabaseSchema(); |
219
|
3 |
|
$this->updateResource($client->getIdentifier(), [ |
220
|
3 |
|
$schema->getClientsNameColumn() => $client->getName(), |
221
|
3 |
|
$schema->getClientsDescriptionColumn() => $client->getDescription(), |
222
|
3 |
|
$schema->getClientsCredentialsColumn() => $client->getCredentials(), |
223
|
3 |
|
$schema->getClientsIsConfidentialColumn() => $client->isConfidential(), |
224
|
3 |
|
$schema->getClientsIsScopeExcessAllowedColumn() => $client->isScopeExcessAllowed(), |
225
|
3 |
|
$schema->getClientsIsUseDefaultScopeColumn() => $client->isUseDefaultScopesOnEmptyRequest(), |
226
|
3 |
|
$schema->getClientsIsCodeGrantEnabledColumn() => $client->isCodeGrantEnabled(), |
227
|
3 |
|
$schema->getClientsIsImplicitGrantEnabledColumn() => $client->isImplicitGrantEnabled(), |
228
|
3 |
|
$schema->getClientsIsPasswordGrantEnabledColumn() => $client->isPasswordGrantEnabled(), |
229
|
3 |
|
$schema->getClientsIsClientGrantEnabledColumn() => $client->isClientGrantEnabled(), |
230
|
3 |
|
$schema->getClientsUpdatedAtColumn() => $now, |
231
|
|
|
]); |
232
|
2 |
|
$client->setUpdatedAt($now); |
233
|
1 |
|
} catch (RepositoryException $exception) { |
234
|
1 |
|
$message = 'Client update failed.'; |
235
|
1 |
|
throw new RepositoryException($message, 0, $exception); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @inheritdoc |
241
|
|
|
* |
242
|
|
|
* @throws RepositoryException |
243
|
|
|
*/ |
244
|
2 |
|
public function delete(string $identifier): void |
245
|
|
|
{ |
246
|
|
|
try { |
247
|
2 |
|
$this->deleteResource($identifier); |
248
|
1 |
|
} catch (RepositoryException $exception) { |
249
|
1 |
|
$message = 'Client deletion failed.'; |
250
|
1 |
|
throw new RepositoryException($message, 0, $exception); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @inheritdoc |
256
|
|
|
*/ |
257
|
29 |
|
protected function getTableNameForWriting(): string |
258
|
|
|
{ |
259
|
29 |
|
return $this->getDatabaseSchema()->getClientsTable(); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @inheritdoc |
264
|
|
|
*/ |
265
|
20 |
|
protected function getPrimaryKeyName(): string |
266
|
|
|
{ |
267
|
20 |
|
return $this->getDatabaseSchema()->getClientsIdentityColumn(); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.