Token   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 43
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 12 2
1
<?php
2
3
namespace Chadicus\Slim\OAuth2\Routes;
4
5
use Chadicus\Slim\OAuth2\Http\RequestBridge;
6
use Chadicus\Slim\OAuth2\Http\ResponseBridge;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use OAuth2;
10
11
/**
12
 * Slim route for /token endpoint.
13
 */
14
final class Token implements RouteCallbackInterface
15
{
16
    const ROUTE = '/token';
17
18
    /**
19
     * The OAuth2 server instance.
20
     *
21
     * @var OAuth2\Server
22
     */
23
    private $server;
24
25
    /**
26
     * Create a new instance of the Token route.
27
     *
28
     * @param OAuth2\Server $server The oauth2 server imstance.
29
     */
30
    public function __construct(OAuth2\Server $server)
31
    {
32
        $this->server = $server;
33
    }
34
35
    /**
36
     * Invoke this route callback.
37
     *
38
     * @param ServerRequestInterface $request   Represents the current HTTP request.
39
     * @param ResponseInterface      $response  Represents the current HTTP response.
40
     * @param array                  $arguments Values for the current route’s named placeholders.
41
     *
42
     * @return RequestInterface
43
     */
44
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $arguments = [])
45
    {
46
        $response = ResponseBridge::fromOAuth2(
47
            $this->server->handleTokenRequest(RequestBridge::toOAuth2($request))
48
        );
49
50
        if ($response->hasHeader('Content-Type')) {
51
            return $response;
52
        }
53
54
        return $response->withHeader('Content-Type', 'application/json');
55
    }
56
}
57