Passed
Push — master ( a979ef...44e6f2 )
by Jonathan
09:57 queued 01:06
created

UtilityTrait::request()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 6
nop 3
crap 3
1
<?php
2
3
/**
4
 * ReportingCloud PHP Wrapper
5
 *
6
 * PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH.
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 © 2018 Text Control GmbH
12
 */
13
14
namespace TxTextControl\ReportingCloud;
15
16
use GuzzleHttp\RequestOptions;
17
use TxTextControl\ReportingCloud\Exception\RuntimeException;
18
use TxTextControl\ReportingCloud\Filter\StaticFilter;
19
20
/**
21
 * Trait UtilityTrait
22
 *
23
 * @package TxTextControl\ReportingCloud
24
 * @author  Jonathan Maron (@JonathanMaron)
25
 */
26
trait UtilityTrait
27
{
28
    /**
29
     * Abstract Methods
30
     * -----------------------------------------------------------------------------------------------------------------
31
     */
32
33
    /**
34
     * Return the REST client of the backend web service
35
     *
36
     * @return \GuzzleHttp\Client
37
     */
38
    abstract public function getClient();
39
40
    /**
41
     * Return the test flag
42
     *
43
     * @return mixed
44
     */
45
    abstract public function getTest();
46
47
    /**
48
     * Get the version string of the backend web service
49
     *
50
     * @return string
51
     */
52
    abstract public function getVersion();
53
54
    /**
55
     * Utility Methods
56
     * -----------------------------------------------------------------------------------------------------------------
57
     */
58
59
    /**
60
     * Request the URI with options
61
     *
62
     * @param string $method  HTTP method
63
     * @param string $uri     URI
64
     * @param array  $options Options
65
     *
66
     * @return mixed|null|\Psr\Http\Message\ResponseInterface
67
     *
68
     * @throws RuntimeException
69
     */
70 46
    protected function request($method, $uri, $options)
71
    {
72 46
        $client = $this->getClient();
73
74
        try {
75 44
            if ($this->getTest()) {
76 2
                $options[RequestOptions::QUERY]['test'] = StaticFilter::execute($this->getTest(), 'BooleanToString');
77 1
            }
78 44
            $ret = $client->request($method, $uri, $options);
79 24
        } catch (\Exception $exception) {
80
            // \GuzzleHttp\Exception\ClientException
81
            // \GuzzleHttp\Exception\ServerException
82 4
            $message = (string) $exception->getMessage();
83 4
            $code    = (int) $exception->getCode();
84 4
            throw new RuntimeException($message, $code);
85
        }
86
87 42
        return $ret;
88
    }
89
90
    /**
91
     * Construct URI with version number
92
     *
93
     * @param string $uri URI
94
     *
95
     * @return string
96
     */
97 46
    protected function uri($uri)
98
    {
99 46
        return sprintf('/%s%s', $this->getVersion(), $uri);
100
    }
101
}
102