AuthenticationErrorResponseMiddleware   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 44
c 0
b 0
f 0
wmc 4
lcom 0
cbo 2
ccs 10
cts 10
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A __invoke() 0 12 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