Completed
Push — master ( 2cb995...b7feec )
by Philipp
02:41
created

AbstractResponseBuilder::error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
crap 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
69
            $exception->getErrorCode()
70
71
        );
72
    }
73
74
    /**
75
     * @param ScopeEntityInterface[] $scopes
76
     * @return string
77
     */
78
    private function generateScopeList(array $scopes)
79
    {
80
        $scopeNames = [];
81
        foreach ($scopes as $scope) {
82
            $scopeNames[] = $scope->getName();
83
        }
84
        return implode(' ', $scopeNames);
85
    }
86
}
87