ReviewOS

also looking at this

meemalabs/meema-client-php

Exceptions

#27
Merged glennmichael123 wants to merge exceptions into main
5 files +168 -14

Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.

src/Exceptions/InvalidFormatException.phpmodified+15-1
Changes to src/Exceptions/InvalidFormatException.php
@@ -2,6 +2,20 @@
22
33namespace Meema\MeemaApi\Exceptions;
44
5class InvalidFormatException
5use Exception;
6
7class InvalidFormatException extends Exception
68{
9 /**
10 * Render the exception as an HTTP response.
11 *
12 * @return string
13 */
14 public function errorMessage(): string
15 {
16 $errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile()
17 .' ID is not a valid integer';
18
19 return $errorMsg;
20 }
721}
src/Models/Folder.phpmodified+105-15
Changes to src/Models/Folder.php
@@ -3,6 +3,7 @@
33namespace Meema\MeemaApi\Models;
44
55use Meema\MeemaApi\Client;
6use Meema\MeemaApi\Exceptions\InvalidFormatException;
67use Meema\MeemaApi\Response\Response;
78
89class Folder
@@ -47,7 +48,9 @@ class Folder
4748 *
4849 * @param int $id
4950 *
50 * @return array
51 * @return mixed
52 *
53 * @throws InvalidFormatException
5154 */
5255 public function get($id = null)
5356 {
@@ -55,7 +58,15 @@ class Folder
5558 return $this->all();
5659 }
5760
58 $ids = is_array($id) ? $id : func_get_args();
61 try {
62 if (! is_int($id)) {
63 throw new InvalidFormatException();
64 }
65
66 $ids = is_array($id) ? $id : func_get_args();
67 } catch (InvalidFormatException $e) {
68 return $e->errorMessage();
69 }
5970
6071 return $this->client->request('GET', 'folders', ['folder_ids' => $ids]);
6172 }
@@ -66,10 +77,21 @@ class Folder
6677 * @param int $id
6778 *
6879 * @return Response
80 *
81 * @throws InvalidFormatException
6982 */
70 public function find($id): Response
83 public function find($id)
7184 {
72 $response = $this->client->request('GET', "folders/${id}");
85 try {
86 if (! is_int($id)) {
87 throw new InvalidFormatException();
88 }
89
90 $response = $this->client->request('GET', "folders/${id}");
91
92 } catch (InvalidFormatException $e) {
93 return $e->errorMessage();
94 }
7395
7496 return new Response($this, $response);
7597 }
@@ -97,14 +119,22 @@ class Folder
97119 /**
98120 * Update folder.
99121 *
100 * @param string $name
101122 * @param int $id
123 * @param string $name
102124 *
103125 * @return array
104126 */
105127 public function update($id, $name): array
106128 {
107 $name = is_array($name) ? $name : compact('name');
129 try {
130 if (! is_int($id)) {
131 throw new InvalidFormatException();
132 }
133
134 $name = is_array($name) ? $name : compact('name');
135 } catch (InvalidFormatException $e) {
136 return $e->errorMessage();
137 }
108138
109139 return $this->client->request('PATCH', "folders/{$id}", $name);
110140 }
@@ -115,11 +145,21 @@ class Folder
115145 * @param int $id
116146 *
117147 * @return null
148 *
149 * @throws InvalidFormatException
118150 */
119151 public function delete($id)
120152 {
121 if ($this->model) {
122 return $this->deleteFolderFromMedia($this->model->getId(), $id);
153 try {
154 if (! is_int($id)) {
155 throw new InvalidFormatException();
156 }
157
158 if ($this->model) {
159 return $this->deleteFolderFromMedia($this->model->getId(), $id);
160 }
161 } catch (InvalidFormatException $e) {
162 return $e->errorMessage();
123163 }
124164
125165 return $this->client->request('DELETE', "folders/{$id}");
@@ -143,10 +183,20 @@ class Folder
143183 * @param int $id
144184 *
145185 * @return array
186 *
187 * @throws InvalidFormatException
146188 */
147 public function archive($id): array
189 public function archive($id)
148190 {
149 return $this->client->request('POST', "folders/{$id}/archive");
191 try {
192 if (! is_int($id)) {
193 throw new InvalidFormatException();
194 }
195
196 return $this->client->request('POST', "folders/{$id}/archive");
197 } catch (InvalidFormatException $e) {
198 return $e->errorMessage();
199 }
150200 }
151201
152202 /**
@@ -155,10 +205,20 @@ class Folder
155205 * @param int $id
156206 *
157207 * @return array
208 *
209 * @throws InvalidFormatException
158210 */
159 public function unarchive($id): array
211 public function unarchive($id)
160212 {
161 return $this->client->request('POST', "folders/{$id}/unarchive");
213 try {
214 if (! is_int($id)) {
215 throw new InvalidFormatException();
216 }
217
218 return $this->client->request('POST', "folders/{$id}/unarchive");
219 } catch (InvalidFormatException $e) {
220 return $e->errorMessage();
221 }
162222 }
163223
164224 /**
@@ -167,10 +227,20 @@ class Folder
167227 * @param int $id
168228 *
169229 * @return array
230 *
231 * @throws InvalidFormatException
170232 */
171233 public function duplicate($id): array
172234 {
173 return $this->client->request('POST', "folders/{$id}/duplicate");
235 try {
236 if (! is_int($id)) {
237 throw new InvalidFormatException();
238 }
239
240 return $this->client->request('POST', "folders/{$id}/duplicate");
241 } catch (InvalidFormatException $e) {
242 return $e->errorMessage();
243 }
174244 }
175245
176246 /**
@@ -179,10 +249,20 @@ class Folder
179249 * @param int $id
180250 *
181251 * @return array
252 *
253 * @throws InvalidFormatException
182254 */
183255 public function addFolderToMedia($id, $name): array
184256 {
185 return $this->client->request('POST', "media/{$id}/folders", $name);
257 try {
258 if (! is_int($id)) {
259 throw new InvalidFormatException();
260 }
261
262 return $this->client->request('POST', "media/{$id}/folders", $name);
263 } catch (InvalidFormatException $e) {
264 return $e->errorMessage();
265 }
186266 }
187267
188268 /**
@@ -192,10 +272,20 @@ class Folder
192272 * @param int $folderId
193273 *
194274 * @return null
275 *
276 * @throws InvalidFormatException
195277 */
196278 public function deleteFolderFromMedia($id, $folderId)
197279 {
198 return $this->client->request('DELETE', "media/{$id}/folders/{$folderId}");
280 try {
281 if (! is_int($id) || ! is_int($folderId)) {
282 throw new InvalidFormatException();
283 }
284
285 return $this->client->request('DELETE', "media/{$id}/folders/{$folderId}");
286 } catch (InvalidFormatException $e) {
287 return $e->errorMessage();
288 }
199289 }
200290
201291 /**