also looking at this
Exceptions
#27
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.
| @@ -2,6 +2,20 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Meema\MeemaApi\Exceptions; |
| 4 | 4 | |
| 5 | class InvalidFormatException | |
| 5 | use Exception; | |
| 6 | ||
| 7 | class InvalidFormatException extends Exception | |
| 6 | 8 | { |
| 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 | } | |
| 7 | 21 | } |
| @@ -3,6 +3,7 @@ | ||
| 3 | 3 | namespace Meema\MeemaApi\Models; |
| 4 | 4 | |
| 5 | 5 | use Meema\MeemaApi\Client; |
| 6 | use Meema\MeemaApi\Exceptions\InvalidFormatException; | |
| 6 | 7 | use Meema\MeemaApi\Response\Response; |
| 7 | 8 | |
| 8 | 9 | class Folder |
| @@ -47,7 +48,9 @@ class Folder | ||
| 47 | 48 | * |
| 48 | 49 | * @param int $id |
| 49 | 50 | * |
| 50 | * @return array | |
| 51 | * @return mixed | |
| 52 | * | |
| 53 | * @throws InvalidFormatException | |
| 51 | 54 | */ |
| 52 | 55 | public function get($id = null) |
| 53 | 56 | { |
| @@ -55,7 +58,15 @@ class Folder | ||
| 55 | 58 | return $this->all(); |
| 56 | 59 | } |
| 57 | 60 | |
| 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 | } | |
| 59 | 70 | |
| 60 | 71 | return $this->client->request('GET', 'folders', ['folder_ids' => $ids]); |
| 61 | 72 | } |
| @@ -66,10 +77,21 @@ class Folder | ||
| 66 | 77 | * @param int $id |
| 67 | 78 | * |
| 68 | 79 | * @return Response |
| 80 | * | |
| 81 | * @throws InvalidFormatException | |
| 69 | 82 | */ |
| 70 | public function find($id): Response | |
| 83 | public function find($id) | |
| 71 | 84 | { |
| 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 | } | |
| 73 | 95 | |
| 74 | 96 | return new Response($this, $response); |
| 75 | 97 | } |
| @@ -97,14 +119,22 @@ class Folder | ||
| 97 | 119 | /** |
| 98 | 120 | * Update folder. |
| 99 | 121 | * |
| 100 | * @param string $name | |
| 101 | 122 | * @param int $id |
| 123 | * @param string $name | |
| 102 | 124 | * |
| 103 | 125 | * @return array |
| 104 | 126 | */ |
| 105 | 127 | public function update($id, $name): array |
| 106 | 128 | { |
| 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 | } | |
| 108 | 138 | |
| 109 | 139 | return $this->client->request('PATCH', "folders/{$id}", $name); |
| 110 | 140 | } |
| @@ -115,11 +145,21 @@ class Folder | ||
| 115 | 145 | * @param int $id |
| 116 | 146 | * |
| 117 | 147 | * @return null |
| 148 | * | |
| 149 | * @throws InvalidFormatException | |
| 118 | 150 | */ |
| 119 | 151 | public function delete($id) |
| 120 | 152 | { |
| 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(); | |
| 123 | 163 | } |
| 124 | 164 | |
| 125 | 165 | return $this->client->request('DELETE', "folders/{$id}"); |
| @@ -143,10 +183,20 @@ class Folder | ||
| 143 | 183 | * @param int $id |
| 144 | 184 | * |
| 145 | 185 | * @return array |
| 186 | * | |
| 187 | * @throws InvalidFormatException | |
| 146 | 188 | */ |
| 147 | public function archive($id): array | |
| 189 | public function archive($id) | |
| 148 | 190 | { |
| 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 | } | |
| 150 | 200 | } |
| 151 | 201 | |
| 152 | 202 | /** |
| @@ -155,10 +205,20 @@ class Folder | ||
| 155 | 205 | * @param int $id |
| 156 | 206 | * |
| 157 | 207 | * @return array |
| 208 | * | |
| 209 | * @throws InvalidFormatException | |
| 158 | 210 | */ |
| 159 | public function unarchive($id): array | |
| 211 | public function unarchive($id) | |
| 160 | 212 | { |
| 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 | } | |
| 162 | 222 | } |
| 163 | 223 | |
| 164 | 224 | /** |
| @@ -167,10 +227,20 @@ class Folder | ||
| 167 | 227 | * @param int $id |
| 168 | 228 | * |
| 169 | 229 | * @return array |
| 230 | * | |
| 231 | * @throws InvalidFormatException | |
| 170 | 232 | */ |
| 171 | 233 | public function duplicate($id): array |
| 172 | 234 | { |
| 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 | } | |
| 174 | 244 | } |
| 175 | 245 | |
| 176 | 246 | /** |
| @@ -179,10 +249,20 @@ class Folder | ||
| 179 | 249 | * @param int $id |
| 180 | 250 | * |
| 181 | 251 | * @return array |
| 252 | * | |
| 253 | * @throws InvalidFormatException | |
| 182 | 254 | */ |
| 183 | 255 | public function addFolderToMedia($id, $name): array |
| 184 | 256 | { |
| 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 | } | |
| 186 | 266 | } |
| 187 | 267 | |
| 188 | 268 | /** |
| @@ -192,10 +272,20 @@ class Folder | ||
| 192 | 272 | * @param int $folderId |
| 193 | 273 | * |
| 194 | 274 | * @return null |
| 275 | * | |
| 276 | * @throws InvalidFormatException | |
| 195 | 277 | */ |
| 196 | 278 | public function deleteFolderFromMedia($id, $folderId) |
| 197 | 279 | { |
| 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 | } | |
| 199 | 289 | } |
| 200 | 290 | |
| 201 | 291 | /** |