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

Client::processResponse()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 28
rs 8.8333
c 0
b 0
f 0
cc 7
nc 6
nop 1
1
<?php
2
3
namespace AcquiaCloudApi\Connector;
4
5
use Psr\Http\Message\ResponseInterface;
6
use GuzzleHttp\Exception\BadResponseException;
7
use Psr\Http\Message\StreamInterface;
8
9
/**
10
 * Class Client
11
 * @package AcquiaCloudApi\CloudApi
12
 */
13
class Client implements ClientInterface
14
{
15
    /** @var ConnectorInterface The API connector. */
16
    protected $connector;
17
18
    /** @var array Query strings to be applied to the request. */
19
    protected $query = [];
20
21
    /**
22
     * Client constructor.
23
     *
24
     * @param ConnectorInterface $connector
25
     */
26
    public function __construct(ConnectorInterface $connector)
27
    {
28
        $this->connector = $connector;
29
    }
30
31
    /**
32
     * Client factory method for instantiating .
33
     *
34
     * @param ConnectorInterface $connector
35
     *
36
     * @return static
37
     */
38
    public static function factory(ConnectorInterface $connector)
39
    {
40
        $client = new static(
41
            $connector
42
        );
43
44
        return $client;
45
    }
46
47
    /**
48
     * Takes parameters passed in, makes a request to the API, and processes the response.
49
     *
50
     * @param string $verb
51
     * @param string $path
52
     * @param array  $query
53
     * @param array  $options
54
     *
55
     * @return mixed
56
     */
57
    public function request(string $verb, string $path, array $options = [])
58
    {
59
        $options['query'] = $this->query;
60
61
        if (!empty($options['query']['filter']) && is_array($options['query']['filter'])) {
62
            // Default to an AND filter.
63
            $options['query']['filter'] = implode(',', $options['query']['filter']);
64
        }
65
        $response = $this->makeRequest($verb, $path, $options);
66
67
        return $this->processResponse($response);
68
    }
69
70
    /**
71
     * Makes a request to the API.
72
     *
73
     * @param string $verb
74
     * @param string $path
75
     * @param array  $query
76
     * @param array  $options
77
     *
78
     * @return ResponseInterface
79
     */
80
    public function makeRequest(string $verb, string $path, array $options = [])
81
    {
82
        try {
83
            $response = $this->connector->sendRequest($verb, $path, $options);
84
        } catch (BadResponseException $e) {
85
            $response = $e->getResponse();
86
        }
87
88
        return $response;
89
    }
90
91
    /**
92
     * Processes the returned response from the API.
93
     *
94
     * @param ResponseInterface $response
95
     * @return mixed
96
     * @throws \Exception
97
     */
98
    public function processResponse(ResponseInterface $response)
99
    {
100
101
        $body = $response->getBody();
102
103
        $object = json_decode($body);
104
        if (json_last_error() === JSON_ERROR_NONE) {
105
            // JSON is valid
106
            if (property_exists($object, '_embedded') && property_exists($object->_embedded, 'items')) {
107
                $return = $object->_embedded->items;
108
            } elseif (property_exists($object, 'error')) {
109
                if (is_array($object->message)) {
110
                    $output = '';
111
                    foreach ($object->message as $message) {
112
                        $output .= $message . PHP_EOL;
113
                    }
114
                    throw new \Exception($output);
115
                } else {
116
                    throw new \Exception($object->message);
117
                }
118
            } else {
119
                $return = $object;
120
            }
121
        } else {
122
            $return = $body;
123
        }
124
125
        return $return;
126
    }
127
128
    /**
129
     * Get query from Client.
130
     *
131
     * @return array
132
     */
133
    public function getQuery()
134
    {
135
        return $this->query;
136
    }
137
138
    /**
139
     * Clear query.
140
     */
141
    public function clearQuery()
142
    {
143
        $this->query = [];
144
    }
145
146
    /**
147
     * Add a query parameter to filter results.
148
     *
149
     * @param string $name
150
     * @param string $value
151
     */
152
    public function addQuery($name, $value)
153
    {
154
        $this->query = array_merge_recursive($this->query, [$name => $value]);
155
    }
156
}
157