Passed
Push — master ( 080658...f11f4c )
by Dane
02:17
created

Connector::getUrlAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace AcquiaCloudApi\Connector;
4
5
use League\OAuth2\Client\Provider\AbstractProvider;
6
use League\OAuth2\Client\Provider\GenericProvider;
7
use League\OAuth2\Client\Token\AccessTokenInterface;
8
use GuzzleHttp\Client as GuzzleClient;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
12
use Symfony\Component\Filesystem\Path;
13
14
/**
15
 * Class Connector
16
 *
17
 * @package AcquiaCloudApi\CloudApi
18
 */
19
class Connector implements ConnectorInterface
20
{
21
    /**
22
     * @var string The base URI for Acquia Cloud API.
23
     */
24
    private string $baseUri;
25
26
    /**
27
     * @var string The URL access token for Accounts API.
28
     */
29
    private string $urlAccessToken;
30
31
    /**
32
     * @var GenericProvider The OAuth 2.0 provider to use in communication.
33
     */
34
    protected AbstractProvider $provider;
35
36
    /**
37
     * @var GuzzleClient The client used to make HTTP requests to the API.
38
     */
39
    protected GuzzleClient $client;
40
41
    /**
42
     * @var AccessTokenInterface|null The generated OAuth 2.0 access token.
43
     */
44
    protected ?AccessTokenInterface $accessToken;
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public function __construct(array $config, string $base_uri = null, string $url_access_token = null)
50
    {
51
        $this->baseUri = ConnectorInterface::BASE_URI;
52
        if ($base_uri) {
53
            $this->baseUri = $base_uri;
54
        }
55
56
        $this->urlAccessToken = ConnectorInterface::URL_ACCESS_TOKEN;
57
        if ($url_access_token) {
58
            $this->urlAccessToken = $url_access_token;
59
        }
60
61
        $this->provider = new GenericProvider(
62
            [
63
            'clientId'                => $config['key'],
64
            'clientSecret'            => $config['secret'],
65
            'urlAuthorize'            => '',
66
            'urlAccessToken'          => $this->getUrlAccessToken(),
67
            'urlResourceOwnerDetails' => '',
68
            ]
69
        );
70
71
        $this->client = new GuzzleClient();
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getBaseUri(): string
78
    {
79
        return $this->baseUri;
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getUrlAccessToken(): string
86
    {
87
        return $this->urlAccessToken;
88
    }
89
90
    /**
91
     * @inheritdoc
92
     * @throws \Psr\Cache\InvalidArgumentException
93
     */
94
    public function createRequest(string $verb, string $path): RequestInterface
95
    {
96
        if (!isset($this->accessToken) || $this->accessToken->hasExpired()) {
0 ignored issues
show
Bug introduced by
The method hasExpired() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
        if (!isset($this->accessToken) || $this->accessToken->/** @scrutinizer ignore-call */ hasExpired()) {

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.

Loading history...
97
            $directory = Path::join(Path::getHomeDirectory(), '.acquia-php-sdk-v2');
98
            /** @infection-ignore-all */
99
            $cache = new FilesystemAdapter('cache', 300, $directory);
100
            $accessToken = $cache->get('cloudapi-token', function () {
101
                return $this->provider->getAccessToken('client_credentials');
102
            });
103
104
            $this->accessToken = $accessToken;
105
        }
106
107
        return $this->provider->getAuthenticatedRequest(
108
            $verb,
109
            $this->getBaseUri() . $path,
110
            $this->accessToken
111
        );
112
    }
113
114
    /**
115
     * @inheritdoc
116
     * @throws \GuzzleHttp\Exception\GuzzleException
117
     * @throws \Psr\Cache\InvalidArgumentException
118
     */
119
    public function sendRequest(string $verb, string $path, array $options): ResponseInterface
120
    {
121
        $request = $this->createRequest($verb, $path);
122
        return $this->client->send($request, $options);
123
    }
124
}
125