diff --git a/CHANGELOG.md b/CHANGELOG.md index b5c1853..36fb6dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ - All authenticators now return an `Innmind\Immutable\Attempt` - All authenticators can return any type inside the `Attempt` - Requires `innmind/foundation:~1.9` +- `Innmind\HttpAuthentication\ViaAuthorization` constructor is private, use `::of()` instead +- `Innmind\HttpAuthentication\ViaBasicAuthorization` constructor is private, use `::of()` instead +- `Innmind\HttpAuthentication\ViaForm` constructor is private, use `::of()` instead +- `Innmind\HttpAuthentication\ViaUrlAuthority` constructor is private, use `::of()` instead ### Removed diff --git a/src/ViaAuthorization.php b/src/ViaAuthorization.php index dc3a4df..1a3a79c 100644 --- a/src/ViaAuthorization.php +++ b/src/ViaAuthorization.php @@ -14,15 +14,11 @@ */ final class ViaAuthorization { - /** @var callable(Authorization): Attempt */ - private $resolve; - /** - * @param callable(Authorization): Attempt $resolve + * @param \Closure(Authorization): Attempt $resolve */ - public function __construct(callable $resolve) + private function __construct(private \Closure $resolve) { - $this->resolve = $resolve; } /** @@ -36,4 +32,16 @@ public function __invoke(ServerRequest $request): Attempt ->attempt(static fn() => new \RuntimeException('Failed to resolve identity')) ->flatMap(fn($value) => ($this->resolve)($value)); } + + /** + * @template A + * + * @param callable(Authorization): Attempt $resolve + * + * @return self + */ + public static function of(callable $resolve): self + { + return new self(\Closure::fromCallable($resolve)); + } } diff --git a/src/ViaBasicAuthorization.php b/src/ViaBasicAuthorization.php index 7a8d75c..86631c7 100644 --- a/src/ViaBasicAuthorization.php +++ b/src/ViaBasicAuthorization.php @@ -14,15 +14,11 @@ */ final class ViaBasicAuthorization { - /** @var callable(string, string): Attempt */ - private $resolve; - /** - * @param callable(string, string): Attempt $resolve + * @param \Closure(string, string): Attempt $resolve */ - public function __construct(callable $resolve) + private function __construct(private \Closure $resolve) { - $this->resolve = $resolve; } /** @@ -47,4 +43,16 @@ public function __invoke(ServerRequest $request): Attempt return ($this->resolve)($user, $password); }); } + + /** + * @template A + * + * @param callable(string, string): Attempt $resolve + * + * @return self + */ + public static function of(callable $resolve): self + { + return new self(\Closure::fromCallable($resolve)); + } } diff --git a/src/ViaForm.php b/src/ViaForm.php index 002b4ee..e015c43 100644 --- a/src/ViaForm.php +++ b/src/ViaForm.php @@ -18,15 +18,11 @@ */ final class ViaForm { - /** @var callable(Form): Attempt */ - private $resolve; - /** - * @param callable(Form): Attempt $resolve + * @param \Closure(Form): Attempt $resolve */ - public function __construct(callable $resolve) + private function __construct(private \Closure $resolve) { - $this->resolve = $resolve; } /** @@ -39,4 +35,16 @@ public function __invoke(ServerRequest $request): Attempt ->attempt(static fn() => new \RuntimeException('Failed to resolve identity')) ->flatMap(fn($request) => ($this->resolve)($request->form())); } + + /** + * @template A + * + * @param callable(Form): Attempt $resolve + * + * @return self + */ + public static function of(callable $resolve): self + { + return new self(\Closure::fromCallable($resolve)); + } } diff --git a/src/ViaUrlAuthority.php b/src/ViaUrlAuthority.php index aecb7b4..da2cb4b 100644 --- a/src/ViaUrlAuthority.php +++ b/src/ViaUrlAuthority.php @@ -15,15 +15,11 @@ */ final class ViaUrlAuthority { - /** @var callable(User, Password): Attempt */ - private $resolve; - /** - * @param callable(User, Password): Attempt $resolve + * @param \Closure(User, Password): Attempt $resolve */ - public function __construct(callable $resolve) + private function __construct(private \Closure $resolve) { - $this->resolve = $resolve; } /** @@ -41,4 +37,16 @@ public function __invoke(ServerRequest $request): Attempt return ($this->resolve)($user, $password); } + + /** + * @template A + * + * @param callable(User, Password): Attempt $resolve + * + * @return self + */ + public static function of(callable $resolve): self + { + return new self(\Closure::fromCallable($resolve)); + } } diff --git a/tests/ViaAuthorizationTest.php b/tests/ViaAuthorizationTest.php index a892b7b..585e5d1 100644 --- a/tests/ViaAuthorizationTest.php +++ b/tests/ViaAuthorizationTest.php @@ -21,7 +21,7 @@ class ViaAuthorizationTest extends TestCase { public function testReturnNothingWhenNoAuthorizationHeader() { - $authenticate = new ViaAuthorization( + $authenticate = ViaAuthorization::of( static fn() => throw new \Exception, ); $request = ServerRequest::of( @@ -38,7 +38,7 @@ public function testReturnNothingWhenNoAuthorizationHeader() public function testReturnNothingWhenAuthorizationHeaderNotParsedCorrectly() { - $authenticate = new ViaAuthorization( + $authenticate = ViaAuthorization::of( static fn() => throw new \Exception, ); $request = ServerRequest::of( @@ -58,7 +58,7 @@ public function testReturnNothingWhenAuthorizationHeaderNotParsedCorrectly() public function testInvokation() { - $authenticate = new ViaAuthorization( + $authenticate = ViaAuthorization::of( static fn($value) => Attempt::result($value), ); $expected = Authorization::of('Bearer', 'foo'); diff --git a/tests/ViaBasicAuthorizationTest.php b/tests/ViaBasicAuthorizationTest.php index 884ddb8..9b53ece 100644 --- a/tests/ViaBasicAuthorizationTest.php +++ b/tests/ViaBasicAuthorizationTest.php @@ -21,7 +21,7 @@ class ViaBasicAuthorizationTest extends TestCase { public function testReturnNothingWhenNoAuthorizationHeader() { - $authenticate = new ViaBasicAuthorization( + $authenticate = ViaBasicAuthorization::of( static fn() => throw new \Exception, ); $request = ServerRequest::of( @@ -38,7 +38,7 @@ public function testReturnNothingWhenNoAuthorizationHeader() public function testReturnNothingWhenAuthorizationHeaderNotParsedCorrectly() { - $authenticate = new ViaBasicAuthorization( + $authenticate = ViaBasicAuthorization::of( static fn() => throw new \Exception, ); $request = ServerRequest::of( @@ -58,7 +58,7 @@ public function testReturnNothingWhenAuthorizationHeaderNotParsedCorrectly() public function testReturnNothingWhenNotBasicAuthorization() { - $authenticate = new ViaBasicAuthorization( + $authenticate = ViaBasicAuthorization::of( static fn() => throw new \Exception, ); $request = ServerRequest::of( @@ -78,7 +78,7 @@ public function testReturnNothingWhenNotBasicAuthorization() public function testInvokation() { - $authenticate = new ViaBasicAuthorization( + $authenticate = ViaBasicAuthorization::of( static fn($user, $password) => Attempt::result([$user, $password]), ); $request = ServerRequest::of( diff --git a/tests/ViaFormTest.php b/tests/ViaFormTest.php index 76f104c..9aa11ed 100644 --- a/tests/ViaFormTest.php +++ b/tests/ViaFormTest.php @@ -17,7 +17,7 @@ class ViaFormTest extends TestCase { public function testReturnNothingWhenNotPostRequest() { - $authenticate = new ViaForm( + $authenticate = ViaForm::of( static fn() => throw new \Exception, ); $request = ServerRequest::of( @@ -34,7 +34,7 @@ public function testReturnNothingWhenNotPostRequest() public function testInvokation() { - $authenticate = new ViaForm( + $authenticate = ViaForm::of( static fn($value) => Attempt::result($value), ); $request = ServerRequest::of( diff --git a/tests/ViaUrlAuthorityTest.php b/tests/ViaUrlAuthorityTest.php index efcd1a4..c29f232 100644 --- a/tests/ViaUrlAuthorityTest.php +++ b/tests/ViaUrlAuthorityTest.php @@ -17,7 +17,7 @@ class ViaUrlAuthorityTest extends TestCase { public function testReturnNothingWhenNoUserProvidedInTheUrl() { - $authenticate = new ViaUrlAuthority( + $authenticate = ViaUrlAuthority::of( static fn() => throw new \Exception, ); $url = Url::of('https://localhost/'); @@ -35,7 +35,7 @@ public function testReturnNothingWhenNoUserProvidedInTheUrl() public function testInvokation() { - $authenticate = new ViaUrlAuthority( + $authenticate = ViaUrlAuthority::of( static fn($user, $password) => Attempt::result([$user, $password]), ); $url = Url::of('https://user:password@localhost/');