1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SugarAPI\SDK\EntryPoint\Abstracts; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use SugarAPI\SDK\EntryPoint\Interfaces\EPInterface; |
7
|
|
|
use SugarAPI\SDK\Exception\EntryPointException; |
8
|
|
|
use SugarAPI\SDK\Request\POST; |
9
|
|
|
use SugarAPI\SDK\Response\JSON as JSONResponse; |
10
|
|
|
|
11
|
|
|
abstract class AbstractEntryPoint implements EPInterface { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Whether or not Authentication is Required |
15
|
|
|
* @var bool |
16
|
|
|
*/ |
17
|
|
|
protected $_AUTH_REQUIRED = true; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The default Module for the EntryPoint |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $_MODULE; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The URL for the EntryPoint |
27
|
|
|
* - When configuring URL you define URL Parameters with $variables |
28
|
|
|
* Examples: |
29
|
|
|
* - Forecasts/$record_id |
30
|
|
|
* - $module Variable is a keyword to place the Module property into the URL |
31
|
|
|
* Examples: |
32
|
|
|
* - $module/$record |
33
|
|
|
* - Options property is used to replace variables in the order in which they are passed |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $_URL; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* An array of Required Data properties that should be passed in the Request |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $_REQUIRED_DATA; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The configured URL for the EntryPoint |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $url; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The configured Module for the EntryPoint |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $Module; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The passed in Options for the EntryPoint |
59
|
|
|
* - If $module variable is used in $_URL static property, then 1st option will be used as Module |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
protected $Options = array(); |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* The data being passed to the API EntryPoint |
66
|
|
|
* @var mixed - array or Std Object |
67
|
|
|
*/ |
68
|
|
|
protected $Data; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* The Request Object, used by the EntryPoint to submit the data |
72
|
|
|
* @var Object |
73
|
|
|
*/ |
74
|
|
|
protected $Request; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* The Response Object, returned by the Request Object |
78
|
|
|
* @var Object |
79
|
|
|
*/ |
80
|
|
|
protected $Response; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Access Token for authentication |
84
|
|
|
* @var string |
85
|
|
|
*/ |
86
|
|
|
protected $accessToken; |
87
|
|
|
|
88
|
|
|
public function __construct($url,$options = array()){ |
89
|
|
|
$this->url = $url; |
90
|
|
|
$this->Module = $this->_MODULE; |
91
|
|
|
|
92
|
|
|
if (!empty($options)) { |
93
|
|
|
if (empty($this->Module)) { |
94
|
|
|
if (strpos($this->_URL, '$module') !== FALSE) { |
95
|
|
|
$this->module($options[0]); |
96
|
|
|
array_shift($options); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
$this->options($options); |
100
|
|
|
} |
101
|
|
|
$this->setupRequest(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @inheritdoc |
106
|
|
|
*/ |
107
|
|
|
public function module($module){ |
108
|
|
|
$this->Module = $module; |
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @inheritdoc |
114
|
|
|
*/ |
115
|
|
|
public function options(array $options){ |
116
|
|
|
$this->Options = $options; |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritdoc |
122
|
|
|
*/ |
123
|
|
|
public function data($data){ |
124
|
|
|
$data = $this->configureData($data); |
125
|
|
|
$this->Data = $data; |
126
|
|
|
$this->Request->setBody($this->Data); |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Override function for configuring Default Values on some EntryPoints to allow for short hand |
132
|
|
|
* @param mixed $data |
133
|
|
|
* @return mixed |
134
|
|
|
*/ |
135
|
|
|
protected function configureData($data){ |
136
|
|
|
return $data; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @inheritdoc |
141
|
|
|
*/ |
142
|
|
|
public function execute(){ |
143
|
|
|
if ($this->verifyURL() && $this->validateData()) { |
144
|
|
|
$this->configureURL(); |
145
|
|
|
$this->Request->setURL($this->url); |
146
|
|
|
$this->Request->send(); |
147
|
|
|
$this->setupResponse(); |
148
|
|
|
//Trying to manage memory by closing Curl Resource |
149
|
|
|
$this->Request->close(); |
150
|
|
|
} |
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @inheritdoc |
156
|
|
|
*/ |
157
|
|
|
public function authRequired() { |
158
|
|
|
return $this->_AUTH_REQUIRED; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @inheritdoc |
163
|
|
|
*/ |
164
|
|
|
public function configureAuth($accessToken) { |
165
|
|
|
if ($this->authRequired()){ |
166
|
|
|
$this->accessToken = $accessToken; |
167
|
|
|
$this->Request->addHeader('OAuth-Token', $accessToken); |
168
|
|
|
} |
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @inheritdoc |
174
|
|
|
*/ |
175
|
|
|
public function getModule() { |
176
|
|
|
return $this->Module; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @inheritdoc |
181
|
|
|
*/ |
182
|
|
|
public function getData(){ |
183
|
|
|
return $this->Data; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @inheritdoc |
188
|
|
|
*/ |
189
|
|
|
public function getURL(){ |
190
|
|
|
return $this->url; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @inheritdoc |
195
|
|
|
*/ |
196
|
|
|
public function getResponse(){ |
197
|
|
|
return $this->Response; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @inheritdoc |
202
|
|
|
*/ |
203
|
|
|
public function getRequest(){ |
204
|
|
|
return $this->Request; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Configures the URL, by updating any variable placeholders in the URL property on the EntryPoint |
209
|
|
|
* - Replaces $module with $this->Module |
210
|
|
|
* - Replaces all other variables starting with $, with options in the order they were given |
211
|
|
|
*/ |
212
|
|
|
protected function configureURL(){ |
213
|
|
|
$url = $this->_URL; |
214
|
|
|
if (strpos($this->_URL,"$")!==FALSE) { |
215
|
|
|
if (count($this->Options) > 0 || !empty($this->Module)) { |
216
|
|
|
$urlParts = explode("/", $this->_URL); |
217
|
|
|
$o = 0; |
218
|
|
|
foreach ($urlParts as $key => $part) { |
219
|
|
|
if (strpos($part, '$module') !== FALSE) { |
220
|
|
|
if (isset($this->Module)) { |
221
|
|
|
$urlParts[$key] = $this->Module; |
222
|
|
|
continue; |
223
|
|
|
} else { |
224
|
|
|
if (isset($this->Options[$o])) { |
225
|
|
|
$this->Module = $this->Options[$o]; |
226
|
|
|
array_shift($this->Options); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
if (strpos($part, "$") !== FALSE) { |
231
|
|
|
if (isset($this->Options[$o])) { |
232
|
|
|
$urlParts[$key] = $this->Options[$o]; |
233
|
|
|
$o++; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
$url = implode($urlParts,"/"); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
$this->url = $this->url.$url; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Setup the Request Object property, setup on initial Construct of EntryPoint |
245
|
|
|
*/ |
246
|
|
|
protected function setupRequest(){ |
247
|
|
|
$this->Request = new POST(); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Setup the Response Object Property, not called until after Request Execution |
252
|
|
|
*/ |
253
|
|
|
protected function setupResponse(){ |
254
|
|
|
$this->Response = new JSONResponse($this->Request->getResponse(),$this->Request->getCurlObject()); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Verify URL variables have been removed, and that valid number of options were passed. |
259
|
|
|
* @return bool |
260
|
|
|
* @throws EntryPointException |
261
|
|
|
*/ |
262
|
|
|
protected function verifyURL(){ |
263
|
|
|
$urlVarCount = substr_count($this->_URL,"$"); |
264
|
|
|
$optionCount = 0; |
265
|
|
|
if (!empty($this->Module)){ |
266
|
|
|
$optionCount++; |
267
|
|
|
} |
268
|
|
|
$optionCount += count($this->Options); |
269
|
|
|
if ($urlVarCount!==$optionCount){ |
270
|
|
|
if (empty($this->Module) && strpos($this->_URL,'$module')){ |
271
|
|
|
throw new EntryPointException('Module is required for EntryPoint '.get_called_class()); |
272
|
|
|
}else{ |
273
|
|
|
throw new EntryPointException('EntryPoint URL ('.$this->_URL.') requires more parameters than passed.'); |
274
|
|
|
} |
275
|
|
|
}else{ |
276
|
|
|
return true; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Validate Required Data for the Request |
282
|
|
|
* @return bool |
283
|
|
|
* @throws EntryPointException |
284
|
|
|
*/ |
285
|
|
|
protected function validateData(){ |
286
|
|
|
if (empty($this->_REQUIRED_DATA)||count($this->_REQUIRED_DATA)==0){ |
287
|
|
|
return true; |
288
|
|
|
}else{ |
289
|
|
|
$errors = array(); |
290
|
|
|
foreach($this->_REQUIRED_DATA as $property){ |
291
|
|
|
if (isset($this->Data[$property]) || $this->Data[$property]!==null){ |
292
|
|
|
continue; |
293
|
|
|
}else{ |
294
|
|
|
$errors[] = $property; |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
if (count($errors)>0){ |
298
|
|
|
throw new EntryPointException('EntryPoint requires specific properties in Request data. Missing the following '.implode($errors,",")); |
299
|
|
|
}else{ |
300
|
|
|
return true; |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
} |