|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AcquiaCloudApi\Connector; |
|
4
|
|
|
|
|
5
|
|
|
use League\OAuth2\Client\Provider\GenericProvider; |
|
6
|
|
|
use League\OAuth2\Client\Token\AccessTokenInterface; |
|
7
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use Psr\Http\Message\RequestInterface; |
|
10
|
|
|
use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
|
11
|
|
|
use Symfony\Contracts\Cache\ItemInterface; |
|
12
|
|
|
use Webmozart\PathUtil\Path; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Connector |
|
16
|
|
|
* |
|
17
|
|
|
* @package AcquiaCloudApi\CloudApi |
|
18
|
|
|
*/ |
|
19
|
|
|
class Connector implements ConnectorInterface |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string The base URI for Acquia Cloud API. |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $baseUri; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var GenericProvider The OAuth 2.0 provider to use in communication. |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $provider; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var GuzzleClient The client used to make HTTP requests to the API. |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $client; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var AccessTokenInterface The generated OAuth 2.0 access token. |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $accessToken; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(array $config, string $base_uri = null) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->baseUri = ConnectorInterface::BASE_URI; |
|
48
|
|
|
if ($base_uri) { |
|
49
|
|
|
$this->baseUri = $base_uri; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->provider = new GenericProvider( |
|
53
|
|
|
[ |
|
54
|
|
|
'clientId' => $config['key'], |
|
55
|
|
|
'clientSecret' => $config['secret'], |
|
56
|
|
|
'urlAuthorize' => '', |
|
57
|
|
|
'urlAccessToken' => self::URL_ACCESS_TOKEN, |
|
58
|
|
|
'urlResourceOwnerDetails' => '', |
|
59
|
|
|
] |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
$this->client = new GuzzleClient(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getBaseUri(): string |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->baseUri; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @inheritdoc |
|
75
|
|
|
*/ |
|
76
|
|
|
public function createRequest($verb, $path) |
|
77
|
|
|
{ |
|
78
|
|
|
if (!isset($this->accessToken) || $this->accessToken->hasExpired()) { |
|
79
|
|
|
$directory = sprintf('%s%s%s', Path::getHomeDirectory(), \DIRECTORY_SEPARATOR, '.acquia-php-sdk-v2'); |
|
80
|
|
|
$cache = new FilesystemAdapter('cache', 300, $directory); |
|
81
|
|
|
$accessToken = $cache->get('cloudapi-token', function () { |
|
82
|
|
|
return $this->provider->getAccessToken('client_credentials'); |
|
83
|
|
|
}); |
|
84
|
|
|
|
|
85
|
|
|
$this->accessToken = $accessToken; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $this->provider->getAuthenticatedRequest( |
|
89
|
|
|
$verb, |
|
90
|
|
|
$this->baseUri . $path, |
|
91
|
|
|
$this->accessToken |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @inheritdoc |
|
97
|
|
|
*/ |
|
98
|
|
|
public function sendRequest($verb, $path, $options) |
|
99
|
|
|
{ |
|
100
|
|
|
$request = $this->createRequest($verb, $path); |
|
101
|
|
|
return $this->client->send($request, $options); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|