Completed
Push — master ( 8ccdf5...29f953 )
by Tomas
37:47
created

ConsoleRequest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 71.76%

Importance

Changes 6
Bugs 1 Features 2
Metric Value
wmc 18
c 6
b 1
f 2
lcom 1
cbo 3
dl 0
loc 167
ccs 61
cts 85
cp 0.7176
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B makeRequest() 0 57 5
B processValues() 0 29 6
A processParam() 0 17 4
A processMultiParam() 0 10 2
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) = $this->processValues($values);
38
39 3
        if (count($getFields)) {
40 3
            $url = $url . '?' . implode('&', $getFields);
41 3
        }
42
43 3
        $startTime = microtime();
44
45 3
        $curl = curl_init();
46 3
        curl_setopt($curl, CURLOPT_URL, $url);
47 3
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
48 3
        curl_setopt($curl, CURLOPT_NOBODY, false);
49 3
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
50 3
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
51 3
        curl_setopt($curl, CURLOPT_VERBOSE, false);
52 3
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
53 3
        curl_setopt($curl, CURLOPT_HEADER, 1);
54 3
        if (count($postFields)) {
55
            curl_setopt($curl, CURLOPT_POST, 1);
56
            curl_setopt($curl, CURLOPT_POSTFIELDS, implode('&', $postFields));
57
        }
58
59 3
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
60 3
        $headers = [];
61 3
        if ($token !== null) {
62
            $headers = ['Authorization: Bearer ' . $token];
63
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
64
        }
65
66 3
        $consoleResponse = new ConsoleResponse(
67 3
            $url,
68 3
            $method,
69 3
            $postFields,
70 3
            $getFields,
71
            $headers
72 3
        );
73
74 3
        $response = curl_exec($curl);
75 3
        $elapsed = intval((microtime() - $startTime) * 1000);
76
77 3
        $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
78 3
        $responseHeaders = substr($response, 0, $headerSize);
79 3
        $responseBody = substr($response, $headerSize);
80
81 3
        $curlErrorNumber = curl_errno($curl);
82 3
        $curlError = curl_error($curl);
83 3
        if ($curlErrorNumber > 0) {
84 3
            $consoleResponse->logError($curlErrorNumber, $curlError, $elapsed);
85 3
        } else {
86
            $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
87
            $consoleResponse->logRequest($httpCode, $responseBody, $responseHeaders, $elapsed);
88
        }
89
90 3
        return $consoleResponse;
91
    }
92
93
    /**
94
     * Process given values to POST and GET fields
95
     *
96
     * @param array $values
97
     *
98
     * @return array
99
     */
100 3
    private function processValues(array $values)
101
    {
102 3
        $params = $this->handler->params();
103
104 3
        $postFields = [];
105 3
        $getFields = [];
106
107 3
        foreach ($values as $key => $value) {
108 3
            if (strstr($key, '___') !== false) {
109
                $parts = explode('___', $key);
110
                $key = $parts[0];
111
            }
112
113 3
            foreach ($params as $param) {
114 3
                $valueData = $this->processParam($param, $key, $value);
115 3
                if ($valueData == null) {
116 3
                    continue;
117
                }
118
119 3
                if ($param->getType() == InputParam::TYPE_POST) {
120
                    $postFields[] = $valueData;
121
                } else {
122 3
                    $getFields[] = $valueData;
123
                }
124 3
            }
125 3
        }
126
127 3
        return [$postFields, $getFields];
128
    }
129
130
    /**
131
     * Process one param and returns value
132
     *
133
     * @param InputParam  $param   input param
134
     * @param string      $key     param key
135
     * @param string      $value   actual value from request
136
     *
137
     * @return string
138
     */
139 3
    private function processParam(InputParam $param, $key, $value)
140
    {
141 3
        if ($param->getKey() == $key) {
142 3
            if (!$value) {
143
                return null;
144
            }
145
146 3
            if ($param->isMulti()) {
147
                $valueData = $this->processMultiParam($key, $value);
148
            } else {
149 3
                $valueData = "$key=$value";
150
            }
151
152 3
            return $valueData;
153
        }
154 3
        return null;
155
    }
156
157
    /**
158
     * Process multi param
159
     *
160
     * @param string  $key
161
     * @param string  $value
162
     * @return string
163
     */
164
    private function processMultiParam($key, $value)
165
    {
166
        $valueKey = '';
167
        if (strstr($value, '=') !== false) {
168
            $parts = explode('=', $value);
169
            $valueKey = $parts[0];
170
            $value = $parts[1];
171
        }
172
        return $key . "[$valueKey]=$value";
173
    }
174
}
175