Completed
Pull Request — master (#78)
by Michal
01:31
created

ConsoleRequest::processValues()   C

Complexity

Conditions 13
Paths 42

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 22.8029

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 19
cts 31
cp 0.6129
rs 6.6166
c 0
b 0
f 0
cc 13
nc 42
nop 1
crap 22.8029

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tomaj\NetteApi\Misc;
6
7
use Nette\Http\FileUpload;
8
use Tomaj\NetteApi\Handlers\ApiHandlerInterface;
9
use Tomaj\NetteApi\Params\InputParam;
10
use Tomaj\NetteApi\Params\ParamInterface;
11
12
class ConsoleRequest
13
{
14
    /** @var ApiHandlerInterface */
15
    private $handler;
16
17 3
    public function __construct(ApiHandlerInterface $handler)
18
    {
19 3
        $this->handler = $handler;
20 3
    }
21
22 3
    public function makeRequest(string $url, string $method, array $values, array $additionalValues = [], ?string $token = null): ConsoleResponse
23
    {
24 3
        list($postFields, $getFields, $cookieFields, $rawPost, $putFields) = $this->processValues($values);
25
26 3
        $postFields = array_merge($postFields, $additionalValues['postFields'] ?? []);
27 3
        $getFields = array_merge($getFields, $additionalValues['getFields'] ?? []);
28 3
        $cookieFields = array_merge($cookieFields, $additionalValues['cookieFields'] ?? []);
29 3
        $putFields = array_merge($putFields, $additionalValues['putFields'] ?? []);
30
31 3
        $postFields = $this->normalizeValues($postFields);
32 3
        $getFields = $this->normalizeValues($getFields);
33 3
        $putFields = $this->normalizeValues($putFields);
34
35 3
        if (count($getFields)) {
36 3
            $parts = [];
37 3
            foreach ($getFields as $key => $value) {
38 3
                $parts[] = "$key=$value";
39
            }
40 3
            $url = $url . '?' . implode('&', $parts);
41
        }
42
43 3
        $putRawPost = null;
44 3
        if (count($putFields)) {
45
            $parts = [];
46
            foreach ($putFields as $key => $value) {
47
                $parts[] = "$key=$value";
48
            }
49
            $putRawPost = implode('&', $parts);
50
        }
51
52 3
        $startTime = microtime(true);
53
54 3
        $curl = curl_init();
55 3
        curl_setopt($curl, CURLOPT_URL, $url);
56 3
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
57 3
        curl_setopt($curl, CURLOPT_NOBODY, false);
58 3
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
59 3
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
60 3
        curl_setopt($curl, CURLOPT_VERBOSE, false);
61 3
        curl_setopt($curl, CURLOPT_TIMEOUT, $additionalValues['timeout'] ?? 30);
62 3
        curl_setopt($curl, CURLOPT_HEADER, true);
63
64 3
        if (count($postFields) || $rawPost || $putRawPost !== null) {
65
            curl_setopt($curl, CURLOPT_POST, true);
66
            curl_setopt($curl, CURLOPT_POSTFIELDS, count($postFields) ? $postFields : ($rawPost ?: $putRawPost));
67
        }
68
69 3
        $headers = $additionalValues['headers'] ?? [];
70 3
        if (count($cookieFields)) {
71
            $parts = [];
72
            foreach ($cookieFields as $key => $value) {
73
                $parts[] = "$key=$value";
74
            }
75
            $headers[] = "Cookie: " . implode('&', $parts);
76
        }
77 3
        if ($token !== null && $token !== false) {
78
            $headers[] = 'Authorization: Bearer ' . $token;
79
        }
80 3
        if (count($headers)) {
81
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
82
        }
83
84 3
        $consoleResponse = new ConsoleResponse(
85 3
            $url,
86 2
            $method,
87 2
            $postFields,
88 2
            $getFields,
89 2
            $cookieFields,
90 2
            $headers,
91 2
            $rawPost
92
        );
93
94 3
        $response = curl_exec($curl);
95 3
        $elapsed = intval((microtime(true) - $startTime) * 1000);
96
97 3
        if ($response === false) {
98 3
            $response = '';
99
        }
100
101 3
        $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
102 3
        $responseHeaders = substr($response, 0, $headerSize);
103 3
        $responseBody = substr($response, $headerSize);
104
105 3
        $curlErrorNumber = curl_errno($curl);
106 3
        $curlError = curl_error($curl);
107 3
        if ($curlErrorNumber > 0) {
108 3
            $consoleResponse->logError($curlErrorNumber, $curlError, $elapsed);
109
        } else {
110
            $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
111
            $consoleResponse->logRequest($httpCode, $responseBody, $responseHeaders, $elapsed);
112
        }
113
114 3
        return $consoleResponse;
115
    }
116
117
    /**
118
     * Process given values to POST and GET fields
119
     *
120
     * @param array $values
121
     *
122
     * @return array
123
     */
124 3
    private function processValues(array $values): array
125
    {
126 3
        $params = $this->handler->params();
127
128 3
        $postFields = [];
129 3
        $rawPost = isset($values['post_raw']) ? $values['post_raw'] : null;
130 3
        $getFields = [];
131 3
        $putFields = [];
132 3
        $cookieFields = [];
133
134 3
        foreach ($values as $key => $value) {
135 3
            if (strstr($key, '___') !== false) {
136
                $parts = explode('___', $key);
137
                $key = $parts[0];
138
            }
139
140 3
            foreach ($params as $param) {
141 3
                $valueData = $this->processParam($param, $key, $value);
142 3
                if ($valueData === null) {
143 3
                    continue;
144
                }
145
146 3
                if ($param->isMulti()) {
147
                    if (in_array($param->getType(), [InputParam::TYPE_POST, InputParam::TYPE_FILE])) {
148
                        $postFields[$key][] = $valueData;
149
                    } elseif ($param->getType() === InputParam::TYPE_PUT) {
150
                        $putFields[$key][] = $valueData;
151
                    } elseif ($param->getType() === InputParam::TYPE_COOKIE) {
152
                        $cookieFields[$key][] = $valueData;
153
                    } else {
154
                        $getFields[$key][] = urlencode($valueData);
155
                    }
156
                } else {
157 3
                    if (in_array($param->getType(), [InputParam::TYPE_POST, InputParam::TYPE_FILE])) {
158
                        $postFields[$key] = $valueData;
159 3
                    } elseif ($param->getType() === InputParam::TYPE_PUT) {
160
                        $putFields[$key] = $valueData;
161 3
                    } elseif ($param->getType() === InputParam::TYPE_COOKIE) {
162
                        $cookieFields[$key] = $valueData;
163
                    } else {
164 3
                        $getFields[$key] = urlencode($valueData);
165
                    }
166
                }
167
            }
168
        }
169
170 3
        return [$postFields, $getFields, $cookieFields, $rawPost, $putFields];
171
    }
172
173
    /**
174
     * Process one param and returns value
175
     *
176
     * @param ParamInterface  $param   input param
177
     * @param string          $key     param key
178
     * @param mixed           $value   actual value from request
179
     *
180
     * @return mixed
181
     */
182 3
    private function processParam(ParamInterface $param, string $key, $value)
183
    {
184 3
        if ($param->getKey() === $key) {
185 3
            $valueData = $value;
186
187 3
            if ($param->getType() === InputParam::TYPE_FILE) {
188
                /** @var FileUpload $file */
189
                $file = $value;
190
                if ($file->isOk()) {
191
                    $valueData = curl_file_create($file->getTemporaryFile(), $file->getContentType(), $file->getName());
192
                } else {
193
                    $valueData = false;
194
                }
195 3
            } elseif ($param->getType() === InputParam::TYPE_POST_RAW) {
196
                $valueData = file_get_contents('php://input');
197
            }
198
199 3
            return $valueData;
200
        }
201 3
        return null;
202
    }
203
204 3
    private function normalizeValues(array $values): array
205
    {
206 3
        $result = [];
207 3
        foreach ($values as $key => $value) {
208 3
            if (!is_array($value)) {
209 3
                $result[$key] = $value;
210 3
                continue;
211
            }
212
213
            foreach ($value as $innerKey => $innerValue) {
214
                if ($innerValue !== '' && $innerValue !== null) {
215
                    $result[$key . "[" . $innerKey . "]"] = $innerValue;
216
                }
217
            }
218
        }
219 3
        return $result;
220
    }
221
}
222