Completed
Push — 1.0 ( b98a16...511929 )
by Vermeulen
05:02 queued 31s
created

CallCurl::curlOptionsAddDatas()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
rs 8.439
cc 6
eloc 18
nc 7
nop 1
1
<?php
2
3
/**
4
 * @version 1.0
5
 * @author Vermeulen Maxime (bulton-fr) <[email protected]>
6
 * @package bultonFr
7
 */
8
9
namespace bultonFr\CallCurl;
10
11
use \Exception;
12
use \bultonFr\CallCurl\Parser as ParserInterface;
13
use \bultonFr\CallCurl\Parser\DefaultParser;
14
15
class CallCurl
16
{
17
    /**
18
     * @var resource $curl Curl resource
19
     */
20
    protected $curl = null;
21
22
    /**
23
     * @var Object $parserInput PHP Class used for parse data before call
24
     */
25
    protected $parserOutput = null;
26
27
    /**
28
     * @var Object $parserOutput PHP Class used for parse data after call
29
     */
30
    protected $parserInput = null;
31
32
    /**
33
     * @var string $url The url to call with curl
34
     */
35
    protected $url = '';
36
37
    /**
38
     * @var mixed $datas Datas to send with curl
39
     */
40
    protected $datas = '';
41
42
    /**
43
     * @var string $httpMethod The HTTP method to use for curl call
44
     */
45
    protected $httpMethod = 'GET';
46
47
    /**
48
     * @var boolean $debug Ask to curl more datas to return for help debug 
49
     */
50
    protected $debug = false;
51
52
    /**
53
     * @var boolean $checkSSL If curl doing check SSL certificate
54
     */
55
    protected $checkSSL = true;
56
57
    /**
58
     * @var mixed $returnDatas : Datas return by curl call
59
     */
60
    protected $returnDatas;
61
62
    /**
63
     * @var boolean|array $curlCallInfos : Informations returned by curl
64
     */
65
    protected $curlCallInfos;
66
67
    /**
68
     * Constructor
69
     * 
70
     * Check dependancy
71
     * Initialise curl connection
72
     * Check parsers
73
     */
74
    public function __construct(&$parserOutput = null, &$parserInput = null)
75
    {
76
        //Check Dependancy
77
        $this->checkLib();
78
79
        //Initialise curl
80
        $this->curl = curl_init();
81
82
        //Check and initialise Parsers
83
        $this->checkParser($parserOutput);
84
        $this->checkParser($parserInput);
85
        
86
        $this->parserOutput = $parserOutput;
87
        $this->parserInput  = $parserInput;
88
    }
89
90
    /**
91
     * Check if all php librairie required is active
92
     * 
93
     * @throws Exception
94
     * 
95
     * @return void
96
     */
97
    protected function checkLib()
98
    {
99
        //Check curl php extension
100
        if(!function_exists('curl_init')) {
101
            throw new Exception('Please install or activate php-curl extension for use CallCurl plugin.');
102
        }
103
    }
104
105
    /**
106
     * Check if datas parser is a object and implement parser interface
107
     * 
108
     * @param mixed &$parser Parser to use
109
     * 
110
     * @throws Exception If error is find on parser definition
111
     */
112
    protected function checkParser(&$parser)
113
    {
114
        if(is_null($parser)) {
115
            $parser = new DefaultParser;
116
        }
117
118
        if(!is_object($parser)) {
119
            throw new Exception('Parser should be an object instance.');
120
        }
121
122
        if(!($parser instanceof ParserInterface)) {
123
            throw new Exception('Parser should implement \bultonFr\Parser interface.');
124
        }
125
    }
126
127
    /**
128
     * Set url to call
129
     * 
130
     * @param string $url : Url to call
131
     * 
132
     * @return \bultonFr\CallCurl : Current class instance
133
     * 
134
     * @throws Exception : If parameter is not a string
135
     */
136
    public function setUrl($url)
137
    {
138
        if(!is_string($url)) {
139
            throw new Exception('Url send to CallCurl should be a string variable');
140
        }
141
142
        $this->url = $url;
143
144
        return $this;
145
    }
146
147
    /**
148
     * Getter to url to call
149
     * 
150
     * @return string
151
     */
152
    public function getUrl()
153
    {
154
        return $this->url;
155
    }
156
157
    /**
158
     * Set datas to send with curl
159
     * 
160
     * @param mixed $datas
161
     * 
162
     * @return \bultonFr\CallCurl : Current class instance
163
     */
164
    public function setDatas($datas)
165
    {
166
        $this->datas = $datas;
167
168
        return $this;
169
    }
170
171
    /**
172
     * Getter to datas send with curl
173
     * 
174
     * @return mixed
175
     */
176
    public function getDatas()
177
    {
178
        return $this->datas;
179
    }
180
181
    /**
182
     * Set HTTP method to use to call
183
     * ex: GET, POST, PUT, DELETE etc
184
     * 
185
     * @param string $method : The HTTP method to use
186
     * 
187
     * @return \bultonFr\CallCurl : Current class instance
188
     * 
189
     * @throws Exception : If parameter is not a string
190
     */
191
    public function setHttpMethod($method)
192
    {
193
        if(!is_string($method)) {
194
            throw new Exception('HTTP method send to CallCurl should be a string variable');
195
        }
196
197
        $this->httpMethod = strtoupper($method);
198
199
        return $this;
200
    }
201
202
    /**
203
     * Set debug mode.
204
     * Curl call return more informations to help
205
     * 
206
     * @param boolean $debug : Debug mode status
207
     * 
208
     * @return \bultonFr\CallCurl : Current class instance
209
     * 
210
     * @throws Exception : If parameter is not a boolean
211
     */
212
    public function setDebug($debug)
213
    {
214
        if(!is_bool($debug)) {
215
            throw new Exception('Debug status send to CallCurl should be a boolean variable');
216
        }
217
218
        $this->debug = $debug;
219
220
        return $this;
221
    }
222
223
    /**
224
     * Set if curl check SSL certificate
225
     * 
226
     * @param type $checkSSL : check SSL status
227
     * 
228
     * @return \bultonFr\CallCurl : Current class instance
229
     * 
230
     * @throws Exception : If parameter is not a boolean
231
     */
232
    public function setCheckSSL($checkSSL)
233
    {
234
        if(!is_bool($checkSSL)) {
235
            throw new Exception('CheckSSL status send to CallCurl should be a boolean variable');
236
        }
237
238
        $this->checkSSL = $checkSSL;
239
240
        return $this;
241
    }
242
243
    /**
244
     * Get curl returned datas after parser formating
245
     * 
246
     * @return mixed
247
     */
248
    public function getCurlReturnDatas()
249
    {
250
        return $this->returnDatas;
251
    }
252
253
    /**
254
     * Get curl call information
255
     * 
256
     * @see http://php.net/manual/fr/function.curl-getinfo.php
257
     * 
258
     * @return boolean|array : false if error. else, array to information
259
     */
260
    public function getCurlCallInfos()
261
    {
262
        return $this->curlCallInfos;
263
    }
264
265
    /**
266
     * Run curl call stack
267
     * * Formating datas before call
268
     * * Generating curl options array
269
     * * Run call
270
     * * Formating datas returned by curl
271
     * 
272
     * @return mixed Datas returned by curl
273
     */
274
    public function runCall()
275
    {
276
        $this->parserOutput->preFormatDatas($this->datas);
277
278
        $options = $this->curlSetOptions();
279
        curl_setopt_array($this->curl, $options);
280
281
        $this->getFromCurl();
282
        $this->parserInput->formatCallReturn($this->returnDatas);
283
284
        return $this->returnDatas;
285
    }
286
287
    /**
288
     * Define curl options array
289
     * 
290
     * @return array : Options array to curl call
291
     */
292
    protected function curlSetOptions()
293
    {
294
        $options = [
295
            CURLOPT_URL            => $this->url,
296
            CURLOPT_RETURNTRANSFER => true
297
        ];
298
299
        $this->curlOptionsAddDatas($options);
300
301
        if($this->debug === true) {
302
            $options[CURLOPT_HEADER]      = true;
303
            $options[CURLOPT_VERBOSE]     = true;
304
            $options[CURLINFO_HEADER_OUT] = true;
305
        }
306
307
        if($this->checkSSL === false) {
308
            $options[CURLOPT_SSL_VERIFYHOST] = false;
309
            $options[CURLOPT_SSL_VERIFYPEER] = false;
310
        }
311
312
        return $options;
313
    }
314
    
315
    protected function curlOptionsAddDatas(&$options)
316
    {
317
        if(empty($this->datas)) {
318
            return;
319
        }
320
        
321
        if($this->httpMethod === 'GET') {
322
            
323
            if(is_string($this->datas)) {
324
                $datasUrl = urlencode($this->datas);
325
            } else {
326
                $datasUrl = http_build_query($this->datas);
327
            }
328
329
            if(strpos($this->url, '?') !== false) {
330
                $datasUrl = '&'.$datasUrl;
331
            }
332
            else {
333
                $datasUrl = '?'.$datasUrl;
334
            }
335
336
            $options[CURLOPT_URL] .= $datasUrl;
337
            return;
338
        }
339
        
340
        if($this->httpMethod === 'POST') {
341
            $options[CURLOPT_POST]       = true;
342
            $options[CURLOPT_POSTFIELDS] = $this->datas;
343
            
344
            return;
345
        }
346
        
347
        //@TODO : Other http status
348
    }
349
350
    /**
351
     * Run curl call and get informations about call
352
     * 
353
     * @return void
354
     */
355
    protected function getFromCurl()
356
    {
357
        $this->returnDatas   = curl_exec($this->curl);
358
        $this->curlCallInfos = curl_getinfo($this->curl);
359
360
        curl_close($this->curl);
361
    }
362
}
363