Skip to content

Commit b0a3732

Browse files
authored
Merge pull request #11 from Innmind/private-constructors
Make constructors private
2 parents 6f42914 + 8e0a67d commit b0a3732

9 files changed

Lines changed: 71 additions & 35 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
- All authenticators now return an `Innmind\Immutable\Attempt`
88
- All authenticators can return any type inside the `Attempt`
99
- Requires `innmind/foundation:~1.9`
10+
- `Innmind\HttpAuthentication\ViaAuthorization` constructor is private, use `::of()` instead
11+
- `Innmind\HttpAuthentication\ViaBasicAuthorization` constructor is private, use `::of()` instead
12+
- `Innmind\HttpAuthentication\ViaForm` constructor is private, use `::of()` instead
13+
- `Innmind\HttpAuthentication\ViaUrlAuthority` constructor is private, use `::of()` instead
1014

1115
### Removed
1216

src/ViaAuthorization.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@
1414
*/
1515
final class ViaAuthorization
1616
{
17-
/** @var callable(Authorization): Attempt<T> */
18-
private $resolve;
19-
2017
/**
21-
* @param callable(Authorization): Attempt<T> $resolve
18+
* @param \Closure(Authorization): Attempt<T> $resolve
2219
*/
23-
public function __construct(callable $resolve)
20+
private function __construct(private \Closure $resolve)
2421
{
25-
$this->resolve = $resolve;
2622
}
2723

2824
/**
@@ -36,4 +32,16 @@ public function __invoke(ServerRequest $request): Attempt
3632
->attempt(static fn() => new \RuntimeException('Failed to resolve identity'))
3733
->flatMap(fn($value) => ($this->resolve)($value));
3834
}
35+
36+
/**
37+
* @template A
38+
*
39+
* @param callable(Authorization): Attempt<A> $resolve
40+
*
41+
* @return self<A>
42+
*/
43+
public static function of(callable $resolve): self
44+
{
45+
return new self(\Closure::fromCallable($resolve));
46+
}
3947
}

src/ViaBasicAuthorization.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@
1414
*/
1515
final class ViaBasicAuthorization
1616
{
17-
/** @var callable(string, string): Attempt<T> */
18-
private $resolve;
19-
2017
/**
21-
* @param callable(string, string): Attempt<T> $resolve
18+
* @param \Closure(string, string): Attempt<T> $resolve
2219
*/
23-
public function __construct(callable $resolve)
20+
private function __construct(private \Closure $resolve)
2421
{
25-
$this->resolve = $resolve;
2622
}
2723

2824
/**
@@ -47,4 +43,16 @@ public function __invoke(ServerRequest $request): Attempt
4743
return ($this->resolve)($user, $password);
4844
});
4945
}
46+
47+
/**
48+
* @template A
49+
*
50+
* @param callable(string, string): Attempt<A> $resolve
51+
*
52+
* @return self<A>
53+
*/
54+
public static function of(callable $resolve): self
55+
{
56+
return new self(\Closure::fromCallable($resolve));
57+
}
5058
}

src/ViaForm.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@
1818
*/
1919
final class ViaForm
2020
{
21-
/** @var callable(Form): Attempt<T> */
22-
private $resolve;
23-
2421
/**
25-
* @param callable(Form): Attempt<T> $resolve
22+
* @param \Closure(Form): Attempt<T> $resolve
2623
*/
27-
public function __construct(callable $resolve)
24+
private function __construct(private \Closure $resolve)
2825
{
29-
$this->resolve = $resolve;
3026
}
3127

3228
/**
@@ -39,4 +35,16 @@ public function __invoke(ServerRequest $request): Attempt
3935
->attempt(static fn() => new \RuntimeException('Failed to resolve identity'))
4036
->flatMap(fn($request) => ($this->resolve)($request->form()));
4137
}
38+
39+
/**
40+
* @template A
41+
*
42+
* @param callable(Form): Attempt<A> $resolve
43+
*
44+
* @return self<A>
45+
*/
46+
public static function of(callable $resolve): self
47+
{
48+
return new self(\Closure::fromCallable($resolve));
49+
}
4250
}

src/ViaUrlAuthority.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@
1515
*/
1616
final class ViaUrlAuthority
1717
{
18-
/** @var callable(User, Password): Attempt<T> */
19-
private $resolve;
20-
2118
/**
22-
* @param callable(User, Password): Attempt<T> $resolve
19+
* @param \Closure(User, Password): Attempt<T> $resolve
2320
*/
24-
public function __construct(callable $resolve)
21+
private function __construct(private \Closure $resolve)
2522
{
26-
$this->resolve = $resolve;
2723
}
2824

2925
/**
@@ -41,4 +37,16 @@ public function __invoke(ServerRequest $request): Attempt
4137

4238
return ($this->resolve)($user, $password);
4339
}
40+
41+
/**
42+
* @template A
43+
*
44+
* @param callable(User, Password): Attempt<A> $resolve
45+
*
46+
* @return self<A>
47+
*/
48+
public static function of(callable $resolve): self
49+
{
50+
return new self(\Closure::fromCallable($resolve));
51+
}
4452
}

