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

Connector::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 8
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
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\BadResponseException;
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
     * Connector constructor.
40
     *
41
     * @param array $config
42
     */
43
    public function __construct(array $config)
44
    {
45
        $this->provider = new GenericProvider([
46
            'clientId'                => $config['key'],
47
            'clientSecret'            => $config['secret'],
48
            'urlAuthorize'            => '',
49
            'urlAccessToken'          => self::URL_ACCESS_TOKEN,
50
            'urlResourceOwnerDetails' => '',
51
        ]);
52
    }
53
54
    /**
55
     * Creates an authenticated Request instance.
56
     *
57
     * @param string $verb
58
     * @param string $path
59
     *
60
     * @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...
61
     */
62
    public function createRequest($verb, $path)
63
    {
64
        if (!isset($this->accessToken)) {
65
            $this->accessToken = $this->provider->getAccessToken('client_credentials');
66
        }
67
68
        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...
69
            $verb,
70
            self::BASE_URI . $path,
71
            $this->accessToken
72
        );
73
    }
74
75
    /**
76
     * Sends the request to the API using Guzzle.
77
     *
78
     * @param string $verb
79
     * @param string $path
80
     * @param array $options
81
     *
82
     * @return ResponseInterface
83
     */
84
    public function sendRequest($verb, $path, $options)
85
    {
86
        $request = $this->createRequest($verb, $path);
87
        $client = new GuzzleClient();
88
        return $client->send($request, $options);
89
    }
90
}
91