|
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(array $data){ |
|
124
|
|
|
$this->Data = $data; |
|
125
|
|
|
$this->Request->setBody($this->Data); |
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @inheritdoc |
|
131
|
|
|
*/ |
|
132
|
|
|
public function execute(){ |
|
133
|
|
|
if ($this->verifyURL() && $this->validateData()) { |
|
134
|
|
|
$this->configureURL(); |
|
135
|
|
|
$this->Request->setURL($this->url); |
|
136
|
|
|
$this->Request->send(); |
|
137
|
|
|
$this->setupResponse(); |
|
138
|
|
|
//Trying to manage memory by closing Curl Resource |
|
139
|
|
|
$this->Request->close(); |
|
140
|
|
|
} |
|
141
|
|
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @inheritdoc |
|
146
|
|
|
*/ |
|
147
|
|
|
public function authRequired() { |
|
148
|
|
|
return $this->_AUTH_REQUIRED; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @inheritdoc |
|
153
|
|
|
*/ |
|
154
|
|
|
public function configureAuth($accessToken) { |
|
155
|
|
|
if ($this->authRequired()){ |
|
156
|
|
|
$this->accessToken = $accessToken; |
|
157
|
|
|
$this->Request->addHeader('OAuth-Token', $accessToken); |
|
158
|
|
|
} |
|
159
|
|
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @inheritdoc |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getModule() { |
|
166
|
|
|
return $this->Module; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @inheritdoc |
|
171
|
|
|
*/ |
|
172
|
|
|
public function getData(){ |
|
173
|
|
|
return $this->Data; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @inheritdoc |
|
178
|
|
|
*/ |
|
179
|
|
|
public function getURL(){ |
|
180
|
|
|
return $this->url; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @inheritdoc |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getResponse(){ |
|
187
|
|
|
return $this->Response; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @inheritdoc |
|
192
|
|
|
*/ |
|
193
|
|
|
public function getRequest(){ |
|
194
|
|
|
return $this->Request; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Configures the URL, by updating any variable placeholders in the URL property on the EntryPoint |
|
199
|
|
|
* - Replaces $module with $this->Module |
|
200
|
|
|
* - Replaces all other variables starting with $, with options in the order they were given |
|
201
|
|
|
*/ |
|
202
|
|
|
protected function configureURL(){ |
|
203
|
|
|
$url = $this->_URL; |
|
204
|
|
|
if (strpos($this->_URL,"$")!==FALSE) { |
|
205
|
|
|
if (count($this->Options) > 0 || !empty($this->Module)) { |
|
206
|
|
|
$urlParts = explode("/", $this->_URL); |
|
207
|
|
|
$o = 0; |
|
208
|
|
|
foreach ($urlParts as $key => $part) { |
|
209
|
|
|
if (strpos($part, '$module') !== FALSE) { |
|
210
|
|
|
if (isset($this->Module)) { |
|
211
|
|
|
$urlParts[$key] = $this->Module; |
|
212
|
|
|
continue; |
|
213
|
|
|
} else { |
|
214
|
|
|
if (isset($this->Options[$o])) { |
|
215
|
|
|
$this->Module = $this->Options[$o]; |
|
216
|
|
|
array_shift($this->Options); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
if (strpos($part, "$") !== FALSE) { |
|
221
|
|
|
if (isset($this->Options[$o])) { |
|
222
|
|
|
$urlParts[$key] = $this->Options[$o]; |
|
223
|
|
|
$o++; |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
$url = implode($urlParts,"/"); |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
$this->url = $this->url.$url; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Setup the Request Object property, setup on initial Construct of EntryPoint |
|
235
|
|
|
*/ |
|
236
|
|
|
protected function setupRequest(){ |
|
237
|
|
|
$this->Request = new POST(); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Setup the Response Object Property, not called until after Request Execution |
|
242
|
|
|
*/ |
|
243
|
|
|
protected function setupResponse(){ |
|
244
|
|
|
$this->Response = new JSONResponse($this->Request->getResponse(),$this->Request->getCurlObject()); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Verify URL variables have been removed, and that valid number of options were passed. |
|
249
|
|
|
* @return bool |
|
250
|
|
|
* @throws EntryPointException |
|
251
|
|
|
*/ |
|
252
|
|
|
protected function verifyURL(){ |
|
253
|
|
|
$urlVarCount = substr_count($this->_URL,"$"); |
|
254
|
|
|
$optionCount = 0; |
|
255
|
|
|
if (!empty($this->Module)){ |
|
256
|
|
|
$optionCount++; |
|
257
|
|
|
} |
|
258
|
|
|
$optionCount += count($this->Options); |
|
259
|
|
|
if ($urlVarCount!==$optionCount){ |
|
260
|
|
|
if (empty($this->Module) && strpos($this->_URL,'$module')){ |
|
261
|
|
|
throw new EntryPointException('Module is required for EntryPoint '.get_called_class()); |
|
262
|
|
|
}else{ |
|
263
|
|
|
throw new EntryPointException('EntryPoint URL ('.$this->_URL.') requires more parameters than passed.'); |
|
264
|
|
|
} |
|
265
|
|
|
}else{ |
|
266
|
|
|
return true; |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Validate Required Data for the Request |
|
272
|
|
|
* @return bool |
|
273
|
|
|
* @throws EntryPointException |
|
274
|
|
|
*/ |
|
275
|
|
|
protected function validateData(){ |
|
276
|
|
|
if (empty($this->_REQUIRED_DATA)||count($this->_REQUIRED_DATA)==0){ |
|
277
|
|
|
return true; |
|
278
|
|
|
}else{ |
|
279
|
|
|
$errors = array(); |
|
280
|
|
|
foreach($this->_REQUIRED_DATA as $property){ |
|
281
|
|
|
if (isset($this->Data[$property]) || $this->Data[$property]!==null){ |
|
282
|
|
|
continue; |
|
283
|
|
|
}else{ |
|
284
|
|
|
$errors[] = $property; |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
if (count($errors)>0){ |
|
288
|
|
|
throw new EntryPointException('EntryPoint requires specific properties in Request data. Missing the following '.implode($errors,",")); |
|
289
|
|
|
}else{ |
|
290
|
|
|
return true; |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
} |