ReviewOS

also looking at this

meemalabs/meema-client-php

Exceptions

#27
Merged glennmichael123 wants to merge exceptions into main
5 files +168 -14
src/Exceptions/InvalidFormatException.phpmodified+13-1
Changes to src/Exceptions/InvalidFormatException.php
@@ -2,6 +2,18 @@
22
33namespace Meema\MeemaApi\Exceptions;
44
5class InvalidFormatException
5use Exception;
6
7class InvalidFormatException extends Exception
68{
9 /**
10 * Construct exception.
11 *
12 * @param string $message
13 */
14 public function __construct(string $message = null)
15 {
16 parent::__construct($message ?? 'Error on line '.$this->getLine().' in '.$this->getFile()
17 .' ID is not a valid integer');
18 }
719}
src/Models/Favorite.phpmodified+17-0
Changes to src/Models/Favorite.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 Favorite
@@ -50,6 +51,10 @@ class Favorite
5051 return $this->all();
5152 }
5253
54 if (! is_int($id)) {
55 throw new InvalidFormatException();
56 }
57
5358 $ids = is_array($id) ? $id : func_get_args();
5459
5560 return $this->client->request('GET', 'favorites', ['favorite_ids' => $ids]);
@@ -64,6 +69,10 @@ class Favorite
6469 */
6570 public function find($id)
6671 {
72 if (! is_int($id)) {
73 throw new InvalidFormatException();
74 }
75
6776 $response = $this->client->request('GET', "favorites/${id}");
6877
6978 return new Response($this, $response);
@@ -91,6 +100,10 @@ class Favorite
91100 */
92101 public function update($id, $data): array
93102 {
103 if (! is_int($id)) {
104 throw new InvalidFormatException();
105 }
106
94107 return $this->client->request('PATCH', "favorites/{$id}", $data);
95108 }
96109
@@ -103,6 +116,10 @@ class Favorite
103116 */
104117 public function delete($id)
105118 {
119 if (! is_int($id)) {
120 throw new InvalidFormatException();
121 }
122
106123 return $this->client->request('DELETE', "favorites/{$id}");
107124 }
108125}
src/Models/Folder.phpmodified+58-5
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,6 +58,10 @@ class Folder
5558 return $this->all();
5659 }
5760
61 if (! is_int($id)) {
62 throw new InvalidFormatException();
63 }
64
5865 $ids = is_array($id) ? $id : func_get_args();
5966
6067 return $this->client->request('GET', 'folders', ['folder_ids' => $ids]);
@@ -66,9 +73,15 @@ class Folder
6673 * @param int $id
6774 *
6875 * @return Response
76 *
77 * @throws InvalidFormatException
6978 */
70 public function find($id): Response
79 public function find($id)
7180 {
81 if (! is_int($id)) {
82 throw new InvalidFormatException();
83 }
84
7285 $response = $this->client->request('GET', "folders/${id}");
7386
7487 return new Response($this, $response);
@@ -97,13 +110,17 @@ class Folder
97110 /**
98111 * Update folder.
99112 *
100 * @param string $name
101113 * @param int $id
114 * @param string $name
102115 *
103116 * @return array
104117 */
105118 public function update($id, $name): array
106119 {
120 if (! is_int($id)) {
121 throw new InvalidFormatException();
122 }
123
107124 $name = is_array($name) ? $name : compact('name');
108125
109126 return $this->client->request('PATCH', "folders/{$id}", $name);
@@ -115,9 +132,15 @@ class Folder
115132 * @param int $id
116133 *
117134 * @return null
135 *
136 * @throws InvalidFormatException
118137 */
119138 public function delete($id)
120139 {
140 if (! is_int($id)) {
141 throw new InvalidFormatException();
142 }
143
121144 if ($this->model) {
122145 return $this->deleteFolderFromMedia($this->model->getId(), $id);
123146 }
@@ -143,9 +166,15 @@ class Folder
143166 * @param int $id
144167 *
145168 * @return array
169 *
170 * @throws InvalidFormatException
146171 */
147 public function archive($id): array
172 public function archive($id)
148173 {
174 if (! is_int($id)) {
175 throw new InvalidFormatException();
176 }
177
149178 return $this->client->request('POST', "folders/{$id}/archive");
150179 }
151180
@@ -155,9 +184,15 @@ class Folder
155184 * @param int $id
156185 *
157186 * @return array
187 *
188 * @throws InvalidFormatException
158189 */
159 public function unarchive($id): array
190 public function unarchive($id)
160191 {
192 if (! is_int($id)) {
193 throw new InvalidFormatException();
194 }
195
161196 return $this->client->request('POST', "folders/{$id}/unarchive");
162197 }
163198
@@ -167,9 +202,15 @@ class Folder
167202 * @param int $id
168203 *
169204 * @return array
205 *
206 * @throws InvalidFormatException
170207 */
171208 public function duplicate($id): array
172209 {
210 if (! is_int($id)) {
211 throw new InvalidFormatException();
212 }
213
173214 return $this->client->request('POST', "folders/{$id}/duplicate");
174215 }
175216
@@ -179,9 +220,15 @@ class Folder
179220 * @param int $id
180221 *
181222 * @return array
223 *
224 * @throws InvalidFormatException
182225 */
183226 public function addFolderToMedia($id, $name): array
184227 {
228 if (! is_int($id)) {
229 throw new InvalidFormatException();
230 }
231
185232 return $this->client->request('POST', "media/{$id}/folders", $name);
186233 }
187234
@@ -192,9 +239,15 @@ class Folder
192239 * @param int $folderId
193240 *
194241 * @return null
242 *
243 * @throws InvalidFormatException
195244 */
196245 public function deleteFolderFromMedia($id, $folderId)
197246 {
247 if (! is_int($id) || ! is_int($folderId)) {
248 throw new InvalidFormatException();
249 }
250
198251 return $this->client->request('DELETE', "media/{$id}/folders/{$folderId}");
199252 }
200253
src/Models/Media.phpmodified+63-8
Changes to src/Models/Media.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 Media
@@ -46,6 +47,8 @@ class Media
4647 * Get specific media.
4748 *
4849 * @return array
50 *
51 * @throws InvalidFormatException
4952 */
5053 public function get($id = null)
5154 {
@@ -57,9 +60,13 @@ class Media
5760 return $this->all();
5861 }
5962
63 if (! is_int($id)) {
64 throw new InvalidFormatException();
65 }
66
6067 $ids = is_array($id) ? $id : func_get_args();
6168
62 return $this->client->request('GET', 'media/show', ['media_ids' => $ids]);
69 return $this->client->request('GET', 'media', ['media_ids' => $ids]);
6370 }
6471
6572 /**
@@ -68,9 +75,15 @@ class Media
6875 * @param int $id
6976 *
7077 * @return array
78 *
79 * @throws InvalidFormatException
7180 */
7281 public function find($id)
7382 {
83 if (! is_int($id)) {
84 throw new InvalidFormatException();
85 }
86
7487 $response = $this->client->request('GET', "media/${id}");
7588
7689 return new Response($this, $response);
@@ -97,9 +110,15 @@ class Media
97110 * @param string $name
98111 *
99112 * @return array
113 *
114 * @throws InvalidFormatException
100115 */
101 public function update($id, $name): array
116 public function update($id, $name)
102117 {
118 if (! is_int($id)) {
119 throw new InvalidFormatException();
120 }
121
103122 $name = is_array($name) ? $name : compact('name');
104123
105124 return $this->client->request('PATCH', "media/{$id}/file-name", $name);
@@ -111,9 +130,15 @@ class Media
111130 * @param int $id
112131 *
113132 * @return null
133 *
134 * @throws InvalidFormatException
114135 */
115 public function delete($id = null)
136 public function delete($id)
116137 {
138 if (! is_int($id)) {
139 throw new InvalidFormatException();
140 }
141
117142 return $this->client->request('DELETE', "media/{$id}", ['media_id' => $id]);
118143 }
119144
@@ -135,9 +160,15 @@ class Media
135160 * @param int $id
136161 *
137162 * @return array
163 *
164 * @throws InvalidFormatException
138165 */
139 public function archive($id = null): array
166 public function archive($id)
140167 {
168 if (! is_int($id)) {
169 throw new InvalidFormatException();
170 }
171
141172 return $this->client->request('POST', "media/{$id}/archive");
142173 }
143174
@@ -147,9 +178,15 @@ class Media
147178 * @param int $id
148179 *
149180 * @return array
181 *
182 * @throws InvalidFormatException
150183 */
151 public function unarchive($id): array
184 public function unarchive($id)
152185 {
186 if (! is_int($id)) {
187 throw new InvalidFormatException();
188 }
189
153190 return $this->client->request('POST', "media/{$id}/unarchive");
154191 }
155192
@@ -159,9 +196,15 @@ class Media
159196 * @param int $id
160197 *
161198 * @return array
199 *
200 * @throws InvalidFormatException
162201 */
163 public function makePrivate($id): array
202 public function makePrivate($id)
164203 {
204 if (! is_int($id)) {
205 throw new InvalidFormatException();
206 }
207
165208 return $this->client->request('PATCH', "media/{$id}/make-private");
166209 }
167210
@@ -171,9 +214,15 @@ class Media
171214 * @param int $id
172215 *
173216 * @return array
217 *
218 * @throws InvalidFormatException
174219 */
175 public function makePublic($id): array
220 public function makePublic($id)
176221 {
222 if (! is_int($id)) {
223 throw new InvalidFormatException();
224 }
225
177226 return $this->client->request('PATCH', "media/{$id}/make-public");
178227 }
179228
@@ -183,9 +232,15 @@ class Media
183232 * @param int $id
184233 *
185234 * @return array
235 *
236 * @throws InvalidFormatException
186237 */
187 public function duplicate($id): array
238 public function duplicate($id)
188239 {
240 if (! is_int($id)) {
241 throw new InvalidFormatException();
242 }
243
189244 return $this->client->request('POST', "media/{$id}/duplicate");
190245 }
191246
src/Models/Tag.phpmodified+17-0
Changes to src/Models/Tag.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 Tag
@@ -59,6 +60,10 @@ class Tag
5960 return $this->all();
6061 }
6162
63 if (! is_int($id)) {
64 throw new InvalidFormatException();
65 }
66
6267 $ids = is_array($id) ? $id : func_get_args();
6368
6469 return $this->client->request('GET', 'tags', ['tag_ids' => $ids]);
@@ -73,6 +78,10 @@ class Tag
7378 */
7479 public function find($id): Response
7580 {
81 if (! is_int($id)) {
82 throw new InvalidFormatException();
83 }
84
7685 $response = $this->client->request('GET', "tags/${id}");
7786
7887 return new Response($this, $response);
@@ -102,6 +111,10 @@ class Tag
102111 */
103112 public function update($id, $data): array
104113 {
114 if (! is_int($id)) {
115 throw new InvalidFormatException();
116 }
117
105118 return $this->client->request('PATCH', 'tags/color', $data);
106119 }
107120
@@ -114,6 +127,10 @@ class Tag
114127 */
115128 public function delete($id)
116129 {
130 if (! is_int($id)) {
131 throw new InvalidFormatException();
132 }
133
117134 return $this->client->request('DELETE', "tags/{$id}");
118135 }
119136