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

AbstractRequest   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 235
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 8
Bugs 3 Features 0
Metric Value
wmc 25
c 8
b 3
f 0
lcom 2
cbo 0
dl 0
loc 235
ccs 0
cts 75
cp 0
rs 10

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A __destruct() 0 5 2
A setURL() 0 5 1
A getURL() 0 3 1
A addHeader() 0 5 1
A setHeaders() 0 9 3
A getHeaders() 0 3 1
A setBody() 0 5 1
A getBody() 0 3 1
A getCurlObject() 0 3 1
A setOption() 0 3 1
A send() 0 6 1
A setType() 0 3 1
A getType() 0 3 1
A reset() 0 7 2
A start() 0 5 1
A close() 0 6 1
A getCurlStatus() 0 3 1
A getCurlResponse() 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\Request\Abstracts;
7
8
use SugarAPI\SDK\Request\Interfaces\RequestInterface;
9
10
abstract class AbstractRequest implements RequestInterface {
11
12
    const STATUS_INIT = 'initialized';
13
    const STATUS_SENT = 'sent';
14
    const STATUS_CLOSED = 'closed';
15
16
    /**
17
     * The HTTP Request Type
18
     * @var string
19
     */
20
    protected static $_TYPE = '';
21
22
    /**
23
     * The Default Curl Options
24
     * @var array
25
     */
26
    protected static $_DEFAULT_OPTIONS = array(
27
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
28
        CURLOPT_HEADER => TRUE,
29
        CURLOPT_SSL_VERIFYPEER => 0,
30
        CURLOPT_RETURNTRANSFER => 1,
31
        CURLOPT_FOLLOWLOCATION => 0,
32
        CURLOPT_USERAGENT => 'SugarAPI-SDK-PHP'
33
    );
34
35
    /**
36
     * The default HTTP Headers to be added to Curl Request
37
     * @var array
38
     */
39
    protected static $_DEFAULT_HEADERS = array();
40
41
    /**
42
     * The Curl Resource used to actually send data to Sugar API
43
     * @var - Curl Resource
44
     */
45
    protected $CurlResponse;
46
47
    /**
48
     * The raw response from curl_exec
49
     * @var - Curl Response
50
     */
51
    protected $CurlRequest;
52
53
    /**
54
     * List of Headers for Request
55
     * @var array
56
     */
57
    protected $headers = array();
58
59
    /**
60
     * The body of the request or payload. JSON Encoded
61
     * @var string
62
     */
63
    protected $body = '';
64
65
    /**
66
     * The URL the Request is sent to
67
     * @var string
68
     */
69
    protected $url = '';
70
71
    /**
72
     * @var null
73
     */
74
    protected $status = NULL;
75
76
    /**
77
     * The Request Type
78
     * @var
79
     */
80
    protected $type;
81
82
    public function __construct($url = NULL){
83
        $this->start();
84
        if (!empty($url)){
85
            $this->setURL($url);
86
        }
87
        $this->setType();
88
        foreach (static::$_DEFAULT_OPTIONS as $option => $value){
89
            $this->setOption($option, $value);
90
        }
91
    }
92
93
    /**
94
     * Always make sure to destroy Curl Resource
95
     */
96
    public function __destruct() {
97
        if ($this->status!==self::STATUS_CLOSED){
98
            curl_close($this->CurlRequest);
99
        }
100
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105
    public function setURL($url){
106
        $this->url = $url;
107
        $this->setOption(CURLOPT_URL, $this->url);
108
        return $this;
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114
    public function getURL(){
115
        return $this->url;
116
    }
117
118
    /**
119
     * @inheritdoc
120
     */
121
    public function addHeader($name, $value){
122
        $token = $name.": ".$value;
123
        $this->headers[] = $token;
124
        return $this;
125
    }
126
127
    /**
128
     * @inheritdoc
129
     */
130
    public function setHeaders(array $array = array()){
131
        if (count($array)>0){
132
            foreach ($array as $key => $value){
133
                $this->addHeader($key, $value);
134
            }
135
        }
136
        $this->setOption(CURLOPT_HTTPHEADER, $this->headers);
137
        return $this;
138
    }
139
140
    /**
141
     * @inheritdoc
142
     */
143
    public function getHeaders(){
144
        return $this->headers;
145
    }
146
147
    /**
148
     * @inheritdoc
149
     */
150
    public function setBody($body){
151
        $this->body = $body;
152
        $this->setOption(CURLOPT_POSTFIELDS, $this->body);
153
        return $this;
154
    }
155
156
    /**
157
     * @inheritdoc
158
     */
159
    public function getBody(){
160
        return $this->body;
161
    }
162
163
    /**
164
     * @inheritdoc
165
     */
166
    public function getCurlObject(){
167
        return $this->CurlRequest;
168
    }
169
170
    /**
171
     * @inheritdoc
172
     */
173
    public function setOption($option, $value){
174
        curl_setopt($this->CurlRequest, $option, $value);
175
    }
176
177
    /**
178
     * @inheritdoc
179
     */
180
    public function send(){
181
        $this->setHeaders();
182
        $this->CurlResponse = curl_exec($this->CurlRequest);
183
        $this->status = self::STATUS_SENT;
0 ignored issues
show
Documentation Bug introduced by
It seems like self::STATUS_SENT of type string is incompatible with the declared type null of property $status.

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...
184
        return $this;
185
    }
186
187
    /**
188
     * @inheritdoc
189
     */
190
    public function getCurlResponse(){
191
        return $this->CurlResponse;
192
    }
193
194
    /**
195
     * Set the Type on the Request
196
     */
197
    protected function setType(){
198
        $this->type = static::$_TYPE;
199
    }
200
201
    /**
202
     * @inheritdoc
203
     */
204
    public function getType(){
205
        return $this->type;
206
    }
207
208
    /**
209
     * @inheritdoc
210
     */
211
    public function reset(){
212
        if (is_object($this->CurlRequest)){
213
            $this->close();
214
        }
215
        $this->start();
216
        return $this;
217
    }
218
219
    /**
220
     * @inheritdoc
221
     */
222
    public function start(){
223
        $this->CurlRequest = curl_init();
224
        $this->status = self::STATUS_INIT;
0 ignored issues
show
Documentation Bug introduced by
It seems like self::STATUS_INIT of type string is incompatible with the declared type null of property $status.

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...
225
        return $this;
226
    }
227
228
    /**
229
     * @inheritdoc
230
     */
231
    public function close(){
232
        curl_close($this->CurlRequest);
233
        unset($this->CurlRequest);
234
        $this->status = self::STATUS_CLOSED;
0 ignored issues
show
Documentation Bug introduced by
It seems like self::STATUS_CLOSED of type string is incompatible with the declared type null of property $status.

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...
235
        return $this;
236
    }
237
238
    /**
239
     * @inheritdoc
240
     */
241
    public function getCurlStatus(){
242
        return $this->status;
243
    }
244
}