|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Chubbyphp\Security\Authentication; |
|
6
|
|
|
|
|
7
|
|
|
use Chubbyphp\ErrorHandler\HttpException; |
|
8
|
|
|
use Pimple\Container; |
|
9
|
|
|
use Pimple\ServiceProviderInterface; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
12
|
|
|
|
|
13
|
|
|
final class AuthenticationProvider implements ServiceProviderInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param Container $container |
|
17
|
|
|
*/ |
|
18
|
|
|
public function register(Container $container) |
|
19
|
|
|
{ |
|
20
|
1 |
|
$container['security.authentication.passwordmanager'] = function () { |
|
21
|
1 |
|
return new PasswordManager(); |
|
22
|
|
|
}; |
|
23
|
|
|
|
|
24
|
1 |
|
$this->registerAuthentication($container); |
|
|
|
|
|
|
25
|
1 |
|
$this->registerMiddleware($container); |
|
26
|
1 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param Container $container |
|
30
|
|
|
*/ |
|
31
|
|
|
private function registerAuthentication(Container $container) |
|
32
|
|
|
{ |
|
33
|
1 |
|
$container['security.authentication.authentications'] = function () { |
|
34
|
1 |
|
return []; |
|
35
|
|
|
}; |
|
36
|
|
|
|
|
37
|
1 |
|
$container['security.authentication'] = function () use ($container) { |
|
38
|
1 |
|
return new AuthenticationStack($container['security.authentication.authentications']); |
|
39
|
|
|
}; |
|
40
|
1 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param Container $container |
|
44
|
|
|
*/ |
|
45
|
|
|
private function registerMiddleware(Container $container) |
|
46
|
|
|
{ |
|
47
|
|
|
$container['security.authentication.errorResponseHandler'] = new class() implements AuthenticationErrorHandlerInterface { |
|
48
|
1 |
|
public function errorResponse( |
|
49
|
|
|
Request $request, |
|
50
|
|
|
Response $response, |
|
51
|
|
|
int $code, |
|
52
|
|
|
string $reasonPhrase = null |
|
53
|
|
|
): Response { |
|
54
|
1 |
|
throw HttpException::create($request, $response, $code, $reasonPhrase); |
|
55
|
|
|
} |
|
56
|
|
|
}; |
|
57
|
|
|
|
|
58
|
1 |
|
$container['security.authentication.middleware'] = function () use ($container) { |
|
59
|
1 |
|
return new AuthenticationErrorResponseMiddleware( |
|
60
|
1 |
|
$container['security.authentication'], |
|
61
|
1 |
|
$container['security.authentication.errorResponseHandler'] |
|
62
|
|
|
); |
|
63
|
|
|
}; |
|
64
|
1 |
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: