AbstractResponseBuilder   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 5
dl 0
loc 74
ccs 0
cts 42
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B success() 0 26 3
A error() 0 10 1
A errorRedirect() 0 6 1
A generateScopeList() 0 8 2
1
<?php
2
3
namespace Phisch\OAuth\Server\Response;
4
5
use Phisch\OAuth\Server\Entity\AccessTokenEntityInterface;
6
use Phisch\OAuth\Server\Entity\RefreshTokenEntityInterface;
7
use Phisch\OAuth\Server\Entity\ScopeEntityInterface;
8
use Phisch\OAuth\Server\Exception\AuthorizationServerException;
9
use Phisch\OAuth\Server\Token\TokenTypeInterface;
10
11
abstract class AbstractResponseBuilder implements ResponseBuilderInterface
12
{
13
    /**
14
     * @param TokenTypeInterface $tokenType
15
     * @param AccessTokenEntityInterface $accessToken
16
     * @param RefreshTokenEntityInterface|null $refreshToken
17
     * @param ScopeEntityInterface[]|null $scopes
18
     * @return mixed
19
     */
20
    public function success(
21
        TokenTypeInterface $tokenType,
22
        AccessTokenEntityInterface $accessToken,
23
        RefreshTokenEntityInterface $refreshToken = null,
24
        array $scopes = null
25
    ) {
26
        $data = [
27
            'access_token' => $tokenType->generate($accessToken),
28
            'token_type' => $tokenType->getType(),
29
            'expires_in' => $accessToken->getExpiryDateTime()->getTimestamp() - (new \DateTime())->getTimestamp()
30
        ];
31
32
        // refresh token is optional
33
        if ($refreshToken instanceof RefreshTokenEntityInterface) {
34
            // TODO: check if refresh_token should use the same token type as access_token
35
            $data['refresh_token'] = $refreshToken->getIdentifier();
36
        }
37
38
        // scope is optional
39
        if (!empty($scopes)) {
40
            $data['scope'] = $this->generateScopeList($scopes);
41
        }
42
43
44
        return $this->buildSuccessResponse($data, 200);
45
    }
46
47
    /**
48
     * @param AuthorizationServerException $exception
49
     * @return mixed
50
     */
51
    public function error(AuthorizationServerException $exception)
52
    {
53
        $data = [
54
            'error' => $exception->getErrorCode(),
55
            'error_description' => $exception->getMessage(),
56
            'error_uri' => '' //TODO: implement error detail endpoint and generate uris
57
        ];
58
59
        return $this->buildErrorResponse($data, 400);
60
    }
61
62
    /**
63
     * @param AuthorizationServerException $exception
64
     */
65
    public function errorRedirect(AuthorizationServerException $exception)
66
    {
67
        return $this->buildErrorRedirectResponse(
0 ignored issues
show
Bug introduced by
The call to buildErrorRedirectResponse() misses some required arguments starting with $error.
Loading history...
68
            $exception->getErrorCode()
69
        );
70
    }
71
72
    /**
73
     * @param ScopeEntityInterface[] $scopes
74
     * @return string
75
     */
76
    private function generateScopeList(array $scopes)
77
    {
78
        $scopeNames = [];
79
        foreach ($scopes as $scope) {
80
            $scopeNames[] = $scope->getName();
81
        }
82
        return implode(' ', $scopeNames);
83
    }
84
}
85