Passed
Push — master ( 799b12...6419e5 )
by Robert
05:29
created

UnauthorizedHandlerMiddleware::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 Robert Pustułka
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