AbstractRefreshToken   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 35
c 1
b 1
f 0
dl 0
loc 127
rs 10
wmc 16

14 Methods

Rating   Name   Duplication   Size   Complexity  
A isRevoked() 0 3 1
A getExpiresAt() 0 3 1
A getResponseData() 0 8 1
A hasExpired() 0 3 1
A getAccessTokenIds() 0 3 1
A getParameter() 0 3 1
A getResourceOwnerId() 0 3 1
A getExpiresIn() 0 3 2
A markAsRevoked() 0 3 1
A addAccessToken() 0 5 2
A getClientId() 0 3 1
A getMetadata() 0 3 1
A __construct() 0 9 1
A getResourceServerId() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 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
22
abstract class AbstractRefreshToken implements RefreshToken
23
{
24
    /**
25
     * @var AccessTokenId[]
26
     */
27
    private $accessTokenIds = [];
28
29
    /**
30
     * @var \DateTimeImmutable
31
     */
32
    private $expiresAt;
33
34
    /**
35
     * @var ResourceOwnerId
36
     */
37
    private $resourceOwnerId;
38
39
    /**
40
     * @var ClientId
41
     */
42
    private $clientId;
43
44
    /**
45
     * @var DataBag
46
     */
47
    private $parameter;
48
49
    /**
50
     * @var DataBag
51
     */
52
    private $metadata;
53
54
    /**
55
     * @var bool
56
     */
57
    private $revoked;
58
59
    /**
60
     * @var null|ResourceServerId
61
     */
62
    private $resourceServerId;
63
64
    public function __construct(ClientId $clientId, ResourceOwnerId $resourceOwnerId, \DateTimeImmutable $expiresAt, DataBag $parameter, DataBag $metadata, ?ResourceServerId $resourceServerId)
65
    {
66
        $this->resourceOwnerId = $resourceOwnerId;
67
        $this->clientId = $clientId;
68
        $this->parameter = $parameter;
69
        $this->metadata = $metadata;
70
        $this->expiresAt = $expiresAt;
71
        $this->resourceServerId = $resourceServerId;
72
        $this->revoked = false;
73
    }
74
75
    public function addAccessToken(AccessTokenId $accessTokenId): void
76
    {
77
        $id = $accessTokenId->getValue();
78
        if (!\array_key_exists($id, $this->accessTokenIds)) {
79
            $this->accessTokenIds[$id] = $accessTokenId;
80
        }
81
    }
82
83
    /**
84
     * @return AccessTokenId[]
85
     */
86
    public function getAccessTokenIds(): array
87
    {
88
        return $this->accessTokenIds;
89
    }
90
91
    public function getResponseData(): array
92
    {
93
        $data = $this->getParameter();
94
        $data->set('access_token', $this->getId()->getValue());
95
        $data->set('expires_in', $this->getExpiresIn());
96
        $data->set('refresh_token', $this->getId());
97
98
        return $data->all();
99
    }
100
101
    public function getExpiresAt(): \DateTimeImmutable
102
    {
103
        return $this->expiresAt;
104
    }
105
106
    public function hasExpired(): bool
107
    {
108
        return $this->expiresAt->getTimestamp() < time();
109
    }
110
111
    public function getResourceOwnerId(): ResourceOwnerId
112
    {
113
        return $this->resourceOwnerId;
114
    }
115
116
    public function getClientId(): ClientId
117
    {
118
        return $this->clientId;
119
    }
120
121
    public function getParameter(): DataBag
122
    {
123
        return $this->parameter;
124
    }
125
126
    public function getMetadata(): DataBag
127
    {
128
        return $this->metadata;
129
    }
130
131
    public function isRevoked(): bool
132
    {
133
        return $this->revoked;
134
    }
135
136
    public function markAsRevoked(): void
137
    {
138
        $this->revoked = true;
139
    }
140
141
    public function getResourceServerId(): ?ResourceServerId
142
    {
143
        return $this->resourceServerId;
144
    }
145
146
    public function getExpiresIn(): int
147
    {
148
        return $this->expiresAt->getTimestamp() - time() < 0 ? 0 : $this->expiresAt->getTimestamp() - time();
149
    }
150
}
151