Passed
Push — master ( 50d8c4...2d01cb )
by Mike
03:46
created

AbstractResponse::getHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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 1
    public function __construct($curlRequest,$curlResponse = NULL){
49 1
        $this->CurlRequest = $curlRequest;
50 1
        if ($curlResponse!==NULL){
51 1
            $this->setCurlResponse($curlResponse);
52 1
        }
53 1
    }
54
55 1
    public function setCurlResponse($curlResponse) {
56 1
        $this->extractInfo();
57 1
        if (!$this->error){
58 1
            $this->extractResponse($curlResponse);
59 1
        }
60 1
    }
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 2
    protected function extractInfo(){
67 2
        $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 2
        $this->status = $this->info['http_code'];
69 2
        if (curl_errno($this->CurlRequest)!== CURLE_OK){
70 1
            $this->error = curl_error($this->CurlRequest);
71 1
        }else {
72 1
            $this->error = FALSE;
73
        }
74 2
    }
75
76
    /**
77
     * Seperate the Headers and Body from the CurlResponse, and set the object properties
78
     * @param string $curlResponse
79
     */
80 1
    protected function extractResponse($curlResponse){
81 1
        $this->headers = substr($curlResponse, 0, $this->info['header_size']);
82 1
        $this->body = substr($curlResponse, $this->info['header_size']);
83 1
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88 1
    public function getStatus(){
89 1
        return $this->status;
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 1
    public function getBody(){
96 1
        return $this->body;
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102 1
    public function getHeaders(){
103 1
        return $this->headers;
104
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109 2
    public function getError(){
110 2
        return $this->error;
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116 1
    public function getInfo(){
117 1
        return $this->info;
118
    }
119
120
}