Skip to content

Commit 397f98c

Browse files
committed
Improve method signatures in authentication class
1 parent 193d4a0 commit 397f98c

File tree

2 files changed

+54
-99
lines changed

2 files changed

+54
-99
lines changed

src/Authentication.php

+32-61
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* (c) A51 doo <[email protected]>. All rights reserved.
77
*/
88

9+
declare(strict_types=1);
10+
911
namespace ActiveCollab\Authentication;
1012

1113
use ActiveCollab\Authentication\Adapter\AdapterInterface;
@@ -24,25 +26,22 @@
2426
use Psr\Http\Message\ServerRequestInterface;
2527
use Psr\Http\Server\RequestHandlerInterface;
2628

27-
/**
28-
* @package ActiveCollab\Authentication
29-
*/
3029
class Authentication implements AuthenticationInterface
3130
{
3231
/**
33-
* @var array
32+
* @var AdapterInterface[]
3433
*/
3534
private $adapters;
3635

3736
/**
3837
* Authenticated user instance.
3938
*
40-
* @var AuthenticatedUserInterface
39+
* @var AuthenticatedUserInterface|null
4140
*/
4241
private $authenticated_user;
4342

4443
/**
45-
* @var AuthenticationResultInterface
44+
* @var AuthenticationResultInterface|null
4645
*/
4746
private $authenticated_with;
4847

@@ -71,9 +70,6 @@ class Authentication implements AuthenticationInterface
7170
*/
7271
private $on_user_deauthenticated = [];
7372

74-
/**
75-
* @param array $adapters
76-
*/
7773
public function __construct(array $adapters)
7874
{
7975
foreach ($adapters as $adapter) {
@@ -118,7 +114,10 @@ public function __invoke(
118114
return $response;
119115
}
120116

121-
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
117+
public function process(
118+
ServerRequestInterface $request,
119+
RequestHandlerInterface $handler
120+
): ResponseInterface
122121
{
123122
$auth_result = $this->authenticatedUsingAdapters($request);
124123

@@ -150,10 +149,12 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
150149
return $response;
151150
}
152151

153-
/**
154-
* {@inheritdoc}
155-
*/
156-
public function authorize(AuthorizerInterface $authorizer, AdapterInterface $adapter, array $credentials, $payload = null)
152+
public function authorize(
153+
AuthorizerInterface $authorizer,
154+
AdapterInterface $adapter,
155+
array $credentials,
156+
$payload = null
157+
): TransportInterface
157158
{
158159
try {
159160
$user = $authorizer->verifyCredentials($credentials);
@@ -169,10 +170,10 @@ public function authorize(AuthorizerInterface $authorizer, AdapterInterface $ada
169170
}
170171
}
171172

172-
/**
173-
* {@inheritdoc}
174-
*/
175-
public function terminate(AdapterInterface $adapter, AuthenticationResultInterface $authenticated_with)
173+
public function terminate(
174+
AdapterInterface $adapter,
175+
AuthenticationResultInterface $authenticated_with
176+
): TransportInterface
176177
{
177178
$termination_result = $adapter->terminate($authenticated_with);
178179

@@ -184,17 +185,12 @@ public function terminate(AdapterInterface $adapter, AuthenticationResultInterfa
184185
/**
185186
* {@inheritdoc}
186187
*/
187-
public function getAdapters()
188+
public function getAdapters(): iterable
188189
{
189190
return $this->adapters;
190191
}
191192

192-
/**
193-
* @param ServerRequestInterface $request
194-
* @return TransportInterface|null
195-
* @throws Exception
196-
*/
197-
private function authenticatedUsingAdapters(ServerRequestInterface $request)
193+
private function authenticatedUsingAdapters(ServerRequestInterface $request): ?TransportInterface
198194
{
199195
$last_exception = null;
200196
$results = [];
@@ -227,18 +223,12 @@ private function authenticatedUsingAdapters(ServerRequestInterface $request)
227223
return $results[0];
228224
}
229225

230-
/**
231-
* {@inheritdoc}
232-
*/
233-
public function getAuthenticatedUser()
226+
public function getAuthenticatedUser(): ?AuthenticatedUserInterface
234227
{
235228
return $this->authenticated_user;
236229
}
237230

238-
/**
239-
* {@inheritdoc}
240-
*/
241-
public function setAuthenticatedUser(AuthenticatedUserInterface $user = null)
231+
public function setAuthenticatedUser(AuthenticatedUserInterface $user = null): AuthenticationInterface
242232
{
243233
$this->authenticated_user = $user;
244234

@@ -247,32 +237,19 @@ public function setAuthenticatedUser(AuthenticatedUserInterface $user = null)
247237
return $this;
248238
}
249239

250-
/**
251-
* @return AuthenticationResultInterface|null
252-
*/
253-
public function getAuthenticatedWith()
240+
public function getAuthenticatedWith(): ?AuthenticationResultInterface
254241
{
255242
return $this->authenticated_with;
256243
}
257244

258-
/**
259-
* {@inheritdoc}
260-
*/
261-
public function setAuthenticatedWith(AuthenticationResultInterface $value)
245+
public function setAuthenticatedWith(AuthenticationResultInterface $value): AuthenticationInterface
262246
{
263247
$this->authenticated_with = $value;
264248

265249
return $this;
266250
}
267251

268-
/**
269-
* Trigger an internal event.
270-
*
271-
* @param string $event_name
272-
* @param array $arguments
273-
* @return $this
274-
*/
275-
private function triggerEvent($event_name, ...$arguments)
252+
private function triggerEvent(string $event_name, ...$arguments): AuthenticationInterface
276253
{
277254
$property_name = "on_{$event_name}";
278255

@@ -284,41 +261,35 @@ private function triggerEvent($event_name, ...$arguments)
284261
return $this;
285262
}
286263

287-
/**
288-
* {@inheritdoc}
289-
*/
290-
public function onUserAuthenticated(callable $value)
264+
public function onUserAuthenticated(callable $value): AuthenticationInterface
291265
{
292266
$this->on_user_authenticated[] = $value;
293267

294268
return $this;
295269
}
296270

297-
public function onUserAuthorized(callable $value)
271+
public function onUserAuthorized(callable $value): AuthenticationInterface
298272
{
299273
$this->on_user_authorized[] = $value;
300274

301275
return $this;
302276
}
303277

304-
public function onUserAuthorizationFailed(callable $value)
278+
public function onUserAuthorizationFailed(callable $value): AuthenticationInterface
305279
{
306280
$this->on_user_authorization_failed[] = $value;
307281

308282
return $this;
309283
}
310284

311-
/**
312-
* {@inheritdoc}
313-
*/
314-
public function onUserSet(callable $value)
285+
public function onUserSet(callable $value): AuthenticationInterface
315286
{
316287
$this->on_user_set[] = $value;
317288

318289
return $this;
319290
}
320291

321-
public function onUserDeauthenticated(callable $value)
292+
public function onUserDeauthenticated(callable $value): AuthenticationInterface
322293
{
323294
$this->on_user_deauthenticated[] = $value;
324295

@@ -330,7 +301,7 @@ public function onUserDeauthenticated(callable $value)
330301
*
331302
* {@inheritdoc}
332303
*/
333-
public function setOnAuthenciatedUserChanged(callable $value = null)
304+
public function setOnAuthenciatedUserChanged(callable $value = null): AuthenticationInterface
334305
{
335306
if (empty($value)) {
336307
throw new InvalidArgumentException('Value needs to be a callable.');

src/AuthenticationInterface.php

+22-38
Original file line numberDiff line numberDiff line change
@@ -21,100 +21,84 @@
2121

2222
interface AuthenticationInterface extends MiddlewareInterface
2323
{
24-
/**
25-
* Authentication can be used as a PSR-7 middleware.
26-
*
27-
* @param ServerRequestInterface $request
28-
* @param ResponseInterface $response
29-
* @param callable|null $next
30-
* @return ResponseInterface
31-
*/
3224
public function __invoke(
3325
ServerRequestInterface $request,
3426
ResponseInterface $response,
3527
callable $next = null
3628
): ResponseInterface;
3729

38-
/**
39-
* Authorize and authenticate with given credentials against authorization/authentication source.
40-
*
41-
* @param AuthorizerInterface $authorizer
42-
* @param AdapterInterface $adapter
43-
* @param array $credentials
44-
* @param mixed $payload
45-
* @return TransportInterface
46-
*/
47-
public function authorize(AuthorizerInterface $authorizer, AdapterInterface $adapter, array $credentials, $payload = null);
30+
public function authorize(
31+
AuthorizerInterface $authorizer,
32+
AdapterInterface $adapter,
33+
array $credentials,
34+
$payload = null
35+
): TransportInterface;
4836

49-
/**
50-
* Deauthetnicate.
51-
*
52-
* @param AdapterInterface $adapter
53-
* @param AuthenticationResultInterface $authenticated_with
54-
* @return TransportInterface
55-
*/
56-
public function terminate(AdapterInterface $adapter, AuthenticationResultInterface $authenticated_with);
37+
public function terminate(
38+
AdapterInterface $adapter,
39+
AuthenticationResultInterface $authenticated_with
40+
): TransportInterface;
5741

5842
/**
59-
* @return AdapterInterface[]
43+
* @return AdapterInterface[]|iterable
6044
*/
61-
public function getAdapters();
45+
public function getAdapters(): iterable;
6246

6347
/**
6448
* Return authenticated in user.
6549
*
6650
* @return AuthenticatedUserInterface
6751
*/
68-
public function getAuthenticatedUser();
52+
public function getAuthenticatedUser(): ?AuthenticatedUserInterface;
6953

7054
/**
7155
* Override authentication adapter and force set logged user for this request.
7256
*
7357
* @param AuthenticatedUserInterface|null $user
7458
* @return $this
7559
*/
76-
public function setAuthenticatedUser(AuthenticatedUserInterface $user = null);
60+
public function setAuthenticatedUser(AuthenticatedUserInterface $user = null): AuthenticationInterface;
7761

7862
/**
7963
* @return AuthenticationResultInterface|null
8064
*/
81-
public function getAuthenticatedWith();
65+
public function getAuthenticatedWith(): ?AuthenticationResultInterface;
8266

8367
/**
8468
* @param AuthenticationResultInterface $value
8569
* @return $this
8670
*/
87-
public function setAuthenticatedWith(AuthenticationResultInterface $value);
71+
public function setAuthenticatedWith(AuthenticationResultInterface $value): AuthenticationInterface;
8872

8973
/**
9074
* @param callable $value
9175
* @return $this
9276
*/
93-
public function onUserAuthenticated(callable $value);
77+
public function onUserAuthenticated(callable $value): AuthenticationInterface;
9478

9579
/**
9680
* @param callable $value
9781
* @return $this
9882
*/
99-
public function onUserAuthorized(callable $value);
83+
public function onUserAuthorized(callable $value): AuthenticationInterface;
10084

10185
/**
10286
* @param callable $value
10387
* @return $this
10488
*/
105-
public function onUserAuthorizationFailed(callable $value);
89+
public function onUserAuthorizationFailed(callable $value): AuthenticationInterface;
10690

10791
/**
10892
* @param callable $value
10993
* @return $this
11094
*/
111-
public function onUserSet(callable $value);
95+
public function onUserSet(callable $value): AuthenticationInterface;
11296

11397
/**
11498
* @param callable $value
11599
* @return $this
116100
*/
117-
public function onUserDeauthenticated(callable $value);
101+
public function onUserDeauthenticated(callable $value): AuthenticationInterface;
118102

119103
/**
120104
* Use onUserSet() instead.
@@ -123,5 +107,5 @@ public function onUserDeauthenticated(callable $value);
123107
* @return $this
124108
* @deprecated
125109
*/
126-
public function setOnAuthenciatedUserChanged(callable $value = null);
110+
public function setOnAuthenciatedUserChanged(callable $value = null): AuthenticationInterface;
127111
}

0 commit comments

Comments
 (0)