1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fousky\Component\iDoklad\Model\Auth; |
4
|
|
|
|
5
|
|
|
use Fousky\Component\iDoklad\Exception\InvalidResponseException; |
6
|
|
|
use Fousky\Component\iDoklad\Model\iDokladAbstractModel; |
7
|
|
|
use Fousky\Component\iDoklad\Model\iDokladModelInterface; |
8
|
|
|
use Fousky\Component\iDoklad\Util\ResponseUtil; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Lukáš Brzák <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class AccessToken extends iDokladAbstractModel implements \Serializable |
15
|
|
|
{ |
16
|
|
|
/** @var string $token */ |
17
|
|
|
protected $token; |
18
|
|
|
|
19
|
|
|
/** @var int $expires */ |
20
|
|
|
protected $expires; |
21
|
|
|
|
22
|
|
|
/** @var string $type */ |
23
|
|
|
protected $type; |
24
|
|
|
|
25
|
|
|
/** @var \DateTime $requestedAt */ |
26
|
|
|
protected $requestedAt; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $token access token string |
30
|
|
|
* @param int $expires expires in seconds |
31
|
|
|
* @param string $type access token type |
32
|
|
|
* @param \DateTime $requestedAt token requested at some time |
33
|
|
|
*/ |
34
|
|
|
public function __construct( |
35
|
|
|
string $token, |
36
|
|
|
int $expires, |
37
|
|
|
string $type, |
38
|
|
|
\DateTime $requestedAt |
39
|
|
|
) { |
40
|
|
|
$this->token = $token; |
41
|
|
|
$this->expires = $expires; |
42
|
|
|
$this->type = $type; |
43
|
|
|
$this->requestedAt = $requestedAt; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param array $data |
48
|
|
|
* |
49
|
|
|
* @throws \InvalidArgumentException |
50
|
|
|
* |
51
|
|
|
* @return AccessToken |
52
|
|
|
*/ |
53
|
|
|
public static function create(array $data): self |
54
|
|
|
{ |
55
|
|
|
if (!array_key_exists('token', $data) || |
56
|
|
|
!array_key_exists('expires', $data) || |
57
|
|
|
!array_key_exists('type', $data) || |
58
|
|
|
!array_key_exists('requestedAt', $data) |
59
|
|
|
) { |
60
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid argument 1 \$data for %s', __METHOD__)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return new self( |
64
|
|
|
$data['token'], |
65
|
|
|
$data['expires'], |
66
|
|
|
$data['type'], |
67
|
|
|
new \DateTime($data['requestedAt']) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getToken(): string |
75
|
|
|
{ |
76
|
|
|
return $this->token; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return int |
81
|
|
|
*/ |
82
|
|
|
public function getExpires(): int |
83
|
|
|
{ |
84
|
|
|
return $this->expires; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getType(): string |
91
|
|
|
{ |
92
|
|
|
return $this->type; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
public function isExpired(): bool |
99
|
|
|
{ |
100
|
|
|
return new \DateTime() > (clone $this->requestedAt)->modify(sprintf('+%d second', $this->expires)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function serialize(): string |
107
|
|
|
{ |
108
|
|
|
return serialize([ |
109
|
|
|
$this->token, |
110
|
|
|
$this->expires, |
111
|
|
|
$this->type, |
112
|
|
|
$this->requestedAt, |
113
|
|
|
]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $serialized |
118
|
|
|
*/ |
119
|
|
|
public function unserialize($serialized) |
120
|
|
|
{ |
121
|
|
|
list( |
122
|
|
|
$this->token, |
123
|
|
|
$this->expires, |
124
|
|
|
$this->type, |
125
|
|
|
$this->requestedAt |
126
|
|
|
) = unserialize($serialized, ['allowed_classes' => [self::class]]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param ResponseInterface $response |
131
|
|
|
* |
132
|
|
|
* @throws InvalidResponseException |
133
|
|
|
* @throws \RuntimeException |
134
|
|
|
* |
135
|
|
|
* @return iDokladModelInterface |
136
|
|
|
*/ |
137
|
|
|
public static function createFromResponse(ResponseInterface $response): iDokladModelInterface |
138
|
|
|
{ |
139
|
|
|
$result = (array) ResponseUtil::handle($response); |
140
|
|
|
|
141
|
|
View Code Duplication |
if (!is_array($result) || |
|
|
|
|
142
|
|
|
!array_key_exists('access_token', $result) || |
143
|
|
|
!array_key_exists('expires_in', $result) || |
144
|
|
|
!array_key_exists('token_type', $result) |
145
|
|
|
) { |
146
|
|
|
throw new InvalidResponseException(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return new self( |
150
|
|
|
$result['access_token'], |
151
|
|
|
$result['expires_in'], |
152
|
|
|
$result['token_type'], |
153
|
|
|
new \DateTime() |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.