Failed Conditions
Push — master ( c6baf0...a3629e )
by Florent
16:19
created

RefreshToken::setTokenId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
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\RefreshTokenGrant;
15
16
use OAuth2Framework\Component\Core\AccessToken\AccessTokenId;
17
use OAuth2Framework\Component\Core\Event\Event;
18
use OAuth2Framework\Component\Core\Token\Token;
19
use OAuth2Framework\Component\Core\Token\TokenId;
20
use OAuth2Framework\Component\RefreshTokenGrant\Event as RefreshTokenEvent;
21
22
class RefreshToken extends Token
23
{
24
    /**
25
     * @var AccessTokenId[]
26
     */
27
    private $accessTokenIds = [];
28
29
    public static function getSchema(): string
30
    {
31
        return 'https://oauth2-framework.spomky-labs.com/schemas/model/refresh-token/1.0/schema';
32
    }
33
34
    public function setTokenId(TokenId $tokenId): void
35
    {
36
        if (!$tokenId instanceof RefreshTokenId) {
37
            throw new \RuntimeException('The token ID must be an Refresh Token ID.');
38
        }
39
        parent::setTokenId($tokenId);
40
    }
41
42
    public function addAccessToken(AccessTokenId $accessTokenId): void
43
    {
44
        $id = $accessTokenId->getValue();
45
        if (!\array_key_exists($id, $this->accessTokenIds)) {
46
            $this->accessTokenIds[$id] = $accessTokenId;
47
        }
48
    }
49
50
    /**
51
     * @return AccessTokenId[]
52
     */
53
    public function getAccessTokenIds(): array
54
    {
55
        return $this->accessTokenIds;
56
    }
57
58
    public function getResponseData(): array
59
    {
60
        $data = $this->getParameter();
61
        $data->with('access_token', $this->getTokenId()->getValue());
62
        $data->with('expires_in', $this->getExpiresIn());
63
        if (!empty($this->getTokenId())) {
64
            $data = $data->with('refresh_token', $this->getTokenId());
65
        }
66
67
        return $data->all();
68
    }
69
70
    public function jsonSerialize()
71
    {
72
        $data = parent::jsonSerialize() + [
73
            'refresh_token_id' => $this->getTokenId()->getValue(),
74
            'access_token_ids' => \array_keys($this->getAccessTokenIds()),
75
            'resource_server_id' => $this->getResourceServerId() ? $this->getResourceServerId()->getValue() : null,
76
        ];
77
78
        return $data;
79
    }
80
81
    public function apply(Event $event): void
82
    {
83
        $map = $this->getEventMap();
84
        if (!\array_key_exists($event->getType(), $map)) {
85
            throw new \InvalidArgumentException('Unsupported event.');
86
        }
87
        if ($this->getTokenId()->getValue() !== $event->getDomainId()->getValue()) {
88
            throw new \RuntimeException('Event not applicable for this refresh token.');
89
        }
90
        $method = $map[$event->getType()];
91
        $this->$method($event);
92
    }
93
94
    private function getEventMap(): array
95
    {
96
        return [
97
            RefreshTokenEvent\RefreshTokenCreatedEvent::class => 'applyRefreshTokenCreatedEvent',
98
            RefreshTokenEvent\AccessTokenAddedToRefreshTokenEvent::class => 'applyAccessTokenAddedToRefreshTokenEvent',
99
            RefreshTokenEvent\RefreshTokenRevokedEvent::class => 'applyRefreshTokenRevokedEvent',
100
        ];
101
    }
102
103
    protected function applyRefreshTokenCreatedEvent(RefreshTokenEvent\RefreshTokenCreatedEvent $event): void
104
    {
105
        $this->setTokenId($event->getRefreshTokenId());
106
        $this->setResourceOwnerId($event->getResourceOwnerId());
107
        $this->setClientId($event->getClientId());
108
        $this->setParameter($event->getParameter());
109
        $this->setMetadata($event->getMetadata());
110
        $this->setExpiresAt($event->getExpiresAt());
111
        $this->setResourceServerId($event->getResourceServerId());
112
    }
113
114
    protected function applyAccessTokenAddedToRefreshTokenEvent(RefreshTokenEvent\AccessTokenAddedToRefreshTokenEvent $event): void
115
    {
116
        $this->accessTokenIds[$event->getAccessTokenId()->getValue()] = $event->getAccessTokenId();
117
    }
118
119
    protected function applyRefreshTokenRevokedEvent(RefreshTokenEvent\RefreshTokenRevokedEvent $event): void
120
    {
121
        $this->markAsRevoked();
122
    }
123
}
124