Passed
Pull Request — master (#285)
by Adam
03:11 queued 01:23
created

Connector   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 36
c 4
b 0
f 0
dl 0
loc 106
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 3
A getBaseUri() 0 3 1
A sendRequest() 0 4 1
A getUrlAccessToken() 0 3 1
A createRequest() 0 21 4
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\Cache\InvalidArgumentException;
10
use Psr\Http\Message\RequestInterface;
11
use Psr\Http\Message\ResponseInterface;
12
use RuntimeException;
13
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
14
use Symfony\Component\Filesystem\Path;
15
16
/**
17
 * Class Connector
18
 *
19
 * @package AcquiaCloudApi\CloudApi
20
 */
21
class Connector implements ConnectorInterface
22
{
23
    /**
24
     * @var string The base URI for Acquia Cloud API.
25
     */
26
    private string $baseUri;
27
28
    /**
29
     * @var string The URL access token for Accounts API.
30
     */
31
    private string $urlAccessToken;
32
33
    /**
34
     * @var GenericProvider The OAuth 2.0 provider to use in communication.
35
     */
36
    protected AbstractProvider $provider;
37
38
    /**
39
     * @var GuzzleClient The client used to make HTTP requests to the API.
40
     */
41
    protected GuzzleClient $client;
42
43
    /**
44
     * @var AccessTokenInterface|null The generated OAuth 2.0 access token.
45
     */
46
    protected ?AccessTokenInterface $accessToken;
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function __construct(array $config, string $base_uri = null, string $url_access_token = null)
52
    {
53
        $this->baseUri = ConnectorInterface::BASE_URI;
54
        if ($base_uri) {
55
            $this->baseUri = $base_uri;
56
        }
57
58
        $this->urlAccessToken = ConnectorInterface::URL_ACCESS_TOKEN;
59
        if ($url_access_token) {
60
            $this->urlAccessToken = $url_access_token;
61
        }
62
63
        $this->provider = new GenericProvider(
64
            [
65
            'clientId'                => $config['key'],
66
            'clientSecret'            => $config['secret'],
67
            'urlAuthorize'            => '',
68
            'urlAccessToken'          => $this->getUrlAccessToken(),
69
            'urlResourceOwnerDetails' => '',
70
            ]
71
        );
72
73
        $this->client = new GuzzleClient();
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function getBaseUri(): string
80
    {
81
        return $this->baseUri;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getUrlAccessToken(): string
88
    {
89
        return $this->urlAccessToken;
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95
    public function createRequest(string $verb, string $path): RequestInterface
96
    {
97
        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

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