Completed
Push — master ( 7982de...94a0c5 )
by Adam
02:06
created

Connector::processResponse()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 5
nop 1
dl 0
loc 27
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace AcquiaCloudApi\CloudApi;
4
5
use Acquia\Hmac\Guzzle\HmacAuthMiddleware;
6
use Acquia\Hmac\Key;
7
use GuzzleHttp\Client as GuzzleClient;
8
use GuzzleHttp\Exception\ClientException;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\StreamInterface;
11
use GuzzleHttp\HandlerStack;
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 GuzzleClient The Guzzle Client to communicate with the API.
26
     */
27
    protected $client;
28
29
    /**
30
     * @var array Injected configuration values.
31
     */
32
    protected $config;
33
34
    /**
35
     * Connector constructor.
36
     *
37
     * @param array $config
38
     */
39
    public function __construct($config)
40
    {
41
        $key = new Key($config['key'], $config['secret']);
42
        $middleware = new HmacAuthMiddleware($key);
43
        $stack = HandlerStack::create();
44
        $stack->push($middleware);
45
46
        $this->client = new GuzzleClient([
47
            'handler' => $stack,
48
        ]);
49
    }
50
51
    /**
52
     * Takes parameters passed in, makes a request to the API, and processes the response.
53
     *
54
     * @param string $verb
55
     * @param string $path
56
     * @param array  $query
57
     * @param array  $options
58
     *
59
     * @return object|array|StreamInterface
60
     */
61
    public function request(string $verb, string $path, array $query = [], array $options = [])
62
    {
63
        $options['query'] = $query;
64
65
        if (!empty($options['query']['filter']) && is_array($options['query']['filter'])) {
66
            // Default to an AND filter.
67
            $options['query']['filter'] = implode(',', $options['query']['filter']);
68
        }
69
        $response = $this->makeRequest($verb, $path, $query, $options);
70
71
        return $this->processResponse($response);
72
    }
73
74
    /**
75
     * Makes a request to the API.
76
     *
77
     * @param string $verb
78
     * @param string $path
79
     * @param array  $query
80
     * @param array  $options
81
     * @return ResponseInterface
82
     */
83
    public function makeRequest(string $verb, string $path, array $query = [], array $options = [])
84
    {
85
        try {
86
            $response = $this->client->$verb(self::BASE_URI . $path, $options);
87
        } catch (ClientException $e) {
88
            print $e->getMessage();
89
            $response = $e->getResponse();
90
        }
91
92
        return $response;
93
    }
94
95
    /**
96
     * Processes the returned response from the API.
97
     *
98
     * @param ResponseInterface $response
99
     * @return object|array|StreamInterface
100
     * @throws \Exception
101
     */
102
    public function processResponse(ResponseInterface $response)
103
    {
104
105
        $body = $response->getBody();
106
107
        $object = json_decode($body);
108
        if (json_last_error() === JSON_ERROR_NONE) {
109
            // JSON is valid
110
            if (property_exists($object, '_embedded') && property_exists($object->_embedded, 'items')) {
111
                $return = $object->_embedded->items;
112
            } elseif (property_exists($object, 'error')) {
113
                if (is_object($object->message)) {
114
                    foreach ($object->message as $message) {
115
                        $output .= $message;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $output seems to be never defined.
Loading history...
116
                    }
117
                } else {
118
                    $output = $object->message;
119
                }
120
                throw new \Exception($output);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $output does not seem to be defined for all execution paths leading up to this point.
Loading history...
121
            } else {
122
                $return = $object;
123
            }
124
        } else {
125
            $return = $body;
126
        }
127
128
        return $return;
129
    }
130
}
131