Passed
Push — master ( 19386d...c4ef65 )
by Jonathan
08:41
created

UtilityTrait::buildMergeSettingsArray()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6.0045

Importance

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