Test Setup Failed
Push — dev ( b27119...389162 )
by Herberto
04:46
created

AccessTokenController::convertResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Acme\App\Presentation\Api\Rest\Oauth;
6
7
use DateInterval;
8
use Exception;
9
use League\OAuth2\Server\AuthorizationServer;
10
use League\OAuth2\Server\Exception\OAuthServerException;
11
use League\OAuth2\Server\Grant\PasswordGrant;
12
use Psr\Http\Message\ResponseInterface;
13
use Psr\Http\Message\ServerRequestInterface;
14
use Symfony\Component\HttpFoundation\Response;
15
use Throwable;
16
use Zend\Diactoros\Response as Psr7Response;
17
18
final class AccessTokenController
19
{
20
    /**
21
     * @var AuthorizationServer
22
     */
23
    private $authorizationServer;
24
25
    /**
26
     * @var PasswordGrant
27
     */
28
    private $passwordGrant;
29
30
    public function __construct(AuthorizationServer $authorizationServer, PasswordGrant $passwordGrant)
31
    {
32
        $this->authorizationServer = $authorizationServer;
33
        $this->passwordGrant = $passwordGrant;
34
    }
35
36
    /**
37
     * @throws Exception
38
     */
39
    public function post(ServerRequestInterface $request): ?ResponseInterface
40
    {
41
        try {
42
            $this->passwordGrant->setRefreshTokenTTL(new DateInterval('P1M'));
43
            $this->authorizationServer->enableGrantType($this->passwordGrant, new DateInterval('PT1H'));
44
45
            return $this->authorizationServer->respondToAccessTokenRequest($request, new Psr7Response());
46
        } catch (OAuthServerException $e) {
0 ignored issues
show
Bug introduced by
The class League\OAuth2\Server\Exc...on\OAuthServerException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
47
            return $this->convertResponse($e->generateHttpResponse(new Psr7Response()));
48
        } catch (Throwable $e) {
49
            return new Psr7Response($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
50
        }
51
    }
52
53
    private function convertResponse(ResponseInterface $psrResponse): ResponseInterface
54
    {
55
        return new Psr7Response($psrResponse->getBody(), $psrResponse->getStatusCode(), $psrResponse->getHeaders());
56
    }
57
}
58