UnauthorizedHandlerMiddleware   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 42
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 9 2
A createUnauthorizedResponse() 0 13 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\Authenticator\Exception\UnauthorizedException;
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Zend\Diactoros\Stream;
20
21
class UnauthorizedHandlerMiddleware
22
{
23
24
    /**
25
     * Callable implementation for the middleware stack.
26
     *
27
     * @param ServerRequestInterface $request The request.
28
     * @param ResponseInterface $response The response.
29
     * @param callable $next The next middleware to call.
30
     * @return ResponseInterface A response.
31
     */
32 1
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
33
    {
34
        try {
35 1
            return $next($request, $response);
36 1
        } catch (UnauthorizedException $e) {
37 1
            return $this->createUnauthorizedResponse($e, $response);
38
        }
39
40
        return $response;
0 ignored issues
show
Unused Code introduced by
return $response is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
41
    }
42
43
    /**
44
     * Creates an unauthorized response.
45
     *
46
     * @param UnauthorizedException $e Exception.
47
     * @param ResponseInterface $response The response.
48
     * @return ResponseInterface
49
     */
50 1
    protected function createUnauthorizedResponse(UnauthorizedException $e, ResponseInterface $response): ResponseInterface
51
    {
52 1
        $body = new Stream('php://memory', 'rw');
53 1
        $body->write($e->getBody());
54
        $response = $response
55 1
            ->withStatus($e->getCode())
56 1
            ->withBody($body);
57
58 1
        foreach ($e->getHeaders() as $header => $value) {
59 1
            $response = $response->withHeader($header, $value);
60
        }
61
62 1
        return $response;
63
    }
64
}
65