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