1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Surge\LaravelSalesforce\Objects; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\ClientException; |
6
|
|
|
use Surge\LaravelSalesforce\Events\RequestSent; |
7
|
|
|
use Surge\LaravelSalesforce\Events\ResponseReceived; |
8
|
|
|
use Surge\LaravelSalesforce\Exceptions\SalesforceException; |
9
|
|
|
use Surge\LaravelSalesforce\Salesforce; |
10
|
|
|
|
11
|
|
|
abstract class AbstractObject implements ObjectInterface |
12
|
|
|
{ |
13
|
|
|
protected $salesforce; |
14
|
|
|
|
15
|
|
|
public $custom = false; |
16
|
|
|
|
17
|
|
|
public function __construct(Salesforce $salesforce) |
18
|
|
|
{ |
19
|
|
|
$this->salesforce = $salesforce; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $method |
24
|
|
|
* @param string $url |
25
|
|
|
* @param array $options |
26
|
|
|
* |
27
|
|
|
* @return object |
28
|
|
|
*/ |
29
|
|
|
protected function sendRequest(string $method, string $url, array $options = []) |
30
|
|
|
{ |
31
|
|
|
event(new RequestSent([ |
32
|
|
|
'data' => $options, |
33
|
|
|
'url' => $url, |
34
|
|
|
'class' => get_class($this), |
35
|
|
|
'type' => 'REQUEST', |
36
|
|
|
])); |
37
|
|
|
|
38
|
|
|
if (config('laravel-salesforce.disable_on_local') && app()->environment('local')) { |
39
|
|
|
$response = (object)['success' => true, 'totalSize' => 0, 'id' => 'localRequestId', 'OwnerId' => 'localRequestId']; |
40
|
|
|
} else { |
41
|
|
|
try { |
42
|
|
|
$response = json_decode( |
43
|
|
|
$this->salesforce->client->request($method, $this->salesforce->baseUrl . $url, $options) |
44
|
|
|
->getBody()); |
45
|
|
|
} catch (ClientException $e) { |
46
|
|
|
throw new SalesforceException($e->getMessage()); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
event(new ResponseReceived([ |
51
|
|
|
'data' => $response, |
52
|
|
|
'url' => $url, |
53
|
|
|
'class' => get_class($this), |
54
|
|
|
'type' => 'RESPONSE', |
55
|
|
|
])); |
56
|
|
|
|
57
|
|
|
return $response; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function getType() |
61
|
|
|
{ |
62
|
|
|
if (isset($this->type)) { |
63
|
|
|
return $this->type; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($this->custom === true) { |
67
|
|
|
return (new \ReflectionClass($this))->getShortName() . '__c'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return (new \ReflectionClass($this))->getShortName(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get latest version. |
75
|
|
|
* |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
|
|
public function getVersion() |
79
|
|
|
{ |
80
|
|
|
return $this->sendRequest('GET', $this->salesforce->instanceUrl . '/services/data'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get all organisation limits. |
85
|
|
|
*/ |
86
|
|
|
public function listOrganisationLimits() |
87
|
|
|
{ |
88
|
|
|
return $this->sendRequest('GET', $this->salesforce->instanceUrl . $this->version['url'] . '/limits'); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* List all available resources. |
93
|
|
|
* |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
public function listAvailableResources() |
97
|
|
|
{ |
98
|
|
|
return $this->sendRequest('GET', ''); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* List all objects. |
103
|
|
|
* |
104
|
|
|
* @return mixed |
105
|
|
|
*/ |
106
|
|
|
public function listObjects() |
107
|
|
|
{ |
108
|
|
|
return $this->sendRequest('GET', '/sobjects'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Describe an object. |
113
|
|
|
* |
114
|
|
|
* @param $objectName |
115
|
|
|
* |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
|
|
public function describeObject($objectName) |
119
|
|
|
{ |
120
|
|
|
return $this->sendRequest('GET', '/sobjects/' . $objectName . '/describe'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Describe basic object. |
125
|
|
|
* |
126
|
|
|
* @param $objectName |
127
|
|
|
* |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
|
|
public function describeBasicObject($objectName) |
131
|
|
|
{ |
132
|
|
|
return $this->sendRequest('GET', '/sobjects/' . $objectName); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Run Salesforce query. |
137
|
|
|
* |
138
|
|
|
* @param $query |
139
|
|
|
* |
140
|
|
|
* @return mixed |
141
|
|
|
*/ |
142
|
|
|
public function query($query) |
143
|
|
|
{ |
144
|
|
|
return $this->sendRequest('GET', '/query', [ |
145
|
|
|
'query' => [ |
146
|
|
|
'q' => $query, |
147
|
|
|
], |
148
|
|
|
]); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get record. |
153
|
|
|
* |
154
|
|
|
* @param string $id |
155
|
|
|
* |
156
|
|
|
* @param array $fields |
157
|
|
|
*/ |
158
|
|
|
public function get(string $id, array $fields = []) |
159
|
|
|
{ |
160
|
|
|
return $this->sendRequest('GET', "/sobjects/" . $this->getType() . "/$id", ['query' => $fields]); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Update. |
165
|
|
|
* |
166
|
|
|
* @param string $id |
167
|
|
|
* @param $params |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
|
|
public function update(string $id, array $params) |
171
|
|
|
{ |
172
|
|
|
$this->sendRequest('PATCH', "/sobjects/" . $this->getType() . "/$id", |
173
|
|
|
[ |
174
|
|
|
'json' => $params, |
175
|
|
|
] |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Insert new account. |
181
|
|
|
* |
182
|
|
|
* @param $params |
183
|
|
|
* |
184
|
|
|
* @throws SalesforceException |
185
|
|
|
*/ |
186
|
|
|
public function create(array $params) |
187
|
|
|
{ |
188
|
|
|
$response = $this->sendRequest('POST', "/sobjects/" . $this->getType(), [ |
189
|
|
|
'json' => $params, |
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 $id |
203
|
|
|
* @throws SalesforceException |
204
|
|
|
*/ |
205
|
|
|
public function delete(string $id) |
206
|
|
|
{ |
207
|
|
|
$this->sendRequest('DELETE', "/sobjects/" . $this->getType() . "/$id"); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get report |
212
|
|
|
* |
213
|
|
|
* @param string $id |
214
|
|
|
* @param bool $includeDetails |
215
|
|
|
* @return object |
216
|
|
|
*/ |
217
|
|
|
public function report(string $id, bool $includeDetails = true) |
218
|
|
|
{ |
219
|
|
|
return $this->sendRequest( |
220
|
|
|
'GET', |
221
|
|
|
'/analytics/reports/' . $id, |
222
|
|
|
['query' => ['hasDetailRows' => $includeDetails]] |
223
|
|
|
); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
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: