Completed
Pull Request — master (#34)
by Adam
02:18
created

Connector::createRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 2
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\ClientException;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\StreamInterface;
11
12
/**
13
 * Class Connector
14
 * @package AcquiaCloudApi\CloudApi
15
 */
16
class Connector implements ConnectorInterface
17
{
18
    /**
19
     * @var string BASE_URI
20
     */
21
    const BASE_URI = 'https://cloud.acquia.com/api';
22
23
    /**
24
     * @var string URL_ACCESS_TOKEN
25
     */
26
    const URL_ACCESS_TOKEN = 'https://accounts.acquia.com/api/auth/oauth/token';
27
28
    /**
29
     * @var GenericProvider The OAuth 2.0 provider to use in communication.
30
     */
31
    protected $provider;
32
33
    /**
34
     * @var string The generated OAuth 2.0 access token.
35
     */
36
    protected $accessToken;
37
38
    /**
39
     * @var GuzzleClient The Guzzle Client to communicate with the API.
40
     */
41
    protected $client;
42
43
    /**
44
     * @var array Injected configuration values.
45
     */
46
    protected $config;
47
48
    /**
49
     * Connector constructor.
50
     *
51
     * @param array $config
52
     */
53
    public function __construct(array $config)
54
    {
55
        $this->provider = new GenericProvider([
56
            'clientId'                => $config['key'],
57
            'clientSecret'            => $config['secret'],
58
            'urlAuthorize'            => '',
59
            'urlAccessToken'          => self::URL_ACCESS_TOKEN,
60
            'urlResourceOwnerDetails' => '',
61
        ]);
62
    }
63
64
    /**
65
     * Creates an authenticated Request instance.
66
     *
67
     * @param string $verb
68
     * @param string $path
69
     *
70
     * @return RequestInterface
0 ignored issues
show
Bug introduced by
The type AcquiaCloudApi\Connector\RequestInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
71
     */
72
    public function createRequest($verb, $path)
73
    {
74
        if (!isset($this->accessToken)) {
75
            $this->accessToken = $this->provider->getAccessToken('client_credentials');
76
        }
77
78
        return $this->provider->getAuthenticatedRequest(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->provider->...th, $this->accessToken) returns the type GuzzleHttp\Psr7\Request which is incompatible with the documented return type AcquiaCloudApi\Connector\RequestInterface.
Loading history...
79
            $verb,
80
            self::BASE_URI . $path,
81
            $this->accessToken
82
        );
83
    }
84
85
    /**
86
     * Sends the request to the API using Guzzle.
87
     *
88
     * @param string $verb
89
     * @param string $path
90
     * @param array $options
91
     *
92
     * @return ResponseInterface
93
     */
94
    public function sendRequest($verb, $path, $options)
95
    {
96
        $request = $this->createRequest($verb, $path);
97
        $client = new GuzzleClient();
98
        return $client->send($request, $options);
99
    }
100
}
101