Completed
Push — master ( cc125e...c31978 )
by Tomas
04:15
created

ConsoleRequest::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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
    public function __construct(ApiHandlerInterface $handler)
16
    {
17
        $this->handler = $handler;
18
    }
19
20
    /**
21
     * Make request to API url
22
     *
23
     * @param string $url
24
     * @param string $method
25
     * @param array $values
26
     * @param string|null $token
27
     * @return ConsoleResponse
28
     */
29
    public function makeRequest($url, $method, $values, $token = null)
30
    {
31
        list($postFields, $getFields) = $this->processValues($values);
32
33
        if (count($getFields)) {
34
            $url = $url . '?' . implode('&', $getFields);
35
        }
36
37
        $startTime = microtime();
38
39
        $curl = curl_init();
40
        curl_setopt($curl, CURLOPT_URL, $url);
41
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
42
        curl_setopt($curl, CURLOPT_NOBODY, false);
43
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
44
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
45
        curl_setopt($curl, CURLOPT_VERBOSE, false);
46
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
47
        if (count($postFields)) {
48
            curl_setopt($curl, CURLOPT_POST, 1);
49
            curl_setopt($curl, CURLOPT_POSTFIELDS, implode('&', $postFields));
50
        }
51
52
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
53
        $headers = [];
54
        if ($token) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $token of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
55
            $headers = ['Authorization: Bearer ' . $token];
56
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
57
        }
58
59
        $consoleResponse = new ConsoleResponse(
60
            $url,
61
            $method,
62
            $postFields,
63
            $getFields,
64
            $headers
65
        );
66
67
        $responseBody = curl_exec($curl);
68
        $elapsed = intval((microtime() - $startTime) * 1000);
69
70
        $curlErrorNumber = curl_errno($curl);
71
        $curlError = curl_error($curl);
72
        if ($curlErrorNumber > 0) {
73
            $consoleResponse->logError($curlErrorNumber, $curlError, $elapsed);
74
        } else {
75
            $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
76
            $consoleResponse->logRequest($httpCode, $responseBody, $elapsed);
77
        }
78
79
        return $consoleResponse;
80
    }
81
82
    private function processValues($values)
83
    {
84
        $params = $this->handler->params();
85
86
        $postFields = [];
87
        $getFields = [];
88
89
        foreach ($values as $key => $value) {
90
            if (strstr($key, '___') !== false) {
91
                $parts = explode('___', $key);
92
                $key = $parts[0];
93
            }
94
95
            foreach ($params as $param) {
96
                $valueData = $this->processParam($param, $key, $value);
97
                if ($valueData == null) {
98
                    continue;
99
                }
100
101
                if ($param->getType() == InputParam::TYPE_POST) {
102
                    $postFields[] = $valueData;
103
                } else {
104
                    $getFields[] = $valueData;
105
                }
106
            }
107
        }
108
109
        return [$postFields, $getFields];
110
    }
111
112
    private function processParam($param, $key, $value)
113
    {
114
        if ($param->getKey() == $key) {
115
            if (!$value) {
116
                return null;
117
            }
118
            if ($param->isMulti()) {
119
                $valueKey = '';
120
                if (strstr($value, '=') !== false) {
121
                    $parts = explode('=', $value);
122
                    $valueKey = $parts[0];
123
                    $value = $parts[1];
124
                }
125
                $valueData = $key . "[$valueKey]=$value";
126
            } else {
127
                $valueData = "$key=$value";
128
            }
129
130
            return $valueData;
131
        }
132
        return null;
133
    }
134
}