AuthenticationErrorResponseMiddleware::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Security\Authentication;
6
7
use Psr\Http\Message\ServerRequestInterface as Request;
8
use Psr\Http\Message\ResponseInterface as Response;
9
10
final class AuthenticationErrorResponseMiddleware
11
{
12
    /**
13
     * @var AuthenticationInterface
14
     */
15
    private $authentication;
16
17
    /**
18
     * @var AuthenticationErrorHandlerInterface
19
     */
20
    private $errorResponseHandler;
21
22
    /**
23
     * @param AuthenticationInterface             $authentication
24
     * @param AuthenticationErrorHandlerInterface $errorResponseHandler
25
     */
26 3
    public function __construct(
27
        AuthenticationInterface $authentication,
28
        AuthenticationErrorHandlerInterface $errorResponseHandler
29
    ) {
30 3
        $this->authentication = $authentication;
31 3
        $this->errorResponseHandler = $errorResponseHandler;
32 3
    }
33
34
    /**
35
     * @param Request       $request
36
     * @param Response      $response
37
     * @param callable|null $next
38
     *
39
     * @return Response
40
     */
41 3
    public function __invoke(Request $request, Response $response, callable $next = null)
42
    {
43 3
        if (!$this->authentication->isAuthenticated($request)) {
44 1
            return $this->errorResponseHandler->errorResponse($request, $response, 401);
45
        }
46
47 2
        if (null !== $next) {
48 1
            $response = $next($request, $response);
49
        }
50
51 2
        return $response;
52
    }
53
}
54