|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gertoska\OAuth2Request; |
|
6
|
|
|
|
|
7
|
|
|
use Gertoska\OAuth2Request\Credential\Credential; |
|
8
|
|
|
use Gertoska\OAuth2Request\Credential\CredentialFactory; |
|
9
|
|
|
use GuzzleHttp\Client; |
|
10
|
|
|
|
|
11
|
|
|
final class Request |
|
12
|
|
|
{ |
|
13
|
|
|
private $credentialFactory; |
|
14
|
|
|
private $client; |
|
15
|
|
|
private $credential; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(array $accessData) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->client = new Client(); |
|
20
|
|
|
|
|
21
|
|
|
$this->credentialFactory = new CredentialFactory(); |
|
22
|
|
|
$this->credential = $this->credentialFactory->buildFromArray($accessData); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function getOrRefreshAccessToken(): Credential |
|
26
|
|
|
{ |
|
27
|
|
|
if ($this->credential->checkIfIsNotNecessaryToRefreshToken()) { |
|
28
|
|
|
return $this->credential; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$response = $this->client->request( |
|
32
|
|
|
'post', |
|
33
|
|
|
$this->credential->uri() . '/oauth/token', |
|
34
|
|
|
[ |
|
35
|
|
|
'headers' => [ |
|
36
|
|
|
'content-type' => 'application/x-www-form-urlencoded', |
|
37
|
|
|
'cache-control' => 'no-cache', |
|
38
|
|
|
'authorization' => 'Basic ' . $this->credential->authorization(), |
|
39
|
|
|
], |
|
40
|
|
|
'form_params' => [ |
|
41
|
|
|
'grant_type' => $this->credential->grantType(), |
|
42
|
|
|
'username' => $this->credential->username(), |
|
43
|
|
|
'password' => $this->credential->password(), |
|
44
|
|
|
], |
|
45
|
|
|
] |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
$response = json_decode((string) $response->getBody()); |
|
49
|
|
|
|
|
50
|
|
|
return $this->credential->setOAuth( |
|
51
|
|
|
$response->access_token, |
|
52
|
|
|
$response->token_type, |
|
53
|
|
|
$response->refresh_token, |
|
54
|
|
|
$response->expires_in, |
|
55
|
|
|
$response->scope, |
|
56
|
|
|
microtime(true) |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function request(string $method, string $path, array $params = null, bool $body = true) |
|
61
|
|
|
{ |
|
62
|
|
|
$response = $this->client->request( |
|
63
|
|
|
$method, |
|
64
|
|
|
$this->credential->uri() . $path, |
|
65
|
|
|
[ |
|
66
|
|
|
'headers' => [ |
|
67
|
|
|
'cache-control' => 'no-cache', |
|
68
|
|
|
'authorization' => 'Bearer ' . $this->credential->accessToken(), |
|
69
|
|
|
], |
|
70
|
|
|
'json' => $params, |
|
71
|
|
|
] |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
if ($body) { |
|
75
|
|
|
return json_decode((string) $response->getBody()); |
|
76
|
|
|
} else { |
|
77
|
|
|
return json_decode((string) $response->getStatusCode()); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function bodyRequest(string $method, string $path, string $body) |
|
82
|
|
|
{ |
|
83
|
|
|
$response = $this->client->request( |
|
84
|
|
|
$method, |
|
85
|
|
|
$this->credential->uri() . $path, |
|
86
|
|
|
[ |
|
87
|
|
|
'headers' => [ |
|
88
|
|
|
'cache-control' => 'no-cache', |
|
89
|
|
|
'authorization' => 'Bearer ' . $this->credential->accessToken(), |
|
90
|
|
|
], |
|
91
|
|
|
'body' => $body, |
|
92
|
|
|
] |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
return json_decode((string) $response->getStatusCode()); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|