ReviewOS

also looking at this

meemalabs/meema-client-php

Media model

#6
Merged glennmichael123 wants to merge media-model into main
7 files +1700 -76

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/Models/Folder.phpmodified+109-12
Changes to src/Models/Folder.php
@@ -3,27 +3,43 @@
33namespace Meema\MeemaApi\Models;
44
55use Meema\MeemaApi\Client;
6use Meema\MeemaApi\Response\Response;
7use Meema\MeemaApi\Traits\SerializesResponse;
68
79class Folder
810{
11 use SerializesResponse;
12
913 /**
1014 * @var Meema\MeemaApi\Client
1115 */
12 public $client;
16 protected $client;
17
18 /**
19 * @var int
20 */
21 protected $id;
1322
1423 /**
1524 * @var int
1625 */
17 public $id;
26 protected $mediaId;
27
28 /**
29 * @var array
30 */
31 protected $content;
1832
1933 /**
2034 * Construct Folder model.
2135 *
2236 * @param Meema\MeemaApi\Client $client
2337 */
24 public function __construct(Client $client)
38 public function __construct(Client $client, $mediaId = null)
2539 {
2640 $this->client = $client;
41
42 $this->mediaId = $mediaId;
2743 }
2844
2945 /**
@@ -39,6 +55,8 @@ class Folder
3955 /**
4056 * Get specific folders.
4157 *
58 * @param int $id
59 *
4260 * @return array
4361 */
4462 public function get($id = null)
@@ -49,7 +67,24 @@ class Folder
4967
5068 $ids = is_array($id) ? $id : func_get_args();
5169
52 return $this->client->request('GET', 'folders/show', ['folder_ids' => $ids]);
70 return $this->client->request('GET', 'folders', ['folder_ids' => $ids]);
71 }
72
73 /**
74 * Get specific folders.
75 *
76 * @param int $id
77 *
78 * @return array
79 */
80 public function find($id)
81 {
82 $response = $this->client->request('GET', "folders/${id}");
83
84 $this->content = $response;
85 $this->id = $response['data']['id'];
86
87 return new Response($this, $response);
5388 }
5489
5590 /**
@@ -59,23 +94,31 @@ class Folder
5994 *
6095 * @return array
6196 */
62 public function create($name): array
97 public function create($name)
6398 {
6499 $name = is_array($name) ? $name : compact('name');
65100
101 if ($this->mediaId) {
102 $folderName = ['folder_name' => $name['name']];
103
104 return $this->addFolderToMedia($this->mediaId, $folderName);
105 }
106
66107 return $this->client->request('POST', "folders/{$this->client->getAccessKey()}", $name);
67108 }
68109
69110 /**
70111 * Update folder.
71112 *
72 * @param int $id
73113 * @param string $name
114 * @param int $id
74115 *
75116 * @return array
76117 */
77 public function update($id, $name): array
118 public function update($name, $id = null): array
78119 {
120 $id = $this->id ?? $id;
121
79122 $name = is_array($name) ? $name : compact('name');
80123
81124 return $this->client->request('PATCH', "folders/{$id}", $name);
@@ -84,42 +127,96 @@ class Folder
84127 /**
85128 * Delete a folder.
86129 *
87 * @param int|array $id
130 * @param int $id
88131 *
89132 * @return null
90133 */
91 public function delete($id)
134 public function delete($id = null)
92135 {
136 if ($this->mediaId) {
137 return $this->deleteFolderFromMedia($this->mediaId, $id);
138 }
139
140 $id = $this->id ?? $id;
141
93142 return $this->client->request('DELETE', "folders/{$id}");
94143 }
95144
96145 /**
97146 * Archive a folder.
98147 *
148 * @param int $id
149 *
99150 * @return array
100151 */
101 public function archive($id): array
152 public function archive($id = null): array
102153 {
154 $id = $this->id ?? $id;
155
103156 return $this->client->request('POST', "folders/{$id}/archive");
104157 }
105158
106159 /**
107160 * Unarchive a folder.
108161 *
162 * @param int $id
163 *
109164 * @return array
110165 */
111 public function unarchive($id): array
166 public function unarchive($id = null): array
112167 {
168 $id = $this->id ?? $id;
169
113170 return $this->client->request('POST', "folders/{$id}/unarchive");
114171 }
115172
116173 /**
117174 * Duplicate a folder.
118175 *
176 * @param int $id
177 *
119178 * @return array
120179 */
121 public function duplicate($id): array
180 public function duplicate($id = null): array
122181 {
182 $id = $this->id ?? $id;
183
123184 return $this->client->request('POST', "folders/{$id}/duplicate");
124185 }
186
187 /**
188 * Initialize the media model.
189 *
190 * @return Meema\MeemaApi\Models\Media
191 */
192 public function media(): Media
193 {
194 $client = new Client($this->client->getAccessKey());
195
196 return new Media($client, $this->id);
197 }
198
199 /**
200 * Add folder to media.
201 *
202 * @param int $id
203 *
204 * @return array
205 */
206 public function addFolderToMedia($id, $name)
207 {
208 return $this->client->request('POST', "media/{$id}/folders", $name);
209 }
210
211 /**
212 * Add folder to media.
213 *
214 * @param int $id
215 *
216 * @return array
217 */
218 public function deleteFolderFromMedia($mediaId, $folderId)
219 {
220 return $this->client->request('DELETE', "media/{$mediaId}/folders/{$folderId}");
221 }
125222}
src/Models/Media.phpmodified+97-12
Changes to src/Models/Media.php
@@ -3,27 +3,43 @@
33namespace Meema\MeemaApi\Models;
44
55use Meema\MeemaApi\Client;
6use Meema\MeemaApi\Response\Response;
7use Meema\MeemaApi\Traits\SerializesResponse;
68
79class Media
810{
11 use SerializesResponse;
12
913 /**
1014 * @var Meema\MeemaApi\Client
1115 */
12 public $client;
16 protected $client;
17
18 /**
19 * @var int
20 */
21 protected $id;
1322
1423 /**
1524 * @var int
1625 */
17 public $id;
26 protected $folderId;
27
28 /**
29 * @var array
30 */
31 protected $content;
1832
1933 /**
2034 * Construct media model.
2135 *
2236 * @param Meema\MeemaApi\Client $client
2337 */
24 public function __construct(Client $client)
38 public function __construct(Client $client, $folderId = null)
2539 {
2640 $this->client = $client;
41
42 $this->folderId = $folderId;
2743 }
2844
2945 /**
@@ -43,6 +59,10 @@ class Media
4359 */
4460 public function get($id = null)
4561 {
62 if ($this->folderId) {
63 return $this->fetchMediaForFolder($this->folderId);
64 }
65
4666 if (! $id) {
4767 return $this->all();
4868 }
@@ -52,10 +72,27 @@ class Media
5272 return $this->client->request('GET', 'media/show', ['media_ids' => $ids]);
5373 }
5474
75 /**
76 * Get specific folders.
77 *
78 * @param int $id
79 *
80 * @return array
81 */
82 public function find($id)
83 {
84 $response = $this->client->request('GET', "folders/${id}");
85
86 $this->content = $response;
87 $this->id = $response['data']['id'];
88
89 return new Response($this, $response);
90 }
91
5592 /**
5693 * Create media.
5794 *
58 * @param string $name
95 * @param string $name
5996 *
6097 * @return array
6198 */
@@ -74,8 +111,10 @@ class Media
74111 *
75112 * @return array
76113 */
77 public function update($id, $name): array
114 public function update($name, $id = null): array
78115 {
116 $id = $this->id ?? $id;
117
79118 $name = is_array($name) ? $name : compact('name');
80119
81120 return $this->client->request('PATCH', "media/{$id}/file-name", $name);
@@ -84,62 +123,108 @@ class Media
84123 /**
85124 * Delete a media.
86125 *
87 * @param int|array $id
126 * @param int $id
88127 *
89128 * @return null
90129 */
91 public function delete($id)
130 public function delete($id = null)
92131 {
132 $id = $this->id ?? $id;
133
93134 return $this->client->request('DELETE', "media/{$id}", ['media_id' => $id]);
94135 }
95136
96137 /**
97138 * Archive a media.
98139 *
140 * @param int $id
141 *
99142 * @return array
100143 */
101 public function archive($id): array
144 public function archive($id = null): array
102145 {
146 $id = $this->id ?? $id;
147
103148 return $this->client->request('POST', "media/{$id}/archive");
104149 }
105150
106151 /**
107152 * Unarchive a media.
108153 *
154 * @param int $id
155 *
109156 * @return array
110157 */
111 public function unarchive($id): array
158 public function unarchive($id = null): array
112159 {
160 $id = $this->id ?? $id;
161
113162 return $this->client->request('POST', "media/{$id}/unarchive");
114163 }
115164
116165 /**
117166 * Make a media private.
118167 *
168 * @param int $id
169 *
119170 * @return array
120171 */
121 public function makePrivate($id): array
172 public function makePrivate($id = null): array
122173 {
174 $id = $this->id ?? $id;
175
123176 return $this->client->request('PATCH', "media/{$id}/make-private");
124177 }
125178
126179 /**
127180 * Make a media public.
128181 *
182 * @param int $id
183 *
129184 * @return array
130185 */
131 public function makePublic($id): array
186 public function makePublic($id = null): array
132187 {
188 $id = $this->id ?? $id;
189
133190 return $this->client->request('PATCH', "media/{$id}/make-public");
134191 }
135192
136193 /**
137194 * Duplicate a media.
138195 *
196 * @param int $id
197 *
139198 * @return array
140199 */
141 public function duplicate($id): array
200 public function duplicate($id = null): array
142201 {
202 $id = $this->id ?? $id;
203
143204 return $this->client->request('POST', "media/{$id}/duplicate");
144205 }
206
207 /**
208 * Initialize the folder model.
209 *
210 * @return Meema\MeemaApi\Models\Folder
211 */
212 public function folders(): Folder
213 {
214 $client = new Client($this->client->getAccessKey());
215
216 return new Folder($client, $this->id);
217 }
218
219 /**
220 * Fetch the media for the folder.
221 *
222 * @param int $id
223 *
224 * @return array
225 */
226 public function fetchMediaForFolder($id): array
227 {
228 return $this->client->request('GET', "folders/{$id}/media");
229 }
145230}
src/Response/Response.phpadded+28-0
Changes to src/Response/Response.php
@@ -0,0 +1,28 @@
1<?php
2
3namespace Meema\MeemaApi\Response;
4
5use Meema\MeemaApi\Traits\ForwardsCalls;
6
7class Response
8{
9 use ForwardsCalls;
10
11 protected $api;
12
13 public function __construct($api)
14 {
15 $this->api = $api;
16 }
17 /**
18 * Dynamically handle calls into the query instance.
19 *
20 * @param string $method
21 * @param array $parameters
22 * @return mixed
23 */
24 public function __call($method, $parameters)
25 {
26 return $this->forwardCallTo($this->api, $method, $parameters);
27 }
28}
src/Traits/ForwardsCalls.phpadded+54-0
Changes to src/Traits/ForwardsCalls.php
@@ -0,0 +1,54 @@
1<?php
2
3namespace Meema\MeemaApi\Traits;
4
5use BadMethodCallException;
6use Error;
7
8trait ForwardsCalls
9{
10 /**
11 * Forward a method call to the given object.
12 *
13 * @param mixed $object
14 * @param string $method
15 * @param array $parameters
16 * @return mixed
17 *
18 * @throws \BadMethodCallException
19 */
20 protected function forwardCallTo($object, $method, $parameters)
21 {
22 try {
23 return $object->{$method}(...$parameters);
24 } catch (Error | BadMethodCallException $e) {
25 $pattern = '~^Call to undefined method (?P<class>[^:]+)::(?P<method>[^\(]+)\(\)$~';
26
27 if (! preg_match($pattern, $e->getMessage(), $matches)) {
28 throw $e;
29 }
30
31 if ($matches['class'] != get_class($object) ||
32 $matches['method'] != $method) {
33 throw $e;
34 }
35
36 static::throwBadMethodCallException($method);
37 }
38 }
39
40 /**
41 * Throw a bad method call exception for the given method.
42 *
43 * @param string $method
44 * @return void
45 *
46 * @throws \BadMethodCallException
47 */
48 protected static function throwBadMethodCallException($method)
49 {
50 throw new BadMethodCallException(sprintf(
51 'Call to undefined method %s::%s()', static::class, $method
52 ));
53 }
54}
src/Traits/SerializesResponse.phpadded+16-0
Changes to src/Traits/SerializesResponse.php
@@ -0,0 +1,16 @@
1<?php
2
3namespace Meema\MeemaApi\Traits;
4
5trait SerializesResponse
6{
7 /**
8 * Convert the response to array
9 *
10 * @return array
11 */
12 public function toArray()
13 {
14 return $this->content;
15 }
16}