Completed
Push — master ( a0dbc9...273313 )
by Jonathan
06:49 queued 46s
created

AbstractReportingCloud::getClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 16
cts 16
cp 1
rs 9.2
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 0
crap 2
1
<?php
2
3
/**
4
 * ReportingCloud PHP Wrapper
5
 *
6
 * Official wrapper (authored by Text Control GmbH, publisher of ReportingCloud) to access ReportingCloud in PHP.
7
 *
8
 * @link      http://www.reporting.cloud to learn more about ReportingCloud
9
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
10
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
11
 * @copyright © 2016 Text Control GmbH
12
 */
13
namespace TxTextControl\ReportingCloud;
14
15
use GuzzleHttp\Client;
16
use GuzzleHttp\RequestOptions;
17
use TxTextControl\ReportingCloud\Exception\RuntimeException;
18
use TxTextControl\ReportingCloud\Filter\BooleanToString as BooleanToStringFilter;
19
20
/**
21
 * Abstract ReportingCloud
22
 *
23
 * @package TxTextControl\ReportingCloud
24
 * @author  Jonathan Maron (@JonathanMaron)
25
 */
26
abstract class AbstractReportingCloud
27
{
28
    /**
29
     * Default date/time format of backend is 'ISO 8601'
30
     *
31
     * Note, last letter is 'P' and not 'O':
32
     *
33
     * O - Difference to Greenwich time (GMT) in hours (e.g. +0200)
34
     * P - Difference to Greenwich time (GMT) with colon between hours and minutes (e.g. +02:00)
35
     *
36
     * Backend uses the 'P' variant
37
     *
38
     * @const DEFAULT_DATE_FORMAT
39
     */
40
    const DEFAULT_DATE_FORMAT = 'Y-m-d\TH:i:sP';
41
42
    /**
43
     * Default time zone of backend
44
     *
45
     * @const DEFAULT_TIME_ZONE
46
     */
47
    const DEFAULT_TIME_ZONE = 'UTC';
48
49
    /**
50
     * Default base URI of backend
51
     *
52
     * @const DEFAULT_BASE_URI
53
     */
54
    const DEFAULT_BASE_URI = 'https://api.reporting.cloud';
55
56
    /**
57
     * Default version string of backend
58
     *
59
     * @const DEFAULT_VERSION
60
     */
61
    const DEFAULT_VERSION = 'v1';
62
63
    /**
64
     * Default timeout of backend in seconds
65
     *
66
     * @const DEFAULT_TIMEOUT
67
     */
68
    const DEFAULT_TIMEOUT = 120; // seconds
69
70
    /**
71
     * Default test flag of backend
72
     *
73
     * @const DEFAULT_TEST
74
     */
75
    const DEFAULT_TEST = false;
76
77
    /**
78
     * Default debug flag of REST client
79
     *
80
     * @const DEFAULT_DEBUG
81
     */
82
    const DEFAULT_DEBUG = false;
83
84
    /**
85
     * Backend username
86
     *
87
     * @var string
88
     */
89
    protected $username;
90
91
    /**
92
     * Backend password
93
     *
94
     * @var string
95
     */
96
    protected $password;
97
98
    /**
99
     * When true, backend prints "TEST MODE" water mark into output document, and API call does not count against quota
100
     *
101
     * @var boolean
102
     */
103
    protected $test;
104
105
    /**
106
     * Backend base URI
107
     *
108
     * @var string
109
     */
110
    protected $baseUri;
111
112
    /**
113
     * Backend version string
114
     *
115
     * @var string
116
     */
117
    protected $version;
118
119
    /**
120
     * Backend timeout in seconds
121
     *
122
     * @var integer
123
     */
124
    protected $timeout;
125
126
    /**
127
     * REST client to backend
128
     *
129
     * @var Client
130
     */
131
    protected $client;
132
133
    /**
134
     * Debug flag of REST client
135
     *
136
     * @var boolean
137
     */
138
    protected $debug;
139
140
    /**
141
     * AbstractReportingCloud constructor
142
     *
143
     * @param array $options
144
     */
145 59
    public function __construct($options = [])
146
    {
147 59
        if (array_key_exists('username', $options)) {
148 1
            $this->setUsername($options['username']);
149 1
        }
150
151 59
        if (array_key_exists('password', $options)) {
152 1
            $this->setPassword($options['password']);
153 1
        }
154
155 59
        if (array_key_exists('test', $options)) {
156 1
            $this->setTest($options['test']);
157 1
        }
158
159 59
        if (array_key_exists('base_uri', $options)) {
160 1
            $this->setBaseUri($options['base_uri']);
161 1
        }
162
163 59
        if (array_key_exists('version', $options)) {
164 1
            $this->setVersion($options['version']);
165 1
        }
166
167 59
        if (array_key_exists('timeout', $options)) {
168 1
            $this->setTimeout($options['timeout']);
169 1
        }
170
171 59
        if (array_key_exists('debug', $options)) {
172 1
            $this->setDebug($options['debug']);
173 1
        }
174 59
    }
175
176
    /**
177
     * Return the REST client of the backend web service
178
     *
179
     * @return \GuzzleHttp\Client
180
     */
181 18
    public function getClient()
182
    {
183 18
        if (null === $this->client) {
184
185 18
            $credentials   = sprintf('%s:%s', $this->getUsername(), $this->getPassword());
186 18
            $authorization = sprintf('Basic %s', base64_encode($credentials));
187
188 18
            $client = new Client([
189 18
                'base_uri'              => $this->getBaseUri(),
190 18
                RequestOptions::TIMEOUT => $this->getTimeout(),
191 18
                RequestOptions::DEBUG   => $this->getDebug(),
192 18
                RequestOptions::HEADERS => [
193 18
                    'Authorization' => $authorization,
194 18
                    'Content-Type'  => 'application/json',
195 18
                ],
196 18
            ]);
197
198 18
            $this->setClient($client);
199 18
        }
200
201 18
        return $this->client;
202
    }
203
204
    /**
205
     * Set the REST client of the backend web service
206
     *
207
     * @param Client $client REST client
208
     *
209
     * @return ReportingCloud
210
     */
211 18
    public function setClient(Client $client)
212
    {
213 18
        $this->client = $client;
214
215 18
        return $this;
216
    }
217
218
    /**
219
     * Return the base URI of the backend web service
220
     *
221
     * @return string
222
     */
223 22
    public function getBaseUri()
224
    {
225 22
        if (null === $this->baseUri) {
226 20
            $this->setBaseUri(self::DEFAULT_BASE_URI);
227 20
        }
228
229 22
        return $this->baseUri;
230
    }
231
232
    /**
233
     * Set the base URI of the backend web service
234
     *
235
     * @param string $baseUri Base URI
236
     *
237
     * @return ReportingCloud
238
     */
239 22
    public function setBaseUri($baseUri)
240
    {
241 22
        $this->baseUri = $baseUri;
242
243 22
        return $this;
244
    }
245
246
    /**
247
     * Get the timeout (in seconds) of the backend web service
248
     *
249
     * @return integer
250
     */
251 21
    public function getTimeout()
252
    {
253 21
        if (null === $this->timeout) {
254 19
            $this->setTimeout(self::DEFAULT_TIMEOUT);
255 19
        }
256
257 21
        return $this->timeout;
258
    }
259
260
    /**
261
     * Set the timeout (in seconds) of the backend web service
262
     *
263
     * @param integer $timeout Timeout
264
     *
265
     * @return ReportingCloud
266
     */
267 21
    public function setTimeout($timeout)
268
    {
269 21
        $this->timeout = (integer) $timeout;
270
271 21
        return $this;
272
    }
273
274
    /**
275
     * Return the username
276
     *
277
     * @return string
278
     */
279 21
    public function getUsername()
280
    {
281 21
        return $this->username;
282
    }
283
284
    /**
285
     * Set the username
286
     *
287
     * @param string $username Username
288
     *
289
     * @return ReportingCloud
290
     */
291 56
    public function setUsername($username)
292
    {
293 56
        $this->username = $username;
294
295 56
        return $this;
296
    }
297
298
    /**
299
     * Return the password
300
     *
301
     * @return string
302
     */
303 21
    public function getPassword()
304
    {
305 21
        return $this->password;
306
    }
307
308
    /**
309
     * Set the password
310
     *
311
     * @param string $password Password
312
     *
313
     * @return ReportingCloud
314
     */
315 56
    public function setPassword($password)
316
    {
317 56
        $this->password = $password;
318
319 56
        return $this;
320
    }
321
322
    /**
323
     * Return the test flag
324
     *
325
     * @return mixed
326
     */
327 19
    public function getTest()
328
    {
329 19
        if (null === $this->test) {
330 16
            $this->setTest(self::DEFAULT_TEST);
331 16
        }
332
333 19
        return $this->test;
334
    }
335
336
    /**
337
     * Set the test flag
338
     *
339
     * @param boolean $test Test flag
340
     *
341
     * @return ReportingCloud
342
     */
343 19
    public function setTest($test)
344
    {
345 19
        $this->test = (boolean) $test;
346
347 19
        return $this;
348
    }
349
350
    /**
351
     * Return the debug flag
352
     *
353
     * @return mixed
354
     */
355 21
    public function getDebug()
356
    {
357 21
        if (null === $this->debug) {
358 19
            $this->setDebug(self::DEFAULT_DEBUG);
359 19
        }
360
361 21
        return $this->debug;
362
    }
363
364
    /**
365
     * Set the debug flag
366
     *
367
     * @param boolean $debug Debug flag
368
     *
369
     * @return ReportingCloud
370
     */
371 21
    public function setDebug($debug)
372
    {
373 21
        $this->debug = (boolean) $debug;
374
375 21
        return $this;
376
    }
377
378
    /**
379
     * Get the version string of the backend web service
380
     *
381
     * @return string
382
     */
383 20
    public function getVersion()
384
    {
385 20
        if (null === $this->version) {
386 18
            $this->version = self::DEFAULT_VERSION;
387 18
        }
388
389 20
        return $this->version;
390
    }
391
392
    /**
393
     * Set the version string of the backend web service
394
     *
395
     * @param string $version Version string
396
     *
397
     * @return ReportingCloud
398
     */
399 2
    public function setVersion($version)
400
    {
401 2
        $this->version = $version;
402
403 2
        return $this;
404
    }
405
406
    /**
407
     * Request the URI with options
408
     *
409
     * @param string $method  HTTP method
410
     * @param string $uri     URI
411
     * @param array  $options Options
412
     *
413
     * @return mixed|null|\Psr\Http\Message\ResponseInterface
414
     *
415
     * @throws RuntimeException
416
     */
417 17
    protected function request($method, $uri, $options)
418
    {
419 17
        $ret = null;
420
421 17
        $client = $this->getClient();
422
423
        try {
424
425 17
            if (getenv('TRAVIS')) {
426
                $options['curl'][CURLOPT_SSLVERSION] = CURL_SSLVERSION_TLSv1_1;
427
            }
428
429 17
            if ($this->getTest()) {
430 1
                $filter = new BooleanToStringFilter();
431 1
                $options[RequestOptions::QUERY]['test'] = $filter->filter($this->getTest());
0 ignored issues
show
Documentation introduced by
$this->getTest() is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
432 1
            }
433
434 17
            $ret = $client->request($method, $uri, $options);
435
436 17
        } catch (\Exception $exception) {
437
438
            // \GuzzleHttp\Exception\ClientException
439
            // \GuzzleHttp\Exception\ServerException
440
441 1
            $message = (string)  $exception->getMessage();
442 1
            $code    = (integer) $exception->getCode();
443
444 1
            throw new RuntimeException($message, $code);
445
        }
446
447 16
        return $ret;
448
    }
449
450
    /**
451
     * Construct URI with version number
452
     *
453
     * @param string $uri URI
454
     *
455
     * @return string
456
     */
457 17
    protected function uri($uri)
458
    {
459 17
        return sprintf('/%s%s', $this->getVersion(), $uri);
460
    }
461
462
}