Passed
Push — master ( d9082a...b71230 )
by Alexey
03:46
created

AbstractApi   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 24.49%

Importance

Changes 0
Metric Value
wmc 22
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 195
rs 10
ccs 12
cts 49
cp 0.2449

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A sendGetRequest() 0 13 2
A sendPostRequest() 0 12 1
A getGetRequestData() 0 14 3
A getPostRequestData() 0 14 3
A getBaseUrl() 0 4 1
B setBaseUrl() 0 15 6
A isHttps() 0 4 1
A enableHttps() 0 7 1
A disableHttps() 0 7 1
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Service;
4
5
use Guzzle\Service\Client;
6
use Guzzle\Http\Message\Request as GuzzleRequest;
7
use Guzzle\Http\Message\Response as GuzzleResponse;
8
9
/**
10
 * @todo Refactor to Guzzle and DTO
11
 * @see https://github.com/misd-service-development/guzzle-bundle/blob/master/Resources/doc/serialization.md
12
 * @see https://github.com/misd-service-development/guzzle-bundle/blob/master/Resources/doc/clients.md
13
 * @see https://github.com/misd-service-development/guzzle-bundle/blob/master/Resources/doc/param_converter.md
14
 */
15
class AbstractApi
16
{
17
    /**
18
     * @var Client HTTP-client from Guzzle
19
     */
20
    protected $client;
21
22
    /**
23
     * @var bool Use HTTPS instead of HTTP
24
     */
25
    protected $useHttps;
26
27
    /**
28
     * @var string Authentication token for API
29
     */
30
    protected $authToken;
31
32
    /**
33
     * @var string CSRF-token for API
34
     */
35
    protected $csRfToken;
36
37
    /**
38
     * @param Client $httpClient HTTP-client from Guzzle
39
     * @param bool $https Use HTTPS instead of HTTP
40
     * @param string $baseUrl Base URL for API
41
     */
42 12
    public function __construct(Client $httpClient, $https = true, $baseUrl = null)
43
    {
44 12
        $this->client = $httpClient;
45 12
        $this->useHttps = ($https) ? true : false;
46
47 12
        if (null !== $baseUrl) {
48 12
            $this->setBaseUrl($baseUrl);
49
        }
50 12
    }
51
52
    /**
53
     * Make GET request and return Response object
54
     *
55
     * @param string $path Request path
56
     * @param array $parameters Key => Value array of query parameters
57
     * @return GuzzleResponse
58
     */
59
    public function sendGetRequest($path, array $parameters = [])
60
    {
61
        /** @var GuzzleRequest $request */
62
        $request = $this->client->get($path);
63
64
        $query = $request->getQuery();
65
66
        foreach ($parameters as $parameter => $value) {
67
            $query->set($parameter, $value);
68
        }
69
70
        return $request->send();
71
    }
72
73
    /**
74
     * Make POST request and return Response object
75
     *
76
     * @param string $path Request path
77
     * @param array $parameters Key => Value array of request data
78
     * @return GuzzleResponse
79
     */
80
    public function sendPostRequest($path, array $parameters = [])
81
    {
82
        // Cleaning POST parameters from potential @file injections
83
        array_walk($parameters, function (string &$value, string $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
            str_replace('@', '', $value);
85
        });
86
87
        /** @var GuzzleRequest $request */
88
        $request = $this->client->post($path, null, $parameters);
89
90
        return $request->send();
91
    }
92
93
    /**
94
     * Make GET request and return data from response
95
     *
96
     * @param string $path Path template
97
     * @param array $parameters Parameters array used to fill path template
98
     * @param bool $decodeJsonResponse Decode JSON or return plaintext
99
     * @param bool $decodeJsonToObjects Decode JSON objects to PHP objects instead of arrays
100
     * @return mixed
101
     */
102
    public function getGetRequestData($path, array $parameters = [], $decodeJsonResponse = false, $decodeJsonToObjects = false)
103
    {
104
        $response = $this->sendGetRequest($path, $parameters);
105
106
        if ($decodeJsonResponse) {
107
            if ($decodeJsonToObjects) {
108
                return json_decode($response->getBody(true));
109
            } else {
110
                return $response->json();
111
            }
112
        } else {
113
            return $response->getBody(true);
114
        }
115
    }
116
117
    /**
118
     * Make POST request and return data from response
119
     *
120
     * @param string $path Path template
121
     * @param array $parameters Parameters array used to fill path template
122
     * @param bool $decodeJsonResponse Decode JSON or return plaintext
123
     * @param bool $decodeJsonToObjects Decode JSON objects to PHP objects instead of arrays
124
     * @return mixed
125
     */
126
    public function getPostRequestData($path, array $parameters = [], $decodeJsonResponse = false, $decodeJsonToObjects = false)
127
    {
128
        $response = $this->sendPostRequest($path, $parameters);
129
130
        if ($decodeJsonResponse) {
131
            if ($decodeJsonToObjects) {
132
                return json_decode($response->getBody(true));
133
            } else {
134
                return $response->json();
135
            }
136
        } else {
137
            return $response->getBody(true);
138
        }
139
    }
140
141
    /**
142
     * Get HTTP client base URL
143
     *
144
     * @return string Base URL of client
145
     */
146
    public function getBaseUrl()
147
    {
148
        return $this->client->getBaseUrl();
149
    }
150
151
    /**
152
     * Set HTTP client base URL
153
     *
154
     * @param string $baseUrl Base URL of API
155
     * @param bool $useProtocol Do not change URL scheme (http/https) defined in $baseUrl
156
     * @return $this
157
     */
158 12
    public function setBaseUrl($baseUrl, $useProtocol = false)
159
    {
160
        // Overriding protocol
161 12
        if (!$useProtocol) {
162 12
            $baseUrl = str_replace(['http://', 'https://',], ($this->useHttps) ? 'https://' : 'http://', $baseUrl);
163
        }
164
        // Adding missing protocol
165 12
        if ((false === strpos(strtolower($baseUrl), 'http://')) && (false === strpos(strtolower($baseUrl), 'https://'))) {
166
            $baseUrl = (($this->useHttps) ? 'https://' : 'http://') . $baseUrl;
167
        }
168
169 12
        $this->client->setBaseUrl($baseUrl);
170
171 12
        return $this;
172
    }
173
174
    /**
175
     * Check if API service uses HTTPS
176
     *
177
     * @return bool
178
     */
179
    public function isHttps()
180
    {
181
        return $this->useHttps;
182
    }
183
184
    /**
185
     * Enable HTTPS
186
     *
187
     * @return $this
188
     */
189
    public function enableHttps()
190
    {
191
        $this->useHttps = true;
192
        $this->setBaseUrl($this->getBaseUrl());
0 ignored issues
show
Bug introduced by
It seems like $this->getBaseUrl() targeting Skobkin\Bundle\PointTool...stractApi::getBaseUrl() can also be of type object<Guzzle\Http\Url>; however, Skobkin\Bundle\PointTool...stractApi::setBaseUrl() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
193
194
        return $this;
195
    }
196
197
    /**
198
     * Disable HTTPS
199
     *
200
     * @return $this
201
     */
202
    public function disableHttps()
203
    {
204
        $this->useHttps = false;
205
        $this->setBaseUrl($this->getBaseUrl());
0 ignored issues
show
Bug introduced by
It seems like $this->getBaseUrl() targeting Skobkin\Bundle\PointTool...stractApi::getBaseUrl() can also be of type object<Guzzle\Http\Url>; however, Skobkin\Bundle\PointTool...stractApi::setBaseUrl() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
206
207
        return $this;
208
    }
209
}
210