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

AuthenticationMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
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