|
2 | 2 |
|
3 | 3 | namespace SynergiTech\Salesforce\Services;
|
4 | 4 |
|
| 5 | +use Exception; |
5 | 6 | use Illuminate\Support\Collection;
|
6 | 7 | use Omniphx\Forrest\Exceptions\SalesforceException;
|
7 | 8 | use Omniphx\Forrest\Providers\Laravel\Facades\Forrest;
|
| 9 | +use stdClass; |
| 10 | +use SynergiTech\Salesforce\Exceptions\EntityIsDeletedException; |
| 11 | +use SynergiTech\Salesforce\Exceptions\InvalidCrossReferenceKeyException; |
8 | 12 | use SynergiTech\Salesforce\Exceptions\InvalidFieldException;
|
| 13 | +use SynergiTech\Salesforce\Exceptions\JsonParseErrorException; |
| 14 | +use SynergiTech\Salesforce\Exceptions\MalformedIdException; |
9 | 15 | use SynergiTech\Salesforce\Exceptions\MalformedQueryException;
|
10 | 16 | use SynergiTech\Salesforce\Exceptions\NotFoundException;
|
| 17 | +use SynergiTech\Salesforce\Exceptions\RequiredFieldMissingException; |
11 | 18 | use SynergiTech\Salesforce\Models\Builder;
|
12 | 19 | use SynergiTech\Salesforce\Models\Response;
|
13 | 20 |
|
@@ -47,39 +54,169 @@ public function findMany(int|string|array $id, string $fieldName = 'Id'): Collec
|
47 | 54 | $response = $this->get();
|
48 | 55 |
|
49 | 56 | if ($response->records->count() === 0) {
|
50 |
| - throw new NotFoundException("No records with the specified IDs could be found"); |
| 57 | + throw new NotFoundException("No record(s) with the specified ID(s) could be found"); |
51 | 58 | }
|
52 | 59 |
|
53 | 60 | return $response->records;
|
54 | 61 | }
|
55 | 62 |
|
| 63 | + /** |
| 64 | + * Execute the query and retrieve available objects in a response |
| 65 | + */ |
56 | 66 | public function get(): Response
|
57 | 67 | {
|
58 | 68 | try {
|
59 | 69 | $query = $this->getQuery();
|
60 | 70 | return new Response($query, Forrest::query($query));
|
61 | 71 | } catch (SalesforceException $ex) {
|
62 |
| - $message = $ex->getMessage(); |
63 |
| - /** @var array<mixed> $errors */ |
64 |
| - $errors = json_decode($message); |
65 |
| - /** @var \stdClass{errorCode:string, message:string} $error */ |
66 |
| - $error = $errors[0]; |
67 |
| - |
68 |
| - if (property_exists($error, 'errorCode')) { |
69 |
| - $message = $this->formatErrorMessage($error->message); |
70 |
| - |
71 |
| - switch ($error->errorCode) { |
72 |
| - case 'MALFORMED_QUERY': |
73 |
| - throw new MalformedQueryException($message); |
74 |
| - case 'INVALID_QUERY_FILTER_OPERATOR': |
75 |
| - throw new InvalidFieldException($message); |
76 |
| - } |
| 72 | + throw $this->wrapException($ex); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Create a record in the Salesforce table with the specified data |
| 78 | + * |
| 79 | + * @param array<string, mixed> $data |
| 80 | + * @return array<string, mixed>|false |
| 81 | + */ |
| 82 | + public function create(array $data = []): array|false |
| 83 | + { |
| 84 | + try { |
| 85 | + $response = Forrest::sobjects($this->table, [ |
| 86 | + 'method' => 'post', |
| 87 | + 'body' => $data, |
| 88 | + ]); |
| 89 | + |
| 90 | + if ($response['success'] ?? false) { |
| 91 | + $response['data'] = $this->find($response['id']); |
| 92 | + return $response; |
77 | 93 | }
|
| 94 | + } catch (SalesforceException $ex) { |
| 95 | + throw $this->wrapException($ex); |
| 96 | + } |
| 97 | + |
| 98 | + return false; |
| 99 | + } |
78 | 100 |
|
79 |
| - throw $ex; |
| 101 | + /** |
| 102 | + * Update a record with the specified Id in Salesforce with the provided data |
| 103 | + * |
| 104 | + * @param array<string, mixed> $data |
| 105 | + * @return array<string, mixed> |
| 106 | + */ |
| 107 | + public function update(string $id, array $data): array |
| 108 | + { |
| 109 | + try { |
| 110 | + Forrest::sobjects(implode('/', [ |
| 111 | + $this->table, |
| 112 | + $id, |
| 113 | + ]), [ |
| 114 | + 'method' => 'patch', |
| 115 | + 'body' => $data, |
| 116 | + ]); |
| 117 | + |
| 118 | + return $this->find($id); |
| 119 | + } catch (SalesforceException $ex) { |
| 120 | + throw $this->wrapException($ex); |
80 | 121 | }
|
81 | 122 | }
|
82 | 123 |
|
| 124 | + /** |
| 125 | + * Upsert a record using the specified external Id field and Id with the provided data |
| 126 | + * |
| 127 | + * @param array<string, mixed> $data |
| 128 | + * @return array<string, mixed>|false |
| 129 | + */ |
| 130 | + public function createOrUpdate(string $field, string $id, array $data = []): array|false |
| 131 | + { |
| 132 | + try { |
| 133 | + $response = Forrest::sobjects(implode('/', [ |
| 134 | + $this->table, |
| 135 | + $field, |
| 136 | + $id, |
| 137 | + ]), [ |
| 138 | + 'method' => 'patch', |
| 139 | + 'body' => $data, |
| 140 | + ]); |
| 141 | + |
| 142 | + if ($response['success'] ?? false) { |
| 143 | + $response['data'] = $this->find($response['id']); |
| 144 | + return $response; |
| 145 | + } |
| 146 | + } catch (SalesforceException $ex) { |
| 147 | + throw $this->wrapException($ex); |
| 148 | + } |
| 149 | + |
| 150 | + return false; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Delete a record on the table using the specified Id |
| 155 | + * |
| 156 | + * @return bool |
| 157 | + */ |
| 158 | + public function delete(string $id): bool |
| 159 | + { |
| 160 | + try { |
| 161 | + Forrest::sobjects(implode('/', [ |
| 162 | + $this->table, |
| 163 | + $id, |
| 164 | + ]), [ |
| 165 | + 'method' => 'delete', |
| 166 | + ]); |
| 167 | + return true; |
| 168 | + } catch (SalesforceException $ex) { |
| 169 | + throw $this->wrapException($ex); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + protected function wrapException(SalesforceException $ex): Exception |
| 174 | + { |
| 175 | + $error = $this->decodeError($ex); |
| 176 | + |
| 177 | + if (property_exists($error, 'errorCode')) { |
| 178 | + $message = $this->formatErrorMessage($error->message); |
| 179 | + |
| 180 | + switch ($error->errorCode) { |
| 181 | + case 'ENTITY_IS_DELETED': |
| 182 | + return new EntityIsDeletedException($message); |
| 183 | + case 'INVALID_CROSS_REFERENCE_KEY': |
| 184 | + return new InvalidCrossReferenceKeyException($message); |
| 185 | + case 'INVALID_ID_FIELD': |
| 186 | + return new InvalidFieldException($message); |
| 187 | + case 'INVALID_QUERY_FILTER_OPERATOR': |
| 188 | + return new InvalidFieldException($message); |
| 189 | + case 'REQUIRED_FIELD_MISSING': |
| 190 | + return new RequiredFieldMissingException($message); |
| 191 | + case 'MALFORMED_QUERY': |
| 192 | + return new MalformedQueryException($message); |
| 193 | + case 'MALFORMED_ID': |
| 194 | + return new MalformedIdException($message); |
| 195 | + case 'NOT_FOUND': |
| 196 | + return new NotFoundException($message); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + return $ex; |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * @return stdClass{errorCode:string, message:string} |
| 205 | + */ |
| 206 | + protected function decodeError(SalesforceException $ex): stdClass |
| 207 | + { |
| 208 | + $message = $ex->getMessage(); |
| 209 | + /** @var array<mixed>|false $errors */ |
| 210 | + $errors = json_decode($message); |
| 211 | + |
| 212 | + if (json_last_error() !== JSON_ERROR_NONE) { |
| 213 | + throw new JsonParseErrorException('Error message received was not valid json. Message: ' . $message); |
| 214 | + } |
| 215 | + |
| 216 | + /** @var stdClass{errorCode:string, message:string} $error */ |
| 217 | + return $errors[0]; |
| 218 | + } |
| 219 | + |
83 | 220 | protected function formatErrorMessage(string $message): string
|
84 | 221 | {
|
85 | 222 | return str_replace('\n', ' - ', $message);
|
|
0 commit comments