Passed
Branch development (d7e242)
by Jonathan
06:53
created

UtilityTrait::request()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
ccs 12
cts 12
cp 1
rs 9.3142
cc 3
eloc 12
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
use TxTextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap;
20
use TxTextControl\ReportingCloud\PropertyMap\MergeSettings as MergeSettingsPropertyMap;
21
use TxTextControl\ReportingCloud\Validator\StaticValidator;
22
23
trait UtilityTrait
24
{
25
    abstract public function getClient();
26
27
    abstract public function getTest();
28
29
    abstract public function getVersion();
30
31
    /**
32
     * Request the URI with options
33
     *
34
     * @param string $method  HTTP method
35
     * @param string $uri     URI
36
     * @param array  $options Options
37
     *
38
     * @return mixed|null|\Psr\Http\Message\ResponseInterface
39
     *
40
     * @throws RuntimeException
41
     */
42 23
    protected function request($method, $uri, $options)
43
    {
44 23
        $ret = null;
45
46 23
        $client = $this->getClient();
47
48
        try {
49 22
            if ($this->getTest()) {
50 1
                $options[RequestOptions::QUERY]['test'] = StaticFilter::execute($this->getTest(), 'BooleanToString');
51 1
            }
52 22
            $ret = $client->request($method, $uri, $options);
53 22
        } catch (\Exception $exception) {
54
            // \GuzzleHttp\Exception\ClientException
55
            // \GuzzleHttp\Exception\ServerException
56 2
            $message = (string) $exception->getMessage();
57 2
            $code    = (int) $exception->getCode();
58 2
            throw new RuntimeException($message, $code);
59
        }
60
61 21
        return $ret;
62
    }
63
64
    /**
65
     * Construct URI with version number
66
     *
67
     * @param string $uri URI
68
     *
69
     * @return string
70
     */
71 23
    protected function uri($uri)
72
    {
73 23
        return sprintf('/%s%s', $this->getVersion(), $uri);
74
    }
75
76
    /**
77
     * Using the passed propertyMap, recursively build array
78
     *
79
     * @param array       $array       Array
80
     * @param PropertyMap $propertyMap PropertyMap
81
     *
82
     * @return array
83
     */
84 7
    protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap)
85
    {
86 7
        $ret = [];
87
88 7
        foreach ($array as $key => $value) {
89 7
            $map = $propertyMap->getMap();
90 7
            if (isset($map[$key])) {
91 7
                $key = $map[$key];
92 7
            }
93 7
            if (is_array($value)) {
94 3
                $value = $this->buildPropertyMapArray($value, $propertyMap);
95 3
            }
96 7
            $ret[$key] = $value;
97 7
        }
98
99 7
        return $ret;
100
    }
101
102
    /**
103
     * Using passed mergeSettings array, build array for backend
104
     *
105
     * @param array $array MergeSettings array
106
     *
107
     * @return array
108
     */
109 10
    protected function buildMergeSettingsArray(array $array)
110
    {
111 9
        $ret = [];
112
113 9
        $propertyMap = new MergeSettingsPropertyMap();
114
115 9
        foreach ($propertyMap->getMap() as $property => $key) {
116 9
            if (isset($array[$key])) {
117 9
                $value = $array[$key];
118 10
                if ('culture' == $key) {
119 1
                    StaticValidator::execute($value, 'Culture');
120
                }
121 9
                if ('remove_' == substr($key, 0, 7)) {
122 6
                    StaticValidator::execute($value, 'TypeBoolean');
123 4
                }
124 9
                if ('_date' == substr($key, -5)) {
125 9
                    StaticValidator::execute($value, 'Timestamp');
126 7
                    $value = StaticFilter::execute($value, 'TimestampToDateTime');
127 7
                }
128 9
                $ret[$property] = $value;
129 9
            }
130 9
        }
131
132 4
        return $ret;
133
    }
134
135
    /**
136
     * Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays)
137
     *
138
     * @param array $array FindAndReplaceData array
139
     *
140
     * @return array
141
     */
142 4
    protected function buildFindAndReplaceDataArray(array $array)
143
    {
144 4
        $ret = [];
145
146 4
        foreach ($array as $search => $replace) {
147 4
            array_push($ret, [
148 4
                $search,
149 4
                $replace,
150 4
            ]);
151 4
        }
152
153 4
        return $ret;
154
    }
155
}
156