Test Failed
Pull Request — master (#11)
by Florian
07:04 queued 04:09
created

createUnauthorizedResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 9
dl 0
loc 15
ccs 9
cts 10
cp 0.9
rs 9.9666
c 1
b 1
f 0
cc 2
nc 2
nop 1
crap 2.004
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
declare(strict_types=1);
16
17
namespace Phauthentic\Authentication\Middleware;
18
19
use Phauthentic\Authentication\Authenticator\Exception\UnauthorizedException;
20
use Psr\Http\Message\ResponseFactoryInterface;
21
use Psr\Http\Message\ResponseInterface;
22
use Psr\Http\Message\ServerRequestInterface;
23
use Psr\Http\Message\StreamFactoryInterface;
24
use Psr\Http\Server\MiddlewareInterface;
25
use Psr\Http\Server\RequestHandlerInterface;
26
27
/**
28
 * Handles the case when the authentication middleware has thrown an exception
29
 */
30
class AuthenticationErrorHandlerMiddleware implements MiddlewareInterface
31
{
32
    /**
33
     * Response factory
34
     *
35
     * @var \Psr\Http\Message\ResponseFactoryInterface
36
     */
37
    protected ResponseFactoryInterface $responseFactory;
38
39
    /**
40
     * PSR Stream Interface
41
     *
42
     * @var \Psr\Http\Message\StreamFactoryInterface
43
     */
44
    protected StreamFactoryInterface $streamFactory;
45
46
    /**
47
     * Constructor.
48
     *
49
     * @param \Psr\Http\Message\ResponseFactoryInterface $responseFactory Factory.
50
     * @param \Psr\Http\Message\StreamFactoryInterface $streamFactory Factory.
51
     */
52 2
    public function __construct(
53
        ResponseFactoryInterface $responseFactory,
54
        StreamFactoryInterface $streamFactory
55
    ) {
56 2
        $this->responseFactory = $responseFactory;
57 2
        $this->streamFactory = $streamFactory;
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63 2
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
64
    {
65
        try {
66 2
            return $handler->handle($request);
67 2
        } catch (UnauthorizedException $e) {
68 1
            return $this->createUnauthorizedResponse($e);
69
        }
70
    }
71
72
    /**
73
     * Creates an unauthorized response.
74
     *
75
     * @param \Phauthentic\Authentication\Authenticator\Exception\UnauthorizedException $e Exception.
76
     * @return \Psr\Http\Message\ResponseInterface
77
     */
78 1
    protected function createUnauthorizedResponse(UnauthorizedException $exception): ResponseInterface
79
    {
80 1
        $body = $this->streamFactory->createStream();
81 1
        $body->write($exception->getBody());
82
83 1
        $response = $this
84 1
            ->responseFactory
85 1
            ->createResponse($exception->getCode())
86 1
            ->withBody($body);
87
88 1
        foreach ($exception->getHeaders() as $header => $value) {
89
            $response = $response->withHeader($header, $value);
90
        }
91
92 1
        return $response;
93
    }
94
}
95