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
|
|
|
private $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 Salesforce query. |
106
|
|
|
* |
107
|
|
|
* @param $query |
108
|
|
|
* |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
|
|
protected function query($query) |
112
|
|
|
{ |
113
|
|
|
return $this->sendRequest('GET', '/query', ['query' => [ |
114
|
|
|
'q' => $query, |
115
|
|
|
]]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get record. |
120
|
|
|
* |
121
|
|
|
* @param $id |
122
|
|
|
* @param array $fields |
123
|
|
|
* |
124
|
|
|
* @return bool|mixed |
125
|
|
|
*/ |
126
|
|
|
public function get($id, array $fields = []) |
127
|
|
|
{ |
128
|
|
|
if (!$id) { |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$response = $this->sendRequest('GET', "/sobjects/$type/$id", ['query' => $fields]); |
|
|
|
|
133
|
|
|
|
134
|
|
|
if (!$response) { |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $response; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Update. |
143
|
|
|
* |
144
|
|
|
* @param $id |
145
|
|
|
* @param $params |
146
|
|
|
* @return bool|mixed |
147
|
|
|
* @throws SalesforceException |
148
|
|
|
*/ |
149
|
|
|
public function update($id, array $params) |
150
|
|
|
{ |
151
|
|
|
if (!$id) { |
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$response = $this->sendRequest( |
156
|
|
|
'PATCH', |
157
|
|
|
"/sobjects/$type/$id", |
|
|
|
|
158
|
|
|
[ |
159
|
|
|
'json' => $data, |
|
|
|
|
160
|
|
|
] |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
if (!$response) { |
164
|
|
|
return false; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if ($response->success !== true) { |
168
|
|
|
throw new SalesforceException($response->errors); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $response; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Insert new account. |
176
|
|
|
* |
177
|
|
|
* @param $params |
178
|
|
|
* |
179
|
|
|
* @return bool |
180
|
|
|
* @throws SalesforceException |
181
|
|
|
*/ |
182
|
|
View Code Duplication |
public function create($params) |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
$response = $this->sendRequest('POST', "/sobjects/$type", [ |
|
|
|
|
185
|
|
|
'json' => $data, |
|
|
|
|
186
|
|
|
]); |
187
|
|
|
|
188
|
|
|
if (!$response) { |
189
|
|
|
return false; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if ($response->success !== true) { |
193
|
|
|
throw new SalesforceException($response->errors); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $response; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Delete a given record |
201
|
|
|
* |
202
|
|
|
* @param string $type |
203
|
|
|
* @param string $id |
204
|
|
|
* @return bool |
205
|
|
|
* @throws SalesforceException |
206
|
|
|
*/ |
207
|
|
View Code Duplication |
public function delete(string $type, string $id) |
|
|
|
|
208
|
|
|
{ |
209
|
|
|
$response = $this->sendRequest('DELETE', "/sobjects/$type/$id"); |
210
|
|
|
|
211
|
|
|
if (!$response) { |
212
|
|
|
return false; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
if ($response->success !== true) { |
216
|
|
|
throw new SalesforceException($response->errors); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return $response; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
} |
223
|
|
|
|
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: