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

Client::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace AcquiaCloudApi\Connector;
4
5
use Psr\Http\Message\ResponseInterface;
6
7
/**
8
 * Class Client
9
 * @package AcquiaCloudApi\CloudApi
10
 */
11
class Client implements ClientInterface
12
{
13
    /** @var ConnectorInterface The API connector. */
14
    protected $connector;
15
16
    /** @var array Query strings to be applied to the request. */
17
    protected $query = [];
18
19
    /**
20
     * Client constructor.
21
     *
22
     * @param ConnectorInterface $connector
23
     */
24
    public function __construct(ConnectorInterface $connector)
25
    {
26
        $this->connector = $connector;
27
    }
28
29
    /**
30
     * Client factory method for instantiating .
31
     *
32
     * @param ConnectorInterface $connector
33
     *
34
     * @return static
35
     */
36
    public static function factory(ConnectorInterface $connector)
37
    {
38
        $client = new static(
39
            $connector
40
        );
41
42
        return $client;
43
    }
44
45
        /**
46
     * Takes parameters passed in, makes a request to the API, and processes the response.
47
     *
48
     * @param string $verb
49
     * @param string $path
50
     * @param array  $query
51
     * @param array  $options
52
     *
53
     * @return object|array|StreamInterface
0 ignored issues
show
Bug introduced by
The type AcquiaCloudApi\Connector\StreamInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
54
     */
55
    public function request(string $verb, string $path, array $options = [])
56
    {
57
        $options['query'] = $this->query;
58
59
        if (!empty($options['query']['filter']) && is_array($options['query']['filter'])) {
60
            // Default to an AND filter.
61
            $options['query']['filter'] = implode(',', $options['query']['filter']);
62
        }
63
        $response = $this->makeRequest($verb, $path, $options);
64
65
        return $this->processResponse($response);
66
    }
67
68
    /**
69
     * Makes a request to the API.
70
     *
71
     * @param string $verb
72
     * @param string $path
73
     * @param array  $query
74
     * @param array  $options
75
     * @return ResponseInterface
76
     */
77
    public function makeRequest(string $verb, string $path, array $options = [])
78
    {
79
        try {
80
            $response = $this->connector->sendRequest($verb, $path, $options);
0 ignored issues
show
Bug introduced by
The method sendRequest() does not exist on AcquiaCloudApi\Connector\ConnectorInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to AcquiaCloudApi\Connector\ConnectorInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
            /** @scrutinizer ignore-call */ 
81
            $response = $this->connector->sendRequest($verb, $path, $options);
Loading history...
81
        } catch (ClientException $e) {
0 ignored issues
show
Bug introduced by
The type AcquiaCloudApi\Connector\ClientException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
82
            print $e->getMessage();
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 object|array|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')) {
107
                if (is_array($object->message)) {
108
                    foreach ($object->message as $message) {
109
                        $output .= $message;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $output seems to be never defined.
Loading history...
110
                    }
111
                    throw new \Exception($output);
112
                } else {
113
                    throw new \Exception($object->message);
114
                }
115
            } else {
116
                $return = $object;
117
            }
118
        } else {
119
            $return = $body;
120
        }
121
122
        return $return;
123
    }
124
125
    /**
126
     * Get query from Client.
127
     *
128
     * @return array
129
     */
130
    public function getQuery()
131
    {
132
        return $this->query;
133
    }
134
135
    /**
136
     * Clear query.
137
     */
138
    public function clearQuery()
139
    {
140
        $this->query = [];
141
    }
142
143
    /**
144
     * Add a query parameter to filter results.
145
     *
146
     * @param string $name
147
     * @param string $value
148
     */
149
    public function addQuery($name, $value)
150
    {
151
        $this->query = array_merge_recursive($this->query, [$name => $value]);
152
    }
153
}
154