Passed
Push — master ( f9a48a...945dc8 )
by Mike
03:01
created

AbstractResponse   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 7
Bugs 2 Features 0
Metric Value
wmc 12
c 7
b 2
f 0
lcom 1
cbo 0
dl 0
loc 110
ccs 0
cts 34
cp 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A setCurlResponse() 0 6 2
A extractInfo() 0 8 2
A extractResponse() 0 4 1
A getStatus() 0 3 1
A getBody() 0 3 1
A getHeaders() 0 3 1
A getError() 0 3 1
A getInfo() 0 3 1
1
<?php
2
/**
3
 * ©[2016] SugarCRM Inc.  Licensed by SugarCRM under the Apache 2.0 license.
4
 */
5
6
namespace SugarAPI\SDK\Response\Abstracts;
7
8
use SugarAPI\SDK\Response\Interfaces\ResponseInterface;
9
10
abstract class AbstractResponse implements ResponseInterface {
11
12
    /**
13
     * The Curl Request Resource that was used when curl_exec was called
14
     * @var cURL resource handle
15
     */
16
    protected $CurlRequest;
17
18
    /**
19
     * Extracted headers from Curl Response
20
     * @var string
21
     */
22
    protected $headers;
23
24
    /**
25
     * Extracted body from Curl Response
26
     * @var mixed
27
     */
28
    protected $body;
29
30
    /**
31
     * The HTTP Status Code of Request
32
     * @var string
33
     */
34
    protected $status;
35
36
    /**
37
     * The last Curl Error that occurred
38
     * @var string|boolean - False when no Curl Error = 0
39
     */
40
    protected $error;
41
42
    /**
43
     * The cURL Resource information returned via curl_getinfo
44
     * @var array
45
     */
46
    protected $info;
47
48
    public function __construct($curlRequest,$curlResponse = NULL){
49
        $this->CurlRequest = $curlRequest;
50
        if ($curlResponse!==NULL){
51
            $this->setCurlResponse($curlResponse);
52
        }
53
    }
54
55
    public function setCurlResponse($curlResponse) {
56
        $this->extractInfo();
57
        if (!$this->getError()){
58
            $this->extractResponse($curlResponse);
59
        }
60
    }
61
62
    /**
63
     * Extract the information from the Curl Request via curl_getinfo
64
     * Setup the Status property to be equal to the http_code
65
     */
66
    protected function extractInfo(){
67
        $this->info = curl_getinfo($this->CurlRequest);
0 ignored issues
show
Documentation Bug introduced by
It seems like curl_getinfo($this->CurlRequest) of type * is incompatible with the declared type array of property $info.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
68
        $this->status = $this->info['http_code'];
69
        if (curl_errno($this->CurlRequest)!== CURLE_OK){
70
            $this->error = curl_error($this->CurlRequest);
71
        }
72
        $this->error = FALSE;
73
    }
74
75
    /**
76
     * Seperate the Headers and Body from the CurlResponse, and set the object properties
77
     * @param string $curlResponse
78
     */
79
    protected function extractResponse($curlResponse){
80
        $this->headers = substr($curlResponse, 0, $this->info['header_size']);
81
        $this->body = substr($curlResponse, $this->info['header_size']);
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87
    public function getStatus(){
88
        return $this->status;
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94
    public function getBody(){
95
        return $this->body;
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101
    public function getHeaders(){
102
        return $this->headers;
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public function getError(){
109
        return $this->error;
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115
    public function getInfo(){
116
        return $this->info;
117
    }
118
119
}