Passed
Pull Request — master (#34)
by Adam
01:53
created

Connector   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 22
dl 0
loc 66
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A sendRequest() 0 5 1
A createRequest() 0 10 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
    public function createRequest($verb, $path)
65
    {
66
        if (! isset($this->accessToken)) {
67
            $this->accessToken = $this->provider->getAccessToken('client_credentials');
68
        }
69
70
        return $this->provider->getAuthenticatedRequest(
71
            $verb,
72
            self::BASE_URI . $path,
73
            $this->accessToken
74
        );
75
    }
76
77
    public function sendRequest($verb, $path, $options)
78
    {
79
        $request = $this->createRequest($verb, $path);
80
        $client = new GuzzleClient();
81
        return $client->send($request, $options);
82
    }
83
}
84