Completed
Pull Request — master (#34)
by Adam
03:25
created

Client::processResponse()   B

Complexity

Conditions 8
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.4444
c 0
b 0
f 0
cc 8
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  $options
53
     *
54
     * @return StreamInterface
55
     */
56
    public function request(string $verb, string $path, array $options = [])
57
    {
58
        $options['query'] = $this->query;
59
60
        if (!empty($options['query']['filter']) && is_array($options['query']['filter'])) {
61
            // Default to an AND filter.
62
            $options['query']['filter'] = implode(',', $options['query']['filter']);
63
        }
64
        $response = $this->makeRequest($verb, $path, $options);
65
66
        return $this->processResponse($response);
67
    }
68
69
    /**
70
     * Makes a request to the API.
71
     *
72
     * @param string $verb
73
     * @param string $path
74
     * @param array  $options
75
     *
76
     * @return ResponseInterface
77
     */
78
    public function makeRequest(string $verb, string $path, array $options = [])
79
    {
80
        try {
81
            $response = $this->connector->sendRequest($verb, $path, $options);
82
        } catch (BadResponseException $e) {
83
            $response = $e->getResponse();
84
        }
85
86
        return $response;
87
    }
88
89
    /**
90
     * Processes the returned response from the API.
91
     *
92
     * @param ResponseInterface $response
93
     * @return StreamInterface
94
     * @throws \Exception
95
     */
96
    public function processResponse(ResponseInterface $response)
97
    {
98
99
        $body = $response->getBody();
100
101
        $object = json_decode($body);
102
        if (json_last_error() === JSON_ERROR_NONE) {
103
            // JSON is valid
104
            if (property_exists($object, '_embedded') && property_exists($object->_embedded, 'items')) {
105
                $return = $object->_embedded->items;
106
            } elseif (property_exists($object, 'error') && property_exists($object, 'message')) {
107
                if (is_array($object->message)) {
108
                    $output = '';
109
                    foreach ($object->message as $message) {
110
                        $output .= $message . PHP_EOL;
111
                    }
112
                    throw new \Exception($output);
113
                } else {
114
                    throw new \Exception($object->message);
115
                }
116
            } else {
117
                $return = $object;
118
            }
119
        } else {
120
            $return = $body;
121
        }
122
123
        return $return;
124
    }
125
126
    /**
127
     * Get query from Client.
128
     *
129
     * @return array
130
     */
131
    public function getQuery()
132
    {
133
        return $this->query;
134
    }
135
136
    /**
137
     * Clear query.
138
     */
139
    public function clearQuery()
140
    {
141
        $this->query = [];
142
    }
143
144
    /**
145
     * Add a query parameter to filter results.
146
     *
147
     * @param string     $name
148
     * @param string|int $value
149
     */
150
    public function addQuery($name, $value)
151
    {
152
        $this->query = array_merge_recursive($this->query, [$name => $value]);
153
    }
154
}
155