1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AcquiaCloudApi\Connector; |
4
|
|
|
|
5
|
|
|
use League\OAuth2\Client\Provider\GenericProvider; |
6
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
7
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
8
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Http\Message\RequestInterface; |
11
|
|
|
use Psr\Http\Message\StreamInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Connector |
15
|
|
|
* @package AcquiaCloudApi\CloudApi |
16
|
|
|
*/ |
17
|
|
|
class Connector implements ConnectorInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string BASE_URI |
21
|
|
|
*/ |
22
|
|
|
const BASE_URI = 'https://cloud.acquia.com/api'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string URL_ACCESS_TOKEN |
26
|
|
|
*/ |
27
|
|
|
const URL_ACCESS_TOKEN = 'https://accounts.acquia.com/api/auth/oauth/token'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var GenericProvider The OAuth 2.0 provider to use in communication. |
31
|
|
|
*/ |
32
|
|
|
protected $provider; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string The generated OAuth 2.0 access token. |
36
|
|
|
*/ |
37
|
|
|
protected $accessToken; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The authenticated request. |
41
|
|
|
*/ |
42
|
|
|
public $request; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Connector constructor. |
46
|
|
|
* |
47
|
|
|
* @param array $config |
48
|
|
|
*/ |
49
|
|
|
public function __construct(array $config) |
50
|
|
|
{ |
51
|
|
|
$this->provider = new GenericProvider([ |
52
|
|
|
'clientId' => $config['key'], |
53
|
|
|
'clientSecret' => $config['secret'], |
54
|
|
|
'urlAuthorize' => '', |
55
|
|
|
'urlAccessToken' => self::URL_ACCESS_TOKEN, |
56
|
|
|
'urlResourceOwnerDetails' => '', |
57
|
|
|
]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Creates an authenticated Request instance. |
62
|
|
|
* |
63
|
|
|
* @param string $verb |
64
|
|
|
* @param string $path |
65
|
|
|
*/ |
66
|
|
|
public function createRequest($verb, $path) |
67
|
|
|
{ |
68
|
|
|
if (!isset($this->accessToken)) { |
69
|
|
|
$this->accessToken = $this->provider->getAccessToken('client_credentials'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->request = $this->provider->getAuthenticatedRequest( |
73
|
|
|
$verb, |
74
|
|
|
self::BASE_URI . $path, |
75
|
|
|
$this->accessToken |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Sends the request to the API using Guzzle. |
81
|
|
|
* |
82
|
|
|
* @param string $verb |
83
|
|
|
* @param string $path |
84
|
|
|
* @param array $options |
85
|
|
|
* |
86
|
|
|
* @return ResponseInterface |
87
|
|
|
*/ |
88
|
|
|
public function sendRequest($verb, $path, $options) |
89
|
|
|
{ |
90
|
|
|
$this->createRequest($verb, $path); |
91
|
|
|
$client = new GuzzleClient(); |
92
|
|
|
return $client->send($this->request, $options); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Gets the request. |
97
|
|
|
* @return RequestInterface |
98
|
|
|
*/ |
99
|
|
|
public function getRequest() |
100
|
|
|
{ |
101
|
|
|
return $this->request; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|