Failed Conditions
Push — master ( d3ed62...7f2d83 )
by Florent
281:43 queued 02:00
created

AuthorizationCode::applyAuthorizationCodeCreatedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\AuthorizationCodeGrant;
15
16
use OAuth2Framework\Component\Core\Client\ClientId;
17
use OAuth2Framework\Component\Core\DataBag\DataBag;
18
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId;
19
use OAuth2Framework\Component\Core\Token\Token;
20
use OAuth2Framework\Component\Core\Token\TokenId;
21
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
22
23
class AuthorizationCode extends Token
24
{
25
    private $queryParameters;
26
    private $redirectUri;
27
    private $used;
28
29
    public function __construct(AuthorizationCodeId $authorizationCodeId, ClientId $clientId, UserAccountId $userAccountId, array $queryParameters, string $redirectUri, \DateTimeImmutable $expiresAt, DataBag $parameter, DataBag $metadata, ?ResourceServerId $resourceServerId)
30
    {
31
        parent::__construct($authorizationCodeId, $clientId, $userAccountId, $parameter, $metadata, $expiresAt, $resourceServerId);
32
        $this->queryParameters = $queryParameters;
33
        $this->redirectUri = $redirectUri;
34
        $this->used = false;
35
    }
36
37
    public static function getSchema(): string
38
    {
39
        return 'https://oauth2-framework.spomky-labs.com/schemas/model/authorization-code/1.0/schema';
40
    }
41
42
    public function setTokenId(TokenId $tokenId): void
43
    {
44
        if (!$tokenId instanceof AuthorizationCodeId) {
45
            throw new \RuntimeException('The token ID must be an Authorization Code ID.');
46
        }
47
        parent::setTokenId($tokenId);
48
    }
49
50
    public function getQueryParameters(): array
51
    {
52
        return $this->queryParameters;
53
    }
54
55
    public function setQueryParameters(array $queryParameters): void
56
    {
57
        $this->queryParameters = $queryParameters;
58
    }
59
60
    public function isUsed(): bool
61
    {
62
        return $this->used;
63
    }
64
65
    public function markAsUsed(): void
66
    {
67
        $this->used = true;
68
    }
69
70
    public function getQueryParams(): array
71
    {
72
        return $this->queryParameters;
73
    }
74
75
    public function getQueryParam(string $key)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
76
    {
77
        if (!$this->hasQueryParam($key)) {
78
            throw new \RuntimeException(\sprintf('Query parameter with key "%s" does not exist.', $key));
79
        }
80
81
        return $this->queryParameters[$key];
82
    }
83
84
    public function hasQueryParam(string $key): bool
85
    {
86
        return \array_key_exists($key, $this->getQueryParams());
87
    }
88
89
    public function getRedirectUri(): string
90
    {
91
        return $this->redirectUri;
92
    }
93
94
    public function setRedirectUri(string $redirectUri): void
95
    {
96
        $this->redirectUri = $redirectUri;
97
    }
98
99
    public function toArray(): array
100
    {
101
        return [
102
            'code' => $this->getTokenId()->getValue(),
103
        ];
104
    }
105
106
    public function jsonSerialize()
107
    {
108
        $data = parent::jsonSerialize() + [
109
            'auth_code_id' => $this->getTokenId()->getValue(),
110
            'query_parameters' => (object) $this->getQueryParameters(),
111
            'redirect_uri' => $this->getRedirectUri(),
112
            'is_used' => $this->isUsed(),
113
        ];
114
115
        return $data;
116
    }
117
}
118