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
|
|
|
* @param $opportunityId |
122
|
|
|
*/ |
123
|
|
|
public function getAllByOpportunityId($opportunityId) |
124
|
|
|
{ |
125
|
|
|
$query = 'Select Id, Name, Opportunity__c, Net_amount__c, Payment_Date__c, Gross_Amount__c From ' . $this->getType() . ' Where Opportunity__c = \'' . $opportunityId . '\' And IsDeleted = false'; |
126
|
|
|
|
127
|
|
|
$response = $this->query($query); |
128
|
|
|
|
129
|
|
|
if ($response && $response->totalSize > 0) { |
130
|
|
|
return $response->records; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return false; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Run Salesforce query. |
138
|
|
|
* |
139
|
|
|
* @param $query |
140
|
|
|
* |
141
|
|
|
* @return mixed |
142
|
|
|
*/ |
143
|
|
|
public function query($query) |
144
|
|
|
{ |
145
|
|
|
return $this->sendRequest('GET', '/query', [ |
146
|
|
|
'query' => [ |
147
|
|
|
'q' => $query, |
148
|
|
|
], |
149
|
|
|
]); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get record. |
154
|
|
|
* |
155
|
|
|
* @param string $id |
156
|
|
|
* |
157
|
|
|
* @param array $fields |
158
|
|
|
*/ |
159
|
|
|
public function get(string $id, array $fields = []) |
160
|
|
|
{ |
161
|
|
|
return $this->sendRequest('GET', "/sobjects/" . $this->getType() . "/$id", ['query' => $fields]); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Update. |
166
|
|
|
* |
167
|
|
|
* @param string $id |
168
|
|
|
* @param $params |
169
|
|
|
* @throws SalesforceException |
170
|
|
|
*/ |
171
|
|
View Code Duplication |
public function update(string $id, array $params) |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
$response = $this->sendRequest('PATCH', "/sobjects/" . $this->getType() . "/$id", |
174
|
|
|
[ |
175
|
|
|
'json' => $params, |
176
|
|
|
] |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
if ($response->success !== true) { |
180
|
|
|
throw new SalesforceException($response->errors); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $response; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Insert new account. |
188
|
|
|
* |
189
|
|
|
* @param $params |
190
|
|
|
* |
191
|
|
|
* @throws SalesforceException |
192
|
|
|
*/ |
193
|
|
View Code Duplication |
public function create(array $params) |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
$response = $this->sendRequest('POST', "/sobject/" . $this->getType(), [ |
196
|
|
|
'json' => $params, |
197
|
|
|
]); |
198
|
|
|
|
199
|
|
|
if ($response->success !== true) { |
200
|
|
|
throw new SalesforceException($response->errors); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $response; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Delete a given record |
208
|
|
|
* |
209
|
|
|
* @param string $id |
210
|
|
|
* @throws SalesforceException |
211
|
|
|
*/ |
212
|
|
View Code Duplication |
public function delete(string $id) |
|
|
|
|
213
|
|
|
{ |
214
|
|
|
$response = $this->sendRequest('DELETE', "/sobjects/" . $this->getType() . "/$id"); |
215
|
|
|
|
216
|
|
|
if ($response->success !== true) { |
217
|
|
|
throw new SalesforceException($response->errors); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $response; |
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: