CurlClient::basicOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
namespace Metfan\RabbitSetup\Http;
3
4
5
6
/**
7
 * Curl client to use RabbitMQ API
8
 *
9
 * @author Ulrich
10
 * @package Metfan\RabbitSetup\Http
11
 */
12
class CurlClient implements ClientInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    private $host;
18
19
    /**
20
     * @var int
21
     */
22
    private $port;
23
24
    /**
25
     * @var string
26
     */
27
    private $user;
28
29
    /**
30
     * @var string
31
     */
32
    private $password;
33
34
    public function __construct($host, $port, $user, $password)
35
    {
36
        $this->host = $host;
37
        $this->port = $port;
38
        $this->user = $user;
39
        $this->password = $password;
40
    }
41
42
    /**
43
     * execute http request
44
     *
45
     * @param $method
46
     * @param $uri
47
     * @param array $parameters
48
     * @return mixed
49
     */
50
    public function query($method, $uri, array $parameters = array())
51
    {
52
        $options = array_merge(
53
            $this->buildUri($uri),
54
            $this->basicOptions(),
55
            $this->setMethodOption($method)
56
        );
57
58
        if (ClientInterface::METHOD_GET !==$method
59
            && ClientInterface::METHOD_DELETE !==$method
60
            && !empty($parameters)) {
61
            $options['CURLOPT_POSTFIELDS'] = json_encode($parameters);
62
        }
63
64
        $curl = $this->buildCurl($options);
65
        $rawResponse = curl_exec($curl);
66
        $curlInfo = curl_getinfo($curl);
67
        $curlError = curl_error($curl);
68
        $curlErno = curl_errno($curl);
69
        curl_close($curl);
70
71
        if (false === $rawResponse) {
72
            throw new \RuntimeException(sprintf('Curl Error: %s - ', $curlError, $curlErno));
73
        }
74
75
76
77
        if (!in_array($curlInfo['http_code'], array(200, 201, 204))) {
78
            throw new \RuntimeException(sprintf(
79
                'Receive code %d instead of 200, 201 or 204. Url: %s. Body: %s',
80
                $curlInfo['http_code'],
81
                $options['CURLOPT_URL'],
82
                $rawResponse
83
            ));
84
        }
85
86
        return $rawResponse;
87
    }
88
89
    /**
90
     * return basic options configuration for curl
91
     *
92
     * @return array
93
     */
94
    private function basicOptions()
95
    {
96
        return [
97
            'CURLOPT_HTTPHEADER' => array('Content-Type: application/json'),
98
            'CURLOPT_PORT' => $this->port,
99
            'CURLOPT_VERBOSE' => false,
100
            'CURLOPT_HEADER' => false,
101
            'CURLOPT_RETURNTRANSFER' => true,
102
            'CURLOPT_USERPWD' => sprintf('%s:%s', $this->user, $this->password)
103
        ];
104
    }
105
106
    /**
107
     * build uri
108
     *
109
     * @param $uri
110
     * @return array
111
     */
112
    private function buildUri($uri)
113
    {
114
        if ('/' !== substr($this->host, -1) && '/' !== substr($uri, 0, 1)) {
115
            $finalUri = $this->host.'/'.$uri;
116
        } else {
117
            $finalUri = $this->host.$uri;
118
        }
119
120
        return ['CURLOPT_URL' => $finalUri];
121
    }
122
123
    /**
124
     * return correct curl option
125
     * @param $method
126
     * @return array
127
     */
128
    private function setMethodOption($method)
129
    {
130
        if ($method == ClientInterface::METHOD_GET) {
131
            return ['CURLOPT_HTTPGET' => null];
132
        } elseif ($method == ClientInterface::METHOD_POST) {
133
            return ['CURLOPT_POST' => null];
134
        } elseif ($method == ClientInterface::METHOD_PUT) {
135
            return ['CURLOPT_CUSTOMREQUEST' => 'PUT'];
136
        } elseif ($method == ClientInterface::METHOD_DELETE) {
137
            return ['CURLOPT_CUSTOMREQUEST' => 'DELETE'];
138
        } elseif ($method == ClientInterface::METHOD_PATCH) {
139
            return ['CURLOPT_CUSTOMREQUEST' => 'PATCH'];
140
        } elseif ($method == ClientInterface::METHOD_LINK) {
141
            return ['CURLOPT_CUSTOMREQUEST' => 'LINK'];
142
        } elseif ($method == ClientInterface::METHOD_UNLINK) {
143
            return ['CURLOPT_CUSTOMREQUEST' => 'UNLINK'];
144
        } else {
145
            throw new \OutOfRangeException('Method '.$method.' currently not supported');
146
        }
147
    }
148
149
    /**
150
     * init curl with  all options
151
     *
152
     * @param array $options
153
     * @return resource
154
     */
155
    private function buildCurl(array $options)
156
    {
157
        $curl = curl_init();
158
        foreach ($options as $curlOption => $value) {
159
            curl_setopt($curl, constant($curlOption), $value);
160
        }
161
162
        return $curl;
163
    }
164
}
165