Test Setup Failed
Push — oauth2 ( 63c070 )
by Herberto
11:43 queued 07:12
created

AccessTokenController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 49
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A post() 0 13 3
A createPsrResponse() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Presentation\Api\Rest\Oauth;
16
17
use DateInterval;
18
use Exception;
19
use League\OAuth2\Server\AuthorizationServer;
20
use League\OAuth2\Server\Exception\OAuthServerException;
21
use League\OAuth2\Server\Grant\PasswordGrant;
22
use Psr\Http\Message\ResponseInterface;
23
use Psr\Http\Message\ServerRequestInterface;
24
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
25
use Symfony\Component\HttpFoundation\Response;
26
use Throwable;
27
28
final class AccessTokenController
29
{
30
    /**
31
     * @var AuthorizationServer
32
     */
33
    private $authorizationServer;
34
35
    /**
36
     * @var PasswordGrant
37
     */
38
    private $passwordGrant;
39
40
    /**
41
     * @var HttpMessageFactoryInterface
42
     */
43
    private $psrHttpMessageFactory;
44
45
    public function __construct(
46
        AuthorizationServer $authorizationServer,
47
        PasswordGrant $passwordGrant,
48
        HttpMessageFactoryInterface $psrHttpMessageFactory
49
    ) {
50
        $this->authorizationServer = $authorizationServer;
51
        $this->passwordGrant = $passwordGrant;
52
        $this->psrHttpMessageFactory = $psrHttpMessageFactory;
53
    }
54
55
    /**
56
     * @throws Exception
57
     */
58
    public function post(ServerRequestInterface $request): ?ResponseInterface
59
    {
60
        try {
61
            $this->passwordGrant->setRefreshTokenTTL(new DateInterval('P1M'));
62
            $this->authorizationServer->enableGrantType($this->passwordGrant, new DateInterval('PT1H'));
63
64
            return $this->authorizationServer->respondToAccessTokenRequest($request, $this->createPsrResponse());
65
        } catch (OAuthServerException $e) {
66
            return $e->generateHttpResponse($this->createPsrResponse());
67
        } catch (Throwable $e) {
68
            return $this->createPsrResponse($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
69
        }
70
    }
71
72
    private function createPsrResponse($content = '', int $status = 200, array $headers = []): ResponseInterface
73
    {
74
        return $this->psrHttpMessageFactory->createResponse(new Response($content, $status, $headers));
75
    }
76
}
77