Passed
Pull Request — master (#2)
by Florian
03:22
created

createErrorResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 15
c 0
b 0
f 0
ccs 0
cts 9
cp 0
rs 9.9666
cc 2
nc 2
nop 2
crap 6
1
<?php
2
declare(strict_types=1);
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
namespace Phauthentic\Authentication\Middleware;
15
16
use Phauthentic\Authentication\AuthenticationServiceProviderInterface;
17
use Phauthentic\Authentication\Authenticator\Exception\AuthenticationException;
18
use Phauthentic\Authentication\Authenticator\Exception\UnauthorizedException;
19
use Psr\Http\Message\ResponseFactoryInterface;
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
use Psr\Http\Message\StreamFactoryInterface;
23
use Psr\Http\Server\MiddlewareInterface;
24
use Psr\Http\Server\RequestHandlerInterface;
25
use RuntimeException;
26
27
/**
28
 * Handles the case when the authentication middleware has thrown an exception
29
 */
30
class AuthenticationFailureHandlerMiddleware implements MiddlewareInterface
31
{
32
    /**
33
     * Response factory
34
     *
35
     * @var \Psr\Http\Message\ResponseFactoryInterface
36
     */
37
    protected $responseFactory;
38
39
    /**
40
     * PSR Stream Interface
41
     *
42
     * @var \Psr\Http\Message\StreamFactoryInterface
43
     */
44
    protected $streamFactory;
45
46
    /**
47
     * Constructor.
48
     *
49
     * @param \Psr\Http\Message\ResponseFactoryInterface $responseFactory Factory.
50
     * @param \Psr\Http\Message\StreamFactoryInterface $streamFactory Factory.
51
     */
52
    public function __construct(
53
        ResponseFactoryInterface $responseFactory,
54
        StreamFactoryInterface $streamFactory
55
    ) {
56
        $this->responseFactory = $responseFactory;
57
        $this->streamFactory = $streamFactory;
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     *
63
     * @throws RuntimeException When request attribute exists.
64
     */
65
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
66
    {
67
        try {
68
            return $handler->handle($request);
69
        } catch (UnauthorizedException $e) {
70
            return $this->createErrorResponse($e, $e->getCode());
71
        } catch (AuthenticationException $e) {
72
            return $this->createErrorResponse($e);
73
        }
74
    }
75
76
    /**
77
     * Creates an unauthorized response.
78
     *
79
     * @param UnauthorizedException $e Exception.
80
     * @return ResponseInterface
81
     */
82
    protected function createErrorResponse(AuthenticationException $e, int $responseCode = 500): ResponseInterface
83
    {
84
        $body = $this->streamFactory->createStream();
85
        $body->write($e->getBody());
0 ignored issues
show
Bug introduced by
The method getBody() does not exist on Phauthentic\Authenticati...AuthenticationException. It seems like you code against a sub-type of Phauthentic\Authenticati...AuthenticationException such as Phauthentic\Authenticati...n\UnauthorizedException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
        $body->write($e->/** @scrutinizer ignore-call */ getBody());
Loading history...
86
87
        $response = $this
88
            ->responseFactory
89
            ->createResponse($responseCode)
90
            ->withBody($body);
91
92
        foreach ($e->getHeaders() as $header => $value) {
0 ignored issues
show
Bug introduced by
The method getHeaders() does not exist on Phauthentic\Authenticati...AuthenticationException. It seems like you code against a sub-type of Phauthentic\Authenticati...AuthenticationException such as Phauthentic\Authenticati...n\UnauthorizedException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
        foreach ($e->/** @scrutinizer ignore-call */ getHeaders() as $header => $value) {
Loading history...
93
            $response = $response->withHeader($header, $value);
94
        }
95
96
        return $response;
97
    }
98
}
99