Completed
Pull Request — master (#1029)
by Andrew
04:48
created

Psr15ResourceServerMiddleware::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 9
cp 0
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
/**
4
 * @author      Alex Bilbie <[email protected]>
5
 * @copyright   Copyright (c) Alex Bilbie
6
 * @license     http://mit-license.org/
7
 *
8
 * @link        https://github.com/thephpleague/oauth2-server
9
 */
10
11
namespace League\OAuth2\Server\Middleware;
12
13
use Exception;
14
use League\OAuth2\Server\Exception\OAuthServerException;
15
use League\OAuth2\Server\ResourceServer;
16
use Psr\Http\Message\ResponseFactoryInterface;
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Psr\Http\Server\MiddlewareInterface;
20
use Psr\Http\Server\RequestHandlerInterface;
21
22
class Psr15ResourceServerMiddleware implements MiddlewareInterface
23
{
24
    /**
25
     * @var ResourceServer
26
     */
27
    private $server;
28
29
    /**
30
     * @var ResponseFactoryInterface
31
     */
32
    private $responseFactory;
33
34
    /**
35
     * @param ResourceServer           $server
36
     * @param ResponseFactoryInterface $responseFactory
37
     */
38
    public function __construct(ResourceServer $server, ResponseFactoryInterface $responseFactory)
39
    {
40
        $this->server = $server;
41
        $this->responseFactory = $responseFactory;
42
    }
43
44
    /**
45
     * @param ServerRequestInterface  $request
46
     * @param RequestHandlerInterface $handler
47
     *
48
     * @return ResponseInterface
49
     */
50
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
51
    {
52
        try {
53
            $request = $this->server->validateAuthenticatedRequest($request);
54
        } catch (OAuthServerException $exception) {
55
            return $exception->generateHttpResponse($this->responseFactory->createResponse());
56
            // @codeCoverageIgnoreStart
57
        } catch (Exception $exception) {
58
            return (new OAuthServerException($exception->getMessage(), 0, 'unknown_error', 500))
59
                            ->generateHttpResponse($this->responseFactory->createResponse());
60
            // @codeCoverageIgnoreEnd
61
        }
62
63
        // Pass the request on to the next responder in the chain
64
        return $handler->handle($request);
65
    }
66
}
67