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

ResourceServerMiddleware   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 39
ccs 0
cts 13
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 16 3
1
<?php
2
/**
3
 * @author      Alex Bilbie <[email protected]>
4
 * @copyright   Copyright (c) Alex Bilbie
5
 * @license     http://mit-license.org/
6
 *
7
 * @link        https://github.com/thephpleague/oauth2-server
8
 */
9
10
namespace League\OAuth2\Server\Middleware;
11
12
use Exception;
13
use League\OAuth2\Server\Exception\OAuthServerException;
14
use League\OAuth2\Server\ResourceServer;
15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
18
class ResourceServerMiddleware
19
{
20
    /**
21
     * @var ResourceServer
22
     */
23
    private $server;
24
25
    /**
26
     * @param ResourceServer $server
27
     */
28
    public function __construct(ResourceServer $server)
29
    {
30
        $this->server = $server;
31
    }
32
33
    /**
34
     * @param ServerRequestInterface $request
35
     * @param ResponseInterface      $response
36
     * @param callable               $next
37
     *
38
     * @return ResponseInterface
39
     */
40
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
41
    {
42
        try {
43
            $request = $this->server->validateAuthenticatedRequest($request);
44
        } catch (OAuthServerException $exception) {
45
            return $exception->generateHttpResponse($response);
46
            // @codeCoverageIgnoreStart
47
        } catch (Exception $exception) {
48
            return (new OAuthServerException($exception->getMessage(), 0, 'unknown_error', 500))
49
                ->generateHttpResponse($response);
50
            // @codeCoverageIgnoreEnd
51
        }
52
53
        // Pass the request and response on to the next responder in the chain
54
        return $next($request, $response);
55
    }
56
}
57