tests/ViaAuthorizationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ViaAuthorizationTest extends TestCase
2121
{
2222
public function testReturnNothingWhenNoAuthorizationHeader()
2323
{
24-
$authenticate = new ViaAuthorization(
24+
$authenticate = ViaAuthorization::of(
2525
static fn() => throw new \Exception,
2626
);
2727
$request = ServerRequest::of(
@@ -38,7 +38,7 @@ public function testReturnNothingWhenNoAuthorizationHeader()
3838

3939
public function testReturnNothingWhenAuthorizationHeaderNotParsedCorrectly()
4040
{
41-
$authenticate = new ViaAuthorization(
41+
$authenticate = ViaAuthorization::of(
4242
static fn() => throw new \Exception,
4343
);
4444
$request = ServerRequest::of(
@@ -58,7 +58,7 @@ public function testReturnNothingWhenAuthorizationHeaderNotParsedCorrectly()
5858

5959
public function testInvokation()
6060
{
61-
$authenticate = new ViaAuthorization(
61+
$authenticate = ViaAuthorization::of(
6262
static fn($value) => Attempt::result($value),
6363
);
6464
$expected = Authorization::of('Bearer', 'foo');

tests/ViaBasicAuthorizationTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ViaBasicAuthorizationTest extends TestCase
2121
{
2222
public function testReturnNothingWhenNoAuthorizationHeader()
2323
{
24-
$authenticate = new ViaBasicAuthorization(
24+
$authenticate = ViaBasicAuthorization::of(
2525
static fn() => throw new \Exception,
2626
);
2727
$request = ServerRequest::of(
@@ -38,7 +38,7 @@ public function testReturnNothingWhenNoAuthorizationHeader()
3838

3939
public function testReturnNothingWhenAuthorizationHeaderNotParsedCorrectly()
4040
{
41-
$authenticate = new ViaBasicAuthorization(
41+
$authenticate = ViaBasicAuthorization::of(
4242
static fn() => throw new \Exception,
4343
);
4444
$request = ServerRequest::of(
@@ -58,7 +58,7 @@ public function testReturnNothingWhenAuthorizationHeaderNotParsedCorrectly()
5858

5959
public function testReturnNothingWhenNotBasicAuthorization()
6060
{
61-
$authenticate = new ViaBasicAuthorization(
61+
$authenticate = ViaBasicAuthorization::of(
6262
static fn() => throw new \Exception,
6363
);
6464
$request = ServerRequest::of(
@@ -78,7 +78,7 @@ public function testReturnNothingWhenNotBasicAuthorization()
7878

7979
public function testInvokation()
8080
{
81-
$authenticate = new ViaBasicAuthorization(
81+
$authenticate = ViaBasicAuthorization::of(
8282
static fn($user, $password) => Attempt::result([$user, $password]),
8383
);
8484
$request = ServerRequest::of(

tests/ViaFormTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ViaFormTest extends TestCase
1717
{
1818
public function testReturnNothingWhenNotPostRequest()
1919
{
20-
$authenticate = new ViaForm(
20+
$authenticate = ViaForm::of(
2121
static fn() => throw new \Exception,
2222
);
2323
$request = ServerRequest::of(
@@ -34,7 +34,7 @@ public function testReturnNothingWhenNotPostRequest()
3434

3535
public function testInvokation()
3636
{
37-
$authenticate = new ViaForm(
37+
$authenticate = ViaForm::of(
3838
static fn($value) => Attempt::result($value),
3939
);
4040
$request = ServerRequest::of(

tests/ViaUrlAuthorityTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ViaUrlAuthorityTest extends TestCase
1717
{
1818
public function testReturnNothingWhenNoUserProvidedInTheUrl()
1919
{
20-
$authenticate = new ViaUrlAuthority(
20+
$authenticate = ViaUrlAuthority::of(
2121
static fn() => throw new \Exception,
2222
);
2323
$url = Url::of('https://localhost/');
@@ -35,7 +35,7 @@ public function testReturnNothingWhenNoUserProvidedInTheUrl()
3535

3636
public function testInvokation()
3737
{
38-
$authenticate = new ViaUrlAuthority(
38+
$authenticate = ViaUrlAuthority::of(
3939
static fn($user, $password) => Attempt::result([$user, $password]),
4040
);
4141
$url = Url::of('https://user:password@localhost/');

0 commit comments

Comments
 (0)