Failed Conditions
Push — master ( 3df084...2d5f15 )
by Florent
06:05
created

RefreshToken::__construct()   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 7
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\Client\ClientId;
18
use OAuth2Framework\Component\Core\DataBag\DataBag;
19
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwnerId;
20
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId;
21
use OAuth2Framework\Component\Core\Token\Token;
22
23
class RefreshToken extends Token
24
{
25
    /**
26
     * @var AccessTokenId[]
27
     */
28
    private $accessTokenIds = [];
29
30
    public function __construct(RefreshTokenId $refreshTokenId, ClientId $clientId, ResourceOwnerId $resourceOwnerId, \DateTimeImmutable $expiresAt, DataBag $parameter, DataBag $metadata, ?ResourceServerId $resourceServerId)
31
    {
32
        parent::__construct($refreshTokenId, $clientId, $resourceOwnerId, $parameter, $metadata, $expiresAt, $resourceServerId);
33
    }
34
35
    public function addAccessToken(AccessTokenId $accessTokenId): void
36
    {
37
        $id = $accessTokenId->getValue();
38
        if (!\array_key_exists($id, $this->accessTokenIds)) {
39
            $this->accessTokenIds[$id] = $accessTokenId;
40
        }
41
    }
42
43
    /**
44
     * @return AccessTokenId[]
45
     */
46
    public function getAccessTokenIds(): array
47
    {
48
        return $this->accessTokenIds;
49
    }
50
51
    public function getResponseData(): array
52
    {
53
        $data = $this->getParameter();
54
        $data->set('access_token', $this->getTokenId()->getValue());
55
        $data->set('expires_in', $this->getExpiresIn());
56
        if (!empty($this->getTokenId())) {
57
            $data->set('refresh_token', $this->getTokenId());
58
        }
59
60
        return $data->all();
61
    }
62
63
    public function jsonSerialize()
64
    {
65
        $data = parent::jsonSerialize() + [
66
            'refresh_token_id' => $this->getTokenId()->getValue(),
67
            'access_token_ids' => \array_keys($this->getAccessTokenIds()),
68
            'resource_server_id' => $this->getResourceServerId() ? $this->getResourceServerId()->getValue() : null,
69
        ];
70
71
        return $data;
72
    }
73
}
74