Completed
Push — master ( e89be9...8c84a6 )
by Dominik
02:10
created

AuthenticationMiddleware::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 3
1
<?php
2
3
namespace Chubbyphp\Security;
4
5
use Chubbyphp\ErrorHandler\HttpException;
6
use Psr\Http\Message\ServerRequestInterface as Request;
7
use Psr\Http\Message\ResponseInterface as Response;
8
9
final class AuthenticationMiddleware
10
{
11
    /**
12
     * @var AuthenticationInterface
13
     */
14
    private $auth;
15
16
    /**
17
     * @param AuthenticationInterface $auth
18
     */
19
    public function __construct(AuthenticationInterface $auth)
20
    {
21
        $this->auth = $auth;
22
    }
23
24
    /**
25
     * @param Request  $request
26
     * @param Response $response
27
     * @param callable $next
28
     *
29
     * @return Response
30
     *
31
     * @throws HttpException
32
     */
33
    public function __invoke(Request $request, Response $response, callable $next)
34
    {
35
        if (!$this->auth->isAuthenticated($request)) {
36
            throw HttpException::create($request, $response, 401);
37
        }
38
39
        $response = $next($request, $response);
40
41
        return $response;
42
    }
43
}
44