Passed
Branch 2.0.0 (f4033a)
by Florian
02:52
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * Copyright (c) Phauthentic (https://github.com/Phauthentic)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright     Copyright (c) Phauthentic (https://github.com/Phauthentic)
11
 * @link          https://github.com/Phauthentic
12
 * @license       https://opensource.org/licenses/mit-license.php MIT License
13
 */
14
15
namespace Phauthentic\Authentication\Middleware;
16
17
use Phauthentic\Authentication\Authenticator\Exception\UnauthorizedException;
18
use Psr\Http\Message\ResponseFactoryInterface;
19
use Psr\Http\Message\ResponseInterface;
20
use Psr\Http\Message\ServerRequestInterface;
21
use Psr\Http\Message\StreamFactoryInterface;
22
use Psr\Http\Server\MiddlewareInterface;
23
use Psr\Http\Server\RequestHandlerInterface;
24
25
/**
26
 * Handles the case when the authentication middleware has thrown an exception
27
 */
28
class AuthenticationErrorHandlerMiddleware implements MiddlewareInterface
29
{
30
    /**
31
     * Response factory
32
     *
33
     * @var \Psr\Http\Message\ResponseFactoryInterface
34
     */
35
    protected ResponseFactoryInterface $responseFactory;
36
37
    /**
38
     * PSR Stream Interface
39
     *
40
     * @var \Psr\Http\Message\StreamFactoryInterface
41
     */
42
    protected StreamFactoryInterface $streamFactory;
43
44
    /**
45
     * Constructor.
46
     *
47
     * @param \Psr\Http\Message\ResponseFactoryInterface $responseFactory Factory.
48
     * @param \Psr\Http\Message\StreamFactoryInterface $streamFactory Factory.
49
     */
50 4
    public function __construct(
51
        ResponseFactoryInterface $responseFactory,
52
        StreamFactoryInterface $streamFactory
53
    ) {
54 4
        $this->responseFactory = $responseFactory;
55 4
        $this->streamFactory = $streamFactory;
56 4
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61 4
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
62
    {
63
        try {
64 4
            return $handler->handle($request);
65 4
        } catch (UnauthorizedException $e) {
66 2
            return $this->createUnauthorizedResponse($e);
67
        }
68
    }
69
70
    /**
71
     * Creates an unauthorized response.
72
     *
73
     * @param \Phauthentic\Authentication\Authenticator\Exception\UnauthorizedException $e Exception.
74
     * @return \Psr\Http\Message\ResponseInterface
75
     */
76 2
    protected function createUnauthorizedResponse(UnauthorizedException $e): ResponseInterface
77
    {
78 2
        $body = $this->streamFactory->createStream();
79 2
        $body->write($e->getBody());
80
81 1
        $response = $this
82 2
            ->responseFactory
83 2
            ->createResponse($e->getCode())
84 2
            ->withBody($body);
85
86 2
        foreach ($e->getHeaders() as $header => $value) {
87
            $response = $response->withHeader($header, $value);
88
        }
89
90 2
        return $response;
91
    }
92
}
93