1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Surge\LaravelSalesforce\Objects; |
4
|
|
|
|
5
|
|
|
use Event; |
6
|
|
|
use Surge\LaravelSalesforce\Events\RequestSent; |
7
|
|
|
use Surge\LaravelSalesforce\Events\ResponseReceived; |
8
|
|
|
use Surge\LaravelSalesforce\Exceptions\SalesforceException; |
9
|
|
|
|
10
|
|
|
abstract class AbstractObject implements ObjectInterface |
11
|
|
|
{ |
12
|
|
|
protected $recordType; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param string $method |
16
|
|
|
* @param string $url |
17
|
|
|
* @param array $options |
18
|
|
|
* |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
|
|
private function sendRequest(string $method, string $url, array $options = []): string |
22
|
|
|
{ |
23
|
|
|
Event::fire(new RequestSent([ |
24
|
|
|
'options' => $options, |
25
|
|
|
'url' => $url, |
26
|
|
|
'class' => get_class($this), |
27
|
|
|
'type' => 'REQUEST', |
28
|
|
|
])); |
29
|
|
|
|
30
|
|
|
$response = json_decode($this->client->request($method, $this->url.$url, $options)->getBody()); |
|
|
|
|
31
|
|
|
|
32
|
|
|
Event::fire(new ResponseReceived([ |
33
|
|
|
'options' => $response, |
34
|
|
|
'url' => $url, |
35
|
|
|
'class' => get_class($this), |
36
|
|
|
'type' => 'RESPONSE', |
37
|
|
|
])); |
38
|
|
|
|
39
|
|
|
return $response; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get latest version. |
44
|
|
|
* |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
|
|
protected function getVersion() |
48
|
|
|
{ |
49
|
|
|
return $this->sendRequest('GET', $this->auth->instanceUrl.'/services/data'); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get all organisation limits. |
54
|
|
|
*/ |
55
|
|
|
protected function listOrganisationLimits() |
56
|
|
|
{ |
57
|
|
|
return $this->sendRequest('GET', $this->auth->instanceUrl.$this->version['url'].'/limits'); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* List all available resources. |
62
|
|
|
* |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
protected function listAvailableResources() |
66
|
|
|
{ |
67
|
|
|
return $this->sendRequest('GET', ''); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* List all objects. |
72
|
|
|
* |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
|
|
protected function listObjects() |
76
|
|
|
{ |
77
|
|
|
return $this->sendRequest('GET', '/sobjects'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Describe an object. |
82
|
|
|
* |
83
|
|
|
* @param $objectName |
84
|
|
|
* |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
|
|
protected function describeObject($objectName) |
88
|
|
|
{ |
89
|
|
|
return $this->sendRequest('GET', '/sobjects/'.$objectName.'/describe'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Describe basic object. |
94
|
|
|
* |
95
|
|
|
* @param $objectName |
96
|
|
|
* |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
protected function describeBasicObject($objectName) |
100
|
|
|
{ |
101
|
|
|
return $this->sendRequest('GET', '/sobjects/'.$objectName); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Run report. |
106
|
|
|
* |
107
|
|
|
* @param $params |
108
|
|
|
* |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
|
|
public function runReport($params) |
112
|
|
|
{ |
113
|
|
|
return $this->sendRequest( |
114
|
|
|
'GET', |
115
|
|
|
'/analytics/reports/' . $params['id'], |
116
|
|
|
['query' => ['includeDetails' => $params['includeDetails']]] |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Run Salesforce query. |
122
|
|
|
* |
123
|
|
|
* @param $query |
124
|
|
|
* |
125
|
|
|
* @return mixed |
126
|
|
|
*/ |
127
|
|
|
protected function query($query) |
128
|
|
|
{ |
129
|
|
|
return $this->sendRequest('GET', '/query', ['query' => [ |
130
|
|
|
'q' => $query, |
131
|
|
|
]]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get record. |
136
|
|
|
* |
137
|
|
|
* @param string $id |
138
|
|
|
* |
139
|
|
|
* @param array $fields |
140
|
|
|
* @return bool|mixed |
141
|
|
|
*/ |
142
|
|
|
public function get(string $id, array $fields = []) |
143
|
|
|
{ |
144
|
|
|
if (!$id) { |
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$response = $this->sendRequest('GET', "/sobjects/" . $this->getType() . "/$id", ['query' => $fields]); |
149
|
|
|
|
150
|
|
|
if (!$response) { |
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $response; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Update. |
159
|
|
|
* |
160
|
|
|
* @param string $id |
161
|
|
|
* @param $params |
162
|
|
|
* @return bool|mixed |
163
|
|
|
* @throws SalesforceException |
164
|
|
|
*/ |
165
|
|
|
public function update(string $id, array $params) |
166
|
|
|
{ |
167
|
|
|
if (!$id) { |
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$response = $this->sendRequest( |
172
|
|
|
'PATCH', |
173
|
|
|
"/sobjects/$this->recordType/$id", |
174
|
|
|
[ |
175
|
|
|
'json' => $params, |
176
|
|
|
] |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
if (!$response) { |
180
|
|
|
return false; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
if ($response->success !== true) { |
184
|
|
|
throw new SalesforceException($response->errors); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $response; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Insert new account. |
192
|
|
|
* |
193
|
|
|
* @param $params |
194
|
|
|
* |
195
|
|
|
* @return bool |
196
|
|
|
* @throws SalesforceException |
197
|
|
|
*/ |
198
|
|
View Code Duplication |
public function create(array $params) |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
$response = $this->sendRequest('POST', "/sobject/" . $this->getType(), [ |
201
|
|
|
'json' => $params, |
202
|
|
|
]); |
203
|
|
|
|
204
|
|
|
if (!$response) { |
205
|
|
|
return false; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if ($response->success !== true) { |
209
|
|
|
throw new SalesforceException($response->errors); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $response; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Delete a given record |
217
|
|
|
* |
218
|
|
|
* @param string $id |
219
|
|
|
* @return bool |
220
|
|
|
* @throws SalesforceException |
221
|
|
|
*/ |
222
|
|
View Code Duplication |
public function delete(string $id) |
|
|
|
|
223
|
|
|
{ |
224
|
|
|
$response = $this->sendRequest('DELETE', "/sobjects/" . $this->getType() ."/$id"); |
225
|
|
|
|
226
|
|
|
if (!$response) { |
227
|
|
|
return false; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
if ($response->success !== true) { |
231
|
|
|
throw new SalesforceException($response->errors); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return $response; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
protected function getType() |
238
|
|
|
{ |
239
|
|
|
if (isset($this->type)) { |
240
|
|
|
return $this->type; |
|
|
|
|
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return get_class($this); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: