Completed
Pull Request — master (#636)
by Alex
67:31 queued 32:31
created

Psr7ResourceServerMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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 League\OAuth2\Server\Exception\OAuthServerException;
13
use League\OAuth2\Server\ResourceServer;
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
17
class Psr7ResourceServerMiddleware
18
{
19
    /**
20
     * @var ResourceServer
21
     */
22
    private $server;
23
24
    /**
25
     * @param ResourceServer $server
26
     */
27
    public function __construct(ResourceServer $server)
28
    {
29
        $this->server = $server;
30
    }
31
32
    /**
33
     * @param ServerRequestInterface $request
34
     * @param ResponseInterface      $response
35
     * @param callable               $next
36
     *
37
     * @return \Psr\Http\Message\ResponseInterface
38
     */
39
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
40
    {
41
        try {
42
            $request = $this->server->validateAuthenticatedRequest($request);
43
        } catch (OAuthServerException $exception) {
44
            return $exception->generateHttpResponse($response);
45
            // @codeCoverageIgnoreStart
46
        } catch (\Exception $exception) {
47
            return (new OAuthServerException($exception->getMessage(), 0, 'unknown_error', 500))
48
                ->generateHttpResponse($response);
49
            // @codeCoverageIgnoreEnd
50
        }
51
52
        // Pass the request and response on to the next responder in the chain
53
        return $next($request, $response);
54
    }
55
}
56