Completed
Push — master ( 99ef00...91de28 )
by Tomas
03:01
created

ConsoleRequest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 18
c 5
b 1
f 1
lcom 1
cbo 3
dl 0
loc 162
ccs 0
cts 97
cp 0
rs 10

5 Methods

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