Completed
Pull Request — master (#21)
by Tomas
03:30
created

ConsoleRequest::processValues()   C

Complexity

Conditions 11
Paths 34

Size

Total Lines 43
Code Lines 29

Duplication

Lines 14
Ratio 32.56 %

Code Coverage

Tests 19
CRAP Score 19.1097

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 14
loc 43
ccs 19
cts 32
cp 0.5938
rs 5.2653
cc 11
eloc 29
nc 34
nop 1
crap 19.1097

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
namespace Tomaj\NetteApi\Misc;
4
5
use Tomaj\NetteApi\Handlers\ApiHandlerInterface;
6
use Tomaj\NetteApi\Params\InputParam;
7
8
class ConsoleRequest
9
{
10
    /**
11
     * @var ApiHandlerInterface
12
     */
13
    private $handler;
14
15
    /**
16
     * Create ConsoleRequest
17
     *
18
     * @param ApiHandlerInterface $handler
19
     */
20 3
    public function __construct(ApiHandlerInterface $handler)
21
    {
22 3
        $this->handler = $handler;
23 3
    }
24
25
    /**
26
     * Make request to API url
27
     *
28
     * @param string $url
29
     * @param string $method
30
     * @param array $values
31
     * @param string|null $token
32
     *
33
     * @return ConsoleResponse
34
     */
35 3
    public function makeRequest($url, $method, array $values, $token = null)
36
    {
37 3
        list($postFields, $getFields, $cookieFields, $rawPost) = $this->processValues($values);
38
39 3
        $postFields = $this->normalizeValues($postFields);
40 3
        $getFields = $this->normalizeValues($getFields);
41
42 3
        if (count($getFields)) {
43 3
            $parts = [];
44 3
            foreach ($getFields as $key => $value) {
45 3
                $parts[] = "$key=$value";
46 3
            }
47 3
            $url = $url . '?' . implode('&', $parts);
48 3
        }
49
50 3
        $startTime = microtime();
51
52 3
        $curl = curl_init();
53 3
        curl_setopt($curl, CURLOPT_URL, $url);
54 3
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
55 3
        curl_setopt($curl, CURLOPT_NOBODY, false);
56 3
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
57 3
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
58 3
        curl_setopt($curl, CURLOPT_VERBOSE, false);
59 3
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
60 3
        curl_setopt($curl, CURLOPT_HEADER, true);
61 3
        if (count($postFields)) {
62
            curl_setopt($curl, CURLOPT_POST, true);
63
            curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
64
        }
65 3
        if ($rawPost) {
66
            curl_setopt($curl, CURLOPT_POST, true);
67
            curl_setopt($curl, CURLOPT_POSTFIELDS, $rawPost);
68
        }
69 3
        if (count($cookieFields)) {
70
            $parts = [];
71
            foreach ($cookieFields as $key => $value) {
72
                $parts[] = "$key=$value";
73
            }
74
            curl_setopt($curl, CURLOPT_HTTPHEADER, ["Cookie: " . implode('&', $parts)]);
75
        }
76
77 3
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
78 3
        $headers = [];
79 3
        if ($token !== null && $token !== false) {
80
            $headers = ['Authorization: Bearer ' . $token];
81
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
82
        }
83
84 3
        $consoleResponse = new ConsoleResponse(
85 3
            $url,
86 3
            $method,
87 3
            $postFields,
88 3
            $getFields,
89 3
            $cookieFields,
90 3
            $headers,
91
            $rawPost
92 3
        );
93
94 3
        $response = curl_exec($curl);
95 3
        $elapsed = intval((microtime() - $startTime) * 1000);
96
97 3
        $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
98 3
        $responseHeaders = substr($response, 0, $headerSize);
99 3
        $responseBody = substr($response, $headerSize);
100
101 3
        $curlErrorNumber = curl_errno($curl);
102 3
        $curlError = curl_error($curl);
103 3
        if ($curlErrorNumber > 0) {
104 3
            $consoleResponse->logError($curlErrorNumber, $curlError, $elapsed);
105 3
        } else {
106
            $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
107
            $consoleResponse->logRequest($httpCode, $responseBody, $responseHeaders, $elapsed);
108
        }
109
110 3
        return $consoleResponse;
111
    }
112
113
    /**
114
     * Process given values to POST and GET fields
115
     *
116
     * @param array $values
117
     *
118
     * @return array
119
     */
120 3
    private function processValues(array $values)
121
    {
122 3
        $params = $this->handler->params();
123
124 3
        $postFields = [];
125 3
        $rawPost = isset($values['post_raw']) ? $values['post_raw'] : false;
126 3
        $getFields = [];
127 3
        $cookieFields = [];
128
129 3
        foreach ($values as $key => $value) {
130 3
            if (strstr($key, '___') !== false) {
131
                $parts = explode('___', $key);
132
                $key = $parts[0];
133
            }
134
135 3
            foreach ($params as $param) {
136 3
                $valueData = $this->processParam($param, $key, $value);
137 3
                if ($valueData === null) {
138 3
                    continue;
139
                }
140
141 3
                if ($param->isMulti()) {
142 View Code Duplication
                    if (in_array($param->getType(), [InputParam::TYPE_POST, InputParam::TYPE_FILE])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
                        $postFields[$key][] = $valueData;
144
                    } elseif ($param->getType() == InputParam::TYPE_COOKIE) {
145
                        $cookieFields[$key][] = $valueData;
146
                    } else {
147
                        $getFields[$key][] = $valueData;
148
                    }
149
                } else {
150 3 View Code Duplication
                    if (in_array($param->getType(), [InputParam::TYPE_POST, InputParam::TYPE_FILE])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
                        $postFields[$key] = $valueData;
152 3
                    } elseif ($param->getType() == InputParam::TYPE_COOKIE) {
153
                        $cookieFields[$key] = $valueData;
154
                    } else {
155 3
                        $getFields[$key] = $valueData;
156
                    }
157
                }
158 3
            }
159 3
        }
160
161 3
        return [$postFields, $getFields, $cookieFields, $rawPost];
162
    }
163
164
    /**
165
     * Process one param and returns value
166
     *
167
     * @param InputParam  $param   input param
168
     * @param string      $key     param key
169
     * @param string      $value   actual value from request
170
     *
171
     * @return string
172
     */
173 3
    private function processParam(InputParam $param, $key, $value)
174
    {
175 3
        if ($param->getKey() == $key) {
176 3
            if (!$value) {
177
                return null;
178
            }
179
180 3
            $valueData = $value;
181
182 3
            if ($param->getType() == InputParam::TYPE_FILE) {
183
                if ($value->isOk()) {
0 ignored issues
show
Bug introduced by
The method isOk cannot be called on $value (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
184
                    $valueData = curl_file_create($value->getTemporaryFile(), $value->getContentType(), $value->getName());
0 ignored issues
show
Bug introduced by
The method getTemporaryFile cannot be called on $value (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Bug introduced by
The method getContentType cannot be called on $value (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Bug introduced by
The method getName cannot be called on $value (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
185
                } else {
186
                    $valueData = false;
187
                }
188
            }
189
190 3
            if ($param->getType() == InputParam::TYPE_POST_RAW) {
191
                if (isset($HTTP_RAW_POST_DATA)) {
0 ignored issues
show
Bug introduced by
The variable $HTTP_RAW_POST_DATA seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
192
                    $valueData = $HTTP_RAW_POST_DATA;
193
                } else {
194
                    $valueData = file_get_contents('php://input');
195
                }
196
            }
197
198 3
            return $valueData;
199
        }
200 3
        return null;
201
    }
202
203
    /**
204
     * Normalize values array.
205
     *
206
     * @param $values
207
     * @return array
208
     */
209 3
    private function normalizeValues($values)
210
    {
211 3
        $result = [];
212 3
        foreach ($values as $key => $value) {
213 3
            if (is_array($value)) {
214
                $counter = 0;
215
                foreach ($value as $innerValue) {
216
                    if ($innerValue != null) {
217
                        $result[$key . "[".$counter++."]"] = $innerValue;
218
                    }
219
                }
220
            } else {
221 3
                $result[$key] = $value;
222
            }
223 3
        }
224 3
        return $result;
225
    }
226
}
227