HttpClient::getErrorNo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * HttpClient
4
 *
5
 * @license MIT
6
 * @author zhangv
7
 */
8
namespace zhangv\wechat\pay\util;
9
10
class HttpClient{
11
12
	const GET = 'get',POST = 'post', DELETE = 'delete',PUT = 'put';
13
	private $instance = null;
14
	private $errNo = null;
15
	private $info = null;
16
	private $timeout = 1;
17
	private $retry = 1;
18 66
	private $tried = 0;
19 66
20 66
	public function __construct($timeout = 1,$retry = 1) {
21
		$this->timeout = $timeout;
22 66
		$this->retry = $retry;
23 66
		$this->initInstance();
24 66
	}
25 66
26 66
	public function initInstance(){
27 66
		if(!$this->instance) {
28 66
			$this->instance = curl_init();
29
			if($this->timeout < 1) {
30 66
				curl_setopt($this->instance, CURLOPT_NOSIGNAL,1);
0 ignored issues
show
Bug introduced by
It seems like $this->instance can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
				curl_setopt(/** @scrutinizer ignore-type */ $this->instance, CURLOPT_NOSIGNAL,1);
Loading history...
31
				curl_setopt($this->instance, CURLOPT_TIMEOUT_MS, $this->timeout * 1000);
32 2
			}else
33 2
				curl_setopt($this->instance, CURLOPT_TIMEOUT, intval($this->timeout));
34 2
			curl_setopt($this->instance, CURLOPT_RETURNTRANSFER, true);
35 2
			curl_setopt($this->instance, CURLOPT_FOLLOWLOCATION, true);
36 2
			curl_setopt($this->instance, CURLOPT_SSL_VERIFYPEER, false);
37 2
		}
38 2
	}
39 2
40 2
	public function get($url,$params = array(),$headers = array(),$opts = array()) {
41 2
		if (!$this->instance)	$this->initInstance($this->timeout);
0 ignored issues
show
Unused Code introduced by
The call to zhangv\wechat\pay\util\HttpClient::initInstance() has too many arguments starting with $this->timeout. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
		if (!$this->instance)	$this->/** @scrutinizer ignore-call */ initInstance($this->timeout);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
42 2
		if($params && count($params) > 0) $url .= '?' . http_build_query($params);
43
		curl_setopt($this->instance, CURLOPT_URL, $url);
44
		curl_setopt($this->instance, CURLOPT_HTTPGET, true);
45 1
		curl_setopt($this->instance, CURLOPT_HTTPHEADER, $headers);
46 1
		curl_setopt_array($this->instance,$opts);
47 1
		$result = $this->execute();
48 1
		curl_close($this->instance);
49 1
		$this->instance = null;
50 1
		return $result;
51 1
	}
52 1
53 1
	public function post($url, $params = array(),$headers = array(),$opts = array()) {
54 1
		if (!$this->instance)	$this->initInstance($this->timeout);
0 ignored issues
show
Unused Code introduced by
The call to zhangv\wechat\pay\util\HttpClient::initInstance() has too many arguments starting with $this->timeout. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
		if (!$this->instance)	$this->/** @scrutinizer ignore-call */ initInstance($this->timeout);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
55 1
		curl_setopt($this->instance, CURLOPT_URL, $url);
56
		curl_setopt($this->instance, CURLOPT_POST, true);
57
		curl_setopt($this->instance, CURLOPT_POSTFIELDS, $params);
58 3
		curl_setopt($this->instance, CURLOPT_HTTPHEADER, $headers);
59 3
		curl_setopt_array($this->instance,$opts);
60 3
		$result = $this->execute();
61 3
		curl_close($this->instance);
62 3
		$this->instance = null;
63
		return $result;
64
	}
65 3
66 3
	private function execute() {
67
		$this->tried = 0;
68
		do{
69
			$result = curl_exec($this->instance);
70
			$this->errNo = curl_errno($this->instance);
71
		}while(!$result && $this->errNo === CURLE_OPERATION_TIMEDOUT && $this->tried++ < $this->retry);
72
		$this->info = curl_getinfo($this->instance);
73
		return $result;
74
	}
75
76
	public function getInfo(){
77
		return $this->info;
78
	}
79
80
	public function getErrorNo(){
81
		return $this->errNo;
82
	}
83
84
	public function getTried(){
85
		return $this->tried;
86
	}
87
}