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

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 6
c 0
b 0
f 0
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
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\AuthenticationExceptionInterface;
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
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
64
    {
65
        try {
66
            return $handler->handle($request);
67
        } catch (UnauthorizedException $e) {
68
            return $this->createUnauthorizedResponse($e);
69
        } catch (AuthenticationException $e) {
0 ignored issues
show
Bug introduced by
The type Phauthentic\Authenticati...AuthenticationException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
70
            return $this->createErrorResponse($e);
71
        }
72
    }
73
74
    /**
75
     * Creates an unauthorized response.
76
     *
77
     * @param UnauthorizedException $e Exception.
78
     * @return ResponseInterface
79
     */
80
    protected function createUnauthorizedResponse(UnauthorizedException $e): ResponseInterface
81
    {
82
        $body = $this->streamFactory->createStream();
83
        $body->write($e->getBody());
84
85
        $response = $this
86
            ->responseFactory
87
            ->createResponse($e->getCode())
88
            ->withBody($body);
89
90
        foreach ($e->getHeaders() as $header => $value) {
91
            $response = $response->withHeader($header, $value);
92
        }
93
94
        return $response;
95
    }
96
97
    /**
98
     * Creates an error response.
99
     *
100
     * @param UnauthorizedException $e Exception.
101
     * @return ResponseInterface
102
     */
103
    protected function createErrorResponse(AuthenticationExceptionInterface $e, int $responseCode = 500): ResponseInterface
104
    {
105
        $body = $this->streamFactory->createStream();
106
        $body->write($e->getMessage());
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on Phauthentic\Authenticati...ationExceptionInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Phauthentic\Authenticati...ationExceptionInterface. ( Ignorable by Annotation )

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

106
        $body->write($e->/** @scrutinizer ignore-call */ getMessage());
Loading history...
107
108
        $response = $this
109
            ->responseFactory
110
            ->createResponse($responseCode)
111
            ->withBody($body);
112
113
        return $response;
114
    }
115
}
116