CurlRestClient::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 3
Metric Value
c 5
b 0
f 3
dl 0
loc 7
rs 9.4286
cc 1
eloc 5
nc 1
nop 4
1
<?php
2
3
namespace RestClient;
4
5
/**
6
 * Class CurlRestClient
7
 * @package RestClient
8
 */
9
class CurlRestClient extends RestClient
10
{
11
    /**
12
     * @var $url null
13
     */
14
    private $url;
15
    /**
16
     * @var $header array
17
     */
18
    private $header;
19
    /**
20
     * @var $auth array
21
     */
22
    private $auth;
23
24
    /**
25
     * @var $options array
26
     */
27
    private $options = array();
28
29
    private $curl;
30
31
    /**
32
     * @param null $url
33
     * @param array $header
34
     * @param array $auth
35
     * @param array $options
36
     */
37
    public function __construct($url = null, $header = array(), $auth = array(), $options = array())
38
    {
39
        $this->url = $url;
40
        $this->header = $header;
41
        $this->auth = $auth;
42
        $this->options = $options;
43
    }
44
45
    /**
46
     * function setHeader
47
     * @param array $header
48
     */
49
    public function setHeader($header)
50
    {
51
        $this->header = $header;
52
    }
53
54
    /**
55
     * function setAuth
56
     * @param array $auth
57
     */
58
    public function setAuth($auth)
59
    {
60
        $this->auth = $auth;
61
    }
62
63
    /**
64
     * function setOptions
65
     * @param array $options
66
     */
67
    public function setOptions(array $options)
68
    {
69
        $this->options = $options;
70
    }
71
72
    /**
73
     * @param string $url
74
     * @param string $method
75
     * @param array $header
76
     * @param array $data
77
     * @param array $auth
78
     * @param bool $forceInit
79
     * @return mixed
80
     * @throws \Exception
81
     */
82
    public function executeQuery($url, $method = 'GET', $header = array(), $data = array(), $auth = array(), $forceInit = false)
83
    {
84
85
        if (true === $forceInit) {
86
            $this->close(); // close previous channel
87
        }
88
89
        if (null === $this->curl) {
90
            $this->curl = curl_init();
91
        }
92
93
        if ($method == 'GET')
94
            $url = $url . '?' . http_build_query($data);
95
96
        curl_setopt($this->curl, CURLOPT_URL, $url);
97
        curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header);
98
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
99
100
        if (!empty($auth)) {
101
            curl_setopt($this->curl, CURLOPT_HTTPAUTH, $auth['CURLOPT_HTTPAUTH']);
102
            curl_setopt($this->curl, CURLOPT_USERPWD, $auth['username'] . ':' . $auth['password']);
103
        }
104
105
        if (is_array($this->options)) {
106
            foreach ($this->options as $option => $value) {
107
                curl_setopt($this->curl, $option, $value);
108
            }
109
        }
110
111
        if ($method == 'POST') {
112
            curl_setopt($this->curl, CURLOPT_POST, true);
113 View Code Duplication
            if (!empty($data)) {
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...
114
                curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($data));
115
            } else {
116
                curl_setopt($this->curl, CURLOPT_POSTFIELDS, array());
117
            }
118
        } elseif ($method == 'PUT') {
119
            curl_setopt($this->curl, CURLOPT_PUT, true);
120 View Code Duplication
            if (!empty($data)) {
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...
121
                curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($data));
122
            } else {
123
                curl_setopt($this->curl, CURLOPT_POSTFIELDS, array());
124
            }
125
        } elseif ($method == 'DELETE') {
126
            curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "DELETE");
127 View Code Duplication
            if (!empty($data)) {
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...
128
                curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($data));
129
            } else {
130
                curl_setopt($this->curl, CURLOPT_POSTFIELDS, array());
131
            }
132
        }
133
134
        $content = curl_exec($this->curl);
135
136
        if (FALSE === $content)
137
            throw new \Exception(curl_error($this->curl), curl_errno($this->curl));
138
139
        return $content;
140
    }
141
142
    /**
143
     * function call
144
     * @param string $method
145
     * @param string $segment
146
     * @param array $data
147
     * @return array
148
     */
149
    private function call($method, $segment, $data = array())
150
    {
151
        return $this->executeQuery($this->url . '/' . $segment, $method, $this->header, $data, $this->auth);
152
    }
153
154
    /**
155
     * function get
156
     * @param string $segment
157
     * @param array $data
158
     * @return array
159
     */
160
    public function get($segment, $data = array())
161
    {
162
        return $this->call('GET', $segment, $data);
163
    }
164
165
    /**
166
     * function post
167
     * @param string $segment
168
     * @param array $data
169
     * @return array
170
     */
171
    public function post($segment, $data)
172
    {
173
        return $this->call('POST', $segment, $data);
174
    }
175
176
    /**
177
     * function put
178
     * @param string $segment
179
     * @param array $data
180
     * @return array
181
     */
182
    public function put($segment, $data)
183
    {
184
        return $this->call('PUT', $segment, $data);
185
    }
186
187
    /**
188
     * function delete
189
     * @param string $segment
190
     * @param array $data
191
     * @return array
192
     */
193
    public function delete($segment, $data)
194
    {
195
        return $this->call('DELETE', $segment, $data);
196
    }
197
198
    /**
199
     * function getName
200
     * @return string
201
     */
202
    public function getName()
203
    {
204
        return 'curl';
205
    }
206
207
    public function close()
208
    {
209
        if (null !== $this->curl) {
210
            curl_close($this->curl);
211
        }
212
213
        $this->curl = null;
214
    }
215
216
217
}
218