Skip to content

Commit 284a026

Browse files
committed
Remove deprecated method, and improve code style
1 parent e60458c commit 284a026

File tree

3 files changed

+15
-90
lines changed

3 files changed

+15
-90
lines changed

src/Authentication.php

Lines changed: 12 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -20,64 +20,23 @@
2020
use ActiveCollab\Authentication\Authorizer\AuthorizerInterface;
2121
use ActiveCollab\Authentication\Exception\InvalidAuthenticationRequestException;
2222
use Exception;
23-
use InvalidArgumentException;
24-
use LogicException;
2523
use Psr\Http\Message\ResponseInterface;
2624
use Psr\Http\Message\ServerRequestInterface;
2725
use Psr\Http\Server\RequestHandlerInterface;
2826

2927
class Authentication implements AuthenticationInterface
3028
{
31-
/**
32-
* @var AdapterInterface[]
33-
*/
34-
private $adapters;
35-
36-
/**
37-
* Authenticated user instance.
38-
*
39-
* @var AuthenticatedUserInterface|null
40-
*/
41-
private $authenticated_user;
42-
43-
/**
44-
* @var AuthenticationResultInterface|null
45-
*/
46-
private $authenticated_with;
47-
48-
/**
49-
* @var callable[]
50-
*/
51-
private $on_user_authenticated = [];
52-
53-
/**
54-
* @var callable[]
55-
*/
56-
private $on_user_authorized = [];
57-
58-
/**
59-
* @var callable[]
60-
*/
61-
private $on_user_authorization_failed = [];
62-
63-
/**
64-
* @var callable[]
65-
*/
66-
private $on_user_set = [];
67-
68-
/**
69-
* @var callable[]
70-
*/
71-
private $on_user_deauthenticated = [];
29+
private array $adapters;
30+
private ?AuthenticatedUserInterface $authenticated_user = null;
31+
private ?AuthenticationResultInterface $authenticated_with = null;
32+
private array $on_user_authenticated = [];
33+
private array $on_user_authorized = [];
34+
private array $on_user_authorization_failed = [];
35+
private array $on_user_set = [];
36+
private array $on_user_deauthenticated = [];
7237

7338
public function __construct(AdapterInterface ...$adapters)
7439
{
75-
foreach ($adapters as $adapter) {
76-
if (!($adapter instanceof AdapterInterface)) {
77-
throw new LogicException('Invalid authentication adapter provided');
78-
}
79-
}
80-
8140
$this->adapters = $adapters;
8241
}
8342

@@ -114,7 +73,7 @@ public function __invoke(
11473
return $response;
11574
}
11675

117-
public $lastProcessingResult;
76+
public ?TransportInterface $lastProcessingResult = null;
11877

11978
public function process(
12079
ServerRequestInterface $request,
@@ -204,7 +163,6 @@ private function authenticatedUsingAdapters(ServerRequestInterface $request): ?T
204163
$last_exception = null;
205164
$results = [];
206165

207-
/** @var AdapterInterface $adapter */
208166
foreach ($this->adapters as $adapter) {
209167
try {
210168
$initialization_result = $adapter->initialize($request);
@@ -237,7 +195,7 @@ public function getAuthenticatedUser(): ?AuthenticatedUserInterface
237195
return $this->authenticated_user;
238196
}
239197

240-
public function setAuthenticatedUser(AuthenticatedUserInterface $user = null): AuthenticationInterface
198+
public function setAuthenticatedUser(?AuthenticatedUserInterface $user): AuthenticationInterface
241199
{
242200
$this->authenticated_user = $user;
243201

@@ -258,16 +216,14 @@ public function setAuthenticatedWith(?AuthenticationResultInterface $value): Aut
258216
return $this;
259217
}
260218

261-
private function triggerEvent(string $event_name, ...$arguments): AuthenticationInterface
219+
private function triggerEvent(string $event_name, ...$arguments): void
262220
{
263-
$property_name = "on_{$event_name}";
221+
$property_name = sprintf("on_%s", $event_name);
264222

265223
/** @var callable $handler */
266224
foreach ($this->$property_name as $handler) {
267225
call_user_func_array($handler, $arguments);
268226
}
269-
270-
return $this;
271227
}
272228

273229
public function onUserAuthenticated(callable $value): AuthenticationInterface
@@ -304,18 +260,4 @@ public function onUserDeauthenticated(callable $value): AuthenticationInterface
304260

305261
return $this;
306262
}
307-
308-
/**
309-
* Kept for backward compatibility reasons. Will be removed.
310-
*
311-
* {@inheritdoc}
312-
*/
313-
public function setOnAuthenciatedUserChanged(callable $value = null): AuthenticationInterface
314-
{
315-
if (empty($value)) {
316-
throw new InvalidArgumentException('Value needs to be a callable.');
317-
}
318-
319-
return $this->onUserSet($value);
320-
}
321263
}

src/AuthenticationInterface.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ public function terminate(
4040
): TransportInterface;
4141

4242
/**
43-
* @return AdapterInterface[]|iterable
43+
* @return AdapterInterface[]
4444
*/
4545
public function getAdapters(): iterable;
4646

4747
public function getAuthenticatedUser(): ?AuthenticatedUserInterface;
48-
public function setAuthenticatedUser(AuthenticatedUserInterface $user = null): AuthenticationInterface;
48+
public function setAuthenticatedUser(?AuthenticatedUserInterface $user): AuthenticationInterface;
4949

5050
public function getAuthenticatedWith(): ?AuthenticationResultInterface;
5151
public function setAuthenticatedWith(?AuthenticationResultInterface $value): AuthenticationInterface;
@@ -55,13 +55,4 @@ public function onUserAuthorized(callable $value): AuthenticationInterface;
5555
public function onUserAuthorizationFailed(callable $value): AuthenticationInterface;
5656
public function onUserSet(callable $value): AuthenticationInterface;
5757
public function onUserDeauthenticated(callable $value): AuthenticationInterface;
58-
59-
/**
60-
* Use onUserSet() instead.
61-
*
62-
* @param callable|null $value
63-
* @return $this
64-
* @deprecated
65-
*/
66-
public function setOnAuthenciatedUserChanged(callable $value = null): AuthenticationInterface;
6758
}

test/src/EventsTest.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function testOnUserSet()
132132
});
133133

134134
$deprecated_callback_called = false;
135-
$middleware->setOnAuthenciatedUserChanged(function () use (&$deprecated_callback_called) {
135+
$middleware->onUserSet(function () use (&$deprecated_callback_called) {
136136
$this->validateOnUserSetArguments(func_get_args());
137137

138138
$deprecated_callback_called = true;
@@ -144,14 +144,6 @@ public function testOnUserSet()
144144
$this->assertTrue($deprecated_callback_called);
145145
}
146146

147-
public function testInvalidUserChangedMethodCall()
148-
{
149-
$this->expectException(InvalidArgumentException::class);
150-
$this->expectExceptionMessage("Value needs to be a callable.");
151-
152-
$this->prepareForAuthentication()->setOnAuthenciatedUserChanged();
153-
}
154-
155147
private function validateOnUserSetArguments(array $event_arguments)
156148
{
157149
$this->assertCount(1, $event_arguments);

0 commit comments

Comments
 (0)