1 | <?php namespace Simondubois\UnsplashDownloader; |
||
8 | class Task |
||
9 | { |
||
10 | |||
11 | // |
||
12 | // Constants |
||
13 | // |
||
14 | |||
15 | /** |
||
16 | * Notification types |
||
17 | */ |
||
18 | const NOTIFY_INFO = 'info'; |
||
19 | const NOTIFY_COMMENT = 'comment'; |
||
20 | const NOTIFY_ERROR = 'error'; |
||
21 | |||
22 | /** |
||
23 | * Download status |
||
24 | */ |
||
25 | const DOWNLOAD_SUCCESS = 0; |
||
26 | const DOWNLOAD_SKIPPED = 1; |
||
27 | const DOWNLOAD_FAILED = 2; |
||
28 | |||
29 | |||
30 | |||
31 | // |
||
32 | // Attributes |
||
33 | // |
||
34 | |||
35 | /** |
||
36 | * History proxy |
||
37 | * @var History |
||
38 | */ |
||
39 | private $history; |
||
40 | |||
41 | /** |
||
42 | * Callback to call when notification arised : function ($message, $level = null) {}; |
||
43 | * @var callable |
||
44 | */ |
||
45 | private $notificationCallback; |
||
46 | |||
47 | /** |
||
48 | * Path where to download photos |
||
49 | * @var string |
||
50 | */ |
||
51 | private $destination; |
||
52 | |||
53 | /** |
||
54 | * Number of photos to download |
||
55 | * @var int |
||
56 | */ |
||
57 | private $quantity; |
||
58 | |||
59 | /** |
||
60 | * Category ID |
||
61 | * @var int |
||
62 | */ |
||
63 | private $category; |
||
64 | |||
65 | /** |
||
66 | * True if the task should only download featured photos |
||
67 | * @var bool |
||
68 | */ |
||
69 | private $featured; |
||
70 | |||
71 | |||
72 | |||
73 | // |
||
74 | // Getters |
||
75 | // |
||
76 | |||
77 | /** |
||
78 | * Get history proxy attribute. Instantiate it if null |
||
79 | * @return History Instance |
||
80 | */ |
||
81 | 18 | public function getHistoryInstance() |
|
89 | |||
90 | /** |
||
91 | * Get notification callback attribute |
||
92 | * @return callable function ($message, $level = null) {} |
||
93 | */ |
||
94 | 1 | public function getNotificationCallback() |
|
98 | |||
99 | /** |
||
100 | * Get destination attribute |
||
101 | * @return string Path to folder |
||
102 | */ |
||
103 | 1 | public function getDestination() |
|
107 | |||
108 | /** |
||
109 | * Get quantity attribute |
||
110 | * @return int Number of photos to download |
||
111 | */ |
||
112 | 1 | public function getQuantity() |
|
116 | |||
117 | /** |
||
118 | * Get category attribute |
||
119 | * @return int Category ID |
||
120 | */ |
||
121 | 1 | public function getCategory() |
|
125 | |||
126 | /** |
||
127 | * Get featured attribute |
||
128 | * @return bool True if the task should only download featured photos |
||
129 | */ |
||
130 | 2 | public function getFeatured() |
|
134 | |||
135 | /** |
||
136 | * Get history path attribute |
||
137 | * @return string Path to file |
||
138 | */ |
||
139 | 1 | public function getHistory() |
|
143 | |||
144 | |||
145 | |||
146 | // |
||
147 | // Setters |
||
148 | // |
||
149 | |||
150 | /** |
||
151 | * Task constructor (set non scalar attributes) |
||
152 | */ |
||
153 | 23 | public function __construct() |
|
158 | |||
159 | /** |
||
160 | * Set notification callback attribute |
||
161 | * @param callable $notificationCallback function ($message, $level = null) {} |
||
162 | */ |
||
163 | 3 | public function setNotificationCallback($notificationCallback) |
|
167 | |||
168 | /** |
||
169 | * Set destination attribute |
||
170 | * @param string $destination Path to folder |
||
171 | */ |
||
172 | 3 | public function setDestination($destination) |
|
176 | |||
177 | /** |
||
178 | * Set quantity attribute |
||
179 | * @param int $quantity Number of photos to download |
||
180 | */ |
||
181 | 6 | public function setQuantity($quantity) |
|
185 | |||
186 | /** |
||
187 | * Set category attribute |
||
188 | * @param int $category Number of photos to download |
||
189 | */ |
||
190 | 2 | public function setCategory($category) |
|
194 | |||
195 | /** |
||
196 | * Set featured attribute |
||
197 | * @param bool $featured True if the task should only download featured photos |
||
198 | */ |
||
199 | 2 | public function setFeatured($featured) |
|
203 | |||
204 | /** |
||
205 | * Set path attribute in history instance |
||
206 | * @param string $history Path to file |
||
207 | */ |
||
208 | 1 | public function setHistory($history) |
|
212 | |||
213 | |||
214 | |||
215 | // |
||
216 | // Notification |
||
217 | // |
||
218 | |||
219 | /** |
||
220 | * Call the notification callback when a notification arised |
||
221 | * @param string $message Message text |
||
222 | * @param string|null $level Message context |
||
223 | */ |
||
224 | 1 | public function notify($message, $level = null) |
|
229 | |||
230 | |||
231 | |||
232 | // |
||
233 | // Execution |
||
234 | // |
||
235 | |||
236 | /** |
||
237 | * Find photos and download them |
||
238 | * @return bool True if the execution is successful |
||
239 | */ |
||
240 | 2 | public function download() |
|
250 | |||
251 | /** |
||
252 | * List categories |
||
253 | * @return bool True if the execution is successful |
||
254 | */ |
||
255 | 1 | public function categories() |
|
261 | |||
262 | /** |
||
263 | * Request APi to get photos to downloads |
||
264 | * @param Unsplash $unsplash Proxy to Unsplash API |
||
265 | * @return string[] Photo download links indexed by ID |
||
266 | */ |
||
267 | 3 | public function getPhotos(Unsplash $unsplash) { |
|
281 | |||
282 | /** |
||
283 | * Download all photos |
||
284 | * @param string[] $photos Photo download links indexed by ID |
||
285 | * @return boolean True if all downloads are successful |
||
286 | */ |
||
287 | 2 | public function downloadAllPhotos($photos) |
|
299 | |||
300 | /** |
||
301 | * Download one photo |
||
302 | * @param string $id Photo id |
||
303 | * @param string $source Photo downlaod url |
||
304 | */ |
||
305 | 3 | public function downloadOnePhoto($id, $source) |
|
327 | |||
328 | /** |
||
329 | * List all categories returned by API |
||
330 | * @param Unsplash $unsplash Proxy to Unsplash API |
||
331 | * @return boolean True on success |
||
332 | */ |
||
333 | 1 | public function listCategories(Unsplash $unsplash) |
|
344 | |||
345 | /** |
||
346 | * Download file from source to destination |
||
347 | * @param string $source URL to download the file from |
||
348 | * @param string $destination Path to download the file to |
||
349 | * @return bool True if the copy is successful |
||
350 | * @codeCoverageIgnore |
||
351 | */ |
||
352 | public function copyFile($source, $destination) { |
||
355 | |||
356 | } |
||
357 |