CMQHttp   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 39
c 3
b 1
f 0
dl 0
loc 79
ccs 0
cts 59
cp 0
rs 10
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A set_method() 0 3 1
B send_request() 0 45 8
A is_keep_alive() 0 3 1
A set_connection_timeout() 0 3 1
A set_keep_alive() 0 3 1
1
<?php
2
3
namespace Freyo\LaravelQueueCMQ\Queue\Driver;
4
5
class CMQHttp
6
{
7
    private $connection_timeout;
8
    private $keep_alive;
9
    private $host;
10
11
    public function __construct($host, $connection_timeout = 10, $keep_alive = true)
12
    {
13
        $this->connection_timeout = $connection_timeout;
14
        $this->keep_alive = $keep_alive;
15
        $this->host = $host.'/v2/index.php';
16
        $this->curl = null;
0 ignored issues
show
Bug Best Practice introduced by
The property curl does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
    }
18
19
    public function set_method($method = 'POST')
20
    {
21
        $this->method = $method;
0 ignored issues
show
Bug Best Practice introduced by
The property method does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
22
    }
23
24
    public function set_connection_timeout($connection_timeout)
25
    {
26
        $this->connection_timeout = $connection_timeout;
27
    }
28
29
    public function is_keep_alive()
30
    {
31
        return $this->keep_alive;
32
    }
33
34
    public function set_keep_alive($keep_alive)
35
    {
36
        $this->keep_alive = $keep_alive;
37
    }
38
39
    public function send_request($req_inter, $userTimeout)
40
    {
41
        if (!$this->keep_alive) {
42
            $this->curl = curl_init();
0 ignored issues
show
Bug Best Practice introduced by
The property curl does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
        } else {
44
            if ($this->curl == null) {
45
                $this->curl = curl_init();
46
            }
47
        }
48
49
        if ($this->curl == null) {
50
            throw new CMQClientException('Curl init failed');
51
        }
52
53
        $url = $this->host;
54
        if ($req_inter->method == 'POST') {
55
            curl_setopt($this->curl, CURLOPT_POST, 1);
56
            curl_setopt($this->curl, CURLOPT_POSTFIELDS, $req_inter->data);
57
        } else {
58
            $url .= $req_inter->uri.'?'.$req_inter->data;
59
        }
60
61
        if (isset($req_inter->header)) {
62
            curl_setopt($this->curl, CURLOPT_HTTPHEADER, $req_inter->header);
63
        }
64
65
        curl_setopt($this->curl, CURLOPT_URL, $url);
66
        curl_setopt($this->curl, CURLOPT_TIMEOUT, $this->connection_timeout + $userTimeout);
67
68
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
69
70
        if (false !== strpos($url, 'https')) {
71
            // 证书
72
            // curl_setopt($ch,CURLOPT_CAINFO,"ca.crt");
73
            curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
74
            curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
75
        }
76
        $resultStr = curl_exec($this->curl);
77
        if (curl_errno($this->curl)) {
78
            throw new CMQClientNetworkException(curl_error($this->curl));
79
        }
80
        $info = curl_getinfo($this->curl);
81
        $resp_inter = new ResponseInternal($info['http_code'], null, $resultStr);
82
83
        return $resp_inter;
84
    }
85
}
86