Passed
Push — master ( e1fc66...3eafb5 )
by Peter
02:02
created

Api::createResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 15
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Http\Middleware;
6
7
use AbterPhp\Admin\Service\Login as LoginService;
8
use AbterPhp\Framework\Config\Provider as ConfigProvider;
9
use AbterPhp\Framework\Psr7\RequestConverter;
10
use AbterPhp\Framework\Psr7\ResponseConverter;
11
use AbterPhp\Framework\Psr7\ResponseFactory;
12
use Closure;
13
use Exception;
14
use League\OAuth2\Server\Exception\OAuthServerException;
15
use League\OAuth2\Server\ResourceServer;
16
use Opulence\Http\Requests\Request;
17
use Opulence\Http\Responses\Response;
18
use Opulence\Routing\Middleware\IMiddleware;
19
use Psr\Log\LoggerInterface;
20
21
class Api implements IMiddleware
22
{
23
    /** @var ResourceServer */
24
    protected $server;
25
26
    /** @var RequestConverter */
27
    protected $requestConverter;
28
29
    /** @var ResponseFactory */
30
    protected $responseFactory;
31
32
    /** @var ResponseConverter */
33
    protected $responseConverter;
34
35
    /** @var LoggerInterface */
36
    protected $logger;
37
38
    /** @var string */
39
    protected $problemBaseUrl;
40
41
    /**
42
     * @param LoginService $loginService The session used by the application
43
     */
44
    public function __construct(
45
        ResourceServer $server,
46
        RequestConverter $requestConverter,
47
        ResponseFactory $responseFactory,
48
        ResponseConverter $responseConverter,
49
        LoggerInterface $logger,
50
        ConfigProvider $configProvider
51
    ) {
52
        $this->server = $server;
53
54
        $this->requestConverter  = $requestConverter;
55
        $this->responseFactory   = $responseFactory;
56
        $this->responseConverter = $responseConverter;
57
        $this->logger            = $logger;
58
        $this->problemBaseUrl    = $configProvider->getProblemBaseUrl();
59
    }
60
61
    // TODO: Check error response formats
62
    // $next consists of the next middleware in the pipeline
63
    public function handle(Request $request, Closure $next): Response
64
    {
65
        $psr7Request = $this->requestConverter->toPsr($request);
66
67
        try {
68
            $psr7Request = $this->server->validateAuthenticatedRequest($psr7Request);
0 ignored issues
show
Unused Code introduced by
The assignment to $psr7Request is dead and can be removed.
Loading history...
69
        } catch (OAuthServerException $e) {
70
            return $this->createResponse($e);
71
        } catch (Exception $e) {
72
            return $this->createResponse(new OAuthServerException($e->getMessage(), 0, 'unknown_error', 500));
73
        }
74
75
        // $request = $this->requestConverter->fromPsr($psr7Request);
76
77
        return $next($request);
78
    }
79
80
    /**
81
     * @param OAuthServerException $e
82
     *
83
     * @return Response
84
     */
85
    protected function createResponse(OAuthServerException $e): Response
86
    {
87
        $status  = $e->getHttpStatusCode();
88
        $content = [
89
            'type'   => sprintf('%srequest-authentication-failure', $this->problemBaseUrl),
90
            'title'  => 'Access Denied',
91
            'status' => $status,
92
            'detail' => $e->getMessage(),
93
        ];
94
95
        $response = new Response();
96
        $response->setStatusCode($status);
97
        $response->setContent(json_encode($content));
98
99
        return $response;
100
    }
101
}
102