Passed
Push — master ( 23fcc1...e399e4 )
by Adam
04:08 queued 11s
created

Connector::sendRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 3
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\RequestInterface;
11
use Psr\Http\Message\StreamInterface;
12
13
/**
14
 * Class Connector
15
 * @package AcquiaCloudApi\CloudApi
16
 */
17
class Connector implements ConnectorInterface
18
{
19
    /**
20
     * @var string BASE_URI
21
     */
22
    const BASE_URI = 'https://cloud.acquia.com/api';
23
24
    /**
25
     * @var string URL_ACCESS_TOKEN
26
     */
27
    const URL_ACCESS_TOKEN = 'https://accounts.acquia.com/api/auth/oauth/token';
28
29
    /**
30
     * @var GenericProvider The OAuth 2.0 provider to use in communication.
31
     */
32
    protected $provider;
33
34
    /**
35
     * @var string The generated OAuth 2.0 access token.
36
     */
37
    protected $accessToken;
38
39
    /**
40
     * Connector constructor.
41
     *
42
     * @param array $config
43
     */
44
    public function __construct(array $config)
45
    {
46
        $this->provider = new GenericProvider([
47
            'clientId'                => $config['key'],
48
            'clientSecret'            => $config['secret'],
49
            'urlAuthorize'            => '',
50
            'urlAccessToken'          => self::URL_ACCESS_TOKEN,
51
            'urlResourceOwnerDetails' => '',
52
        ]);
53
    }
54
55
    /**
56
     * Creates an authenticated Request instance.
57
     *
58
     * @param string $verb
59
     * @param string $path
60
     *
61
     * @return RequestInterface
62
     */
63
    public function createRequest($verb, $path)
64
    {
65
        if (!isset($this->accessToken)) {
66
            $this->accessToken = $this->provider->getAccessToken('client_credentials');
67
        }
68
69
        return $this->provider->getAuthenticatedRequest(
70
            $verb,
71
            self::BASE_URI . $path,
72
            $this->accessToken
73
        );
74
    }
75
76
    /**
77
     * Sends the request to the API using Guzzle.
78
     *
79
     * @param string $verb
80
     * @param string $path
81
     * @param array $options
82
     *
83
     * @return ResponseInterface
84
     */
85
    public function sendRequest($verb, $path, $options)
86
    {
87
        $request = $this->createRequest($verb, $path);
88
        $client = new GuzzleClient();
89
        return $client->send($request, $options);
90
    }
91
}
92