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