Failed Conditions
Push — master ( aacec5...b5a0b4 )
by Florent
04:50
created

AbstractAccessToken::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 6
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\Core\AccessToken;
15
16
use OAuth2Framework\Component\Core\Client\ClientId;
17
use OAuth2Framework\Component\Core\DataBag\DataBag;
18
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwnerId;
19
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId;
20
21
abstract class AbstractAccessToken implements AccessToken
22
{
23
    /**
24
     * @var \DateTimeImmutable
25
     */
26
    private $expiresAt;
27
28
    /**
29
     * @var ResourceOwnerId
30
     */
31
    private $resourceOwnerId;
32
33
    /**
34
     * @var ClientId
35
     */
36
    private $clientId;
37
38
    /**
39
     * @var DataBag
40
     */
41
    private $parameter;
42
43
    /**
44
     * @var DataBag
45
     */
46
    private $metadata;
47
48
    /**
49
     * @var bool
50
     */
51
    private $revoked;
52
53
    /**
54
     * @var ResourceServerId|null
55
     */
56
    private $resourceServerId;
57
58
    public function __construct(ClientId $clientId, ResourceOwnerId $resourceOwnerId, \DateTimeImmutable $expiresAt, DataBag $parameter, DataBag $metadata, ?ResourceServerId $resourceServerId)
59
    {
60
        $this->resourceOwnerId = $resourceOwnerId;
61
        $this->clientId = $clientId;
62
        $this->parameter = $parameter;
63
        $this->metadata = $metadata;
64
        $this->expiresAt = $expiresAt;
65
        $this->resourceServerId = $resourceServerId;
66
        $this->revoked = false;
67
    }
68
69
    public function getExpiresAt(): \DateTimeImmutable
70
    {
71
        return $this->expiresAt;
72
    }
73
74
    public function hasExpired(): bool
75
    {
76
        return $this->expiresAt->getTimestamp() < \time();
77
    }
78
79
    public function getResourceOwnerId(): ResourceOwnerId
80
    {
81
        return $this->resourceOwnerId;
82
    }
83
84
    public function getClientId(): ClientId
85
    {
86
        return $this->clientId;
87
    }
88
89
    public function getParameter(): DataBag
90
    {
91
        return $this->parameter;
92
    }
93
94
    public function getMetadata(): DataBag
95
    {
96
        return $this->metadata;
97
    }
98
99
    public function isRevoked(): bool
100
    {
101
        return $this->revoked;
102
    }
103
104
    public function markAsRevoked(): void
105
    {
106
        $this->revoked = true;
107
    }
108
109
    public function getResourceServerId(): ?ResourceServerId
110
    {
111
        return $this->resourceServerId;
112
    }
113
114
    public function getExpiresIn(): int
115
    {
116
        return $this->expiresAt->getTimestamp() - \time() < 0 ? 0 : $this->expiresAt->getTimestamp() - \time();
117
    }
118
119
    public function jsonSerialize()
120
    {
121
        $data = [
122
            'access_token_id' => $this->getId()->getValue(),
123
            'expires_at' => $this->getExpiresAt()->getTimestamp(),
124
            'client_id' => $this->getClientId()->getValue(),
125
            'parameters' => (object) $this->getParameter()->all(),
126
            'metadatas' => (object) $this->getMetadata()->all(),
127
            'is_revoked' => $this->isRevoked(),
128
            'resource_owner_id' => $this->getResourceOwnerId()->getValue(),
129
            'resource_owner_class' => \get_class($this->getResourceOwnerId()),
130
            'resource_server_id' => $this->getResourceServerId() ? $this->getResourceServerId()->getValue() : null,
131
        ];
132
133
        return $data;
134
    }
135
136
    public function getResponseData(): array
137
    {
138
        $data = $this->getParameter()->all();
139
        $data['access_token'] = $this->getId()->getValue();
140
        $data['expires_in'] = $this->getExpiresIn();
141
142
        return $data;
143
    }
144
}
145