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

UtilityTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 74
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A request() 0 18 3
A uri() 0 3 1
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