1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AcquiaCloudApi\Connector; |
4
|
|
|
|
5
|
|
|
use AcquiaCloudApi\Connector\OktaProvider; |
6
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
7
|
|
|
use League\OAuth2\Client\Provider\GenericProvider; |
8
|
|
|
use League\OAuth2\Client\Token\AccessTokenInterface; |
9
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
10
|
|
|
use Psr\Http\Message\RequestInterface; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
13
|
|
|
use Symfony\Component\Filesystem\Path; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Connector |
17
|
|
|
* |
18
|
|
|
* @package AcquiaCloudApi\CloudApi |
19
|
|
|
*/ |
20
|
|
|
class Connector implements ConnectorInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string The base URI for Acquia Cloud API. |
24
|
|
|
*/ |
25
|
|
|
private string $baseUri; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string The URL access token for Accounts API. |
29
|
|
|
*/ |
30
|
|
|
private string $urlAccessToken; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string|null The Client ID. |
34
|
|
|
*/ |
35
|
|
|
private ?string $clientId; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var GenericProvider The OAuth 2.0 provider to use in communication. |
39
|
|
|
*/ |
40
|
|
|
protected AbstractProvider $provider; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var GuzzleClient The client used to make HTTP requests to the API. |
44
|
|
|
*/ |
45
|
|
|
protected GuzzleClient $client; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var AccessTokenInterface|null The generated OAuth 2.0 access token. |
49
|
|
|
*/ |
50
|
|
|
protected ?AccessTokenInterface $accessToken; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param array<string, string> $config |
54
|
|
|
*/ |
55
|
|
|
public function __construct(array $config, ?string $base_uri = null, ?string $url_access_token = null) |
56
|
|
|
{ |
57
|
|
|
$this->baseUri = ConnectorInterface::BASE_URI; |
58
|
|
|
if ($base_uri) { |
59
|
|
|
$this->baseUri = $base_uri; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->urlAccessToken = ConnectorInterface::URL_ACCESS_TOKEN; |
63
|
|
|
if ($url_access_token) { |
64
|
|
|
$this->urlAccessToken = $url_access_token; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->clientId = $config['key']; |
68
|
|
|
|
69
|
|
|
$this->provider = new OktaProvider( |
70
|
|
|
[ |
71
|
|
|
'clientId' => $config['key'], |
72
|
|
|
'clientSecret' => $config['secret'], |
73
|
|
|
'urlAuthorize' => '', |
74
|
|
|
'urlAccessToken' => $this->getUrlAccessToken(), |
75
|
|
|
'urlResourceOwnerDetails' => '', |
76
|
|
|
] |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$this->client = new GuzzleClient(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getBaseUri(): string |
83
|
|
|
{ |
84
|
|
|
return $this->baseUri; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getUrlAccessToken(): string |
88
|
|
|
{ |
89
|
|
|
return $this->urlAccessToken; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @inheritdoc |
94
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
95
|
|
|
*/ |
96
|
|
|
public function createRequest(string $verb, string $path): RequestInterface |
97
|
|
|
{ |
98
|
|
|
if (!isset($this->accessToken) || $this->accessToken->hasExpired()) { |
|
|
|
|
99
|
|
|
$xdgCacheHome = getenv('XDG_CACHE_HOME'); |
100
|
|
|
if (!$xdgCacheHome) { |
101
|
|
|
$xdgCacheHome = Path::join(Path::getHomeDirectory(), '.cache'); |
102
|
|
|
} |
103
|
|
|
$directory = Path::join($xdgCacheHome, 'acquia-php-sdk-v2'); |
104
|
|
|
/** @infection-ignore-all */ |
105
|
|
|
$cache = new FilesystemAdapter('cache', 300, $directory); |
106
|
|
|
$orgUuid = getenv('AH_ORGANIZATION_UUID'); |
107
|
|
|
$cacheKey = 'cloudapi-token-' . $this->clientId . $orgUuid; |
108
|
|
|
$accessToken = $cache->get($cacheKey, function () use ($orgUuid) { |
109
|
|
|
$options = []; |
110
|
|
|
if ($orgUuid) { |
111
|
|
|
$options['scope'] = 'organization:' . $orgUuid; |
112
|
|
|
} |
113
|
|
|
return $this->provider->getAccessToken('client_credentials', $options); |
114
|
|
|
}); |
115
|
|
|
|
116
|
|
|
$this->accessToken = $accessToken; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->provider->getAuthenticatedRequest( |
120
|
|
|
$verb, |
121
|
|
|
$this->getBaseUri() . $path, |
122
|
|
|
$this->accessToken |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @inheritdoc |
128
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
129
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
130
|
|
|
*/ |
131
|
|
|
public function sendRequest(string $verb, string $path, array $options): ResponseInterface |
132
|
|
|
{ |
133
|
|
|
$request = $this->createRequest($verb, $path); |
134
|
|
|
return $this->client->send($request, $options); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.