Browse Source

Minor refactor of Commands

Timothy J. Warren 10 months ago
parent
commit
70832f8b63
5 changed files with 112 additions and 47 deletions
  1. 4
    7
      console
  2. 14
    7
      src/Command/BaseCommand.php
  3. 3
    2
      src/Command/CacheClear.php
  4. 4
    3
      src/Command/CachePrime.php
  5. 87
    28
      src/Command/SyncLists.php

+ 4
- 7
console View File

@@ -5,6 +5,7 @@
5 5
 require_once __DIR__ . '/vendor/autoload.php';
6 6
 
7 7
 use Aviat\AnimeClient\Command;
8
+use ConsoleKit\Console;
8 9
 
9 10
 $_SERVER['HTTP_HOST'] = 'localhost';
10 11
 
@@ -13,14 +14,10 @@ $_SERVER['HTTP_HOST'] = 'localhost';
13 14
 // -----------------------------------------------------------------------------
14 15
 try
15 16
 {
16
-	(new \ConsoleKit\Console([
17
+	(new Console([
17 18
 		'cache:clear' => Command\CacheClear::class,
18
-		'cache:prime' => Command\CachePrime::class,
19
-		'lists:sync' => Command\SyncKitsuWithMal::class,
20
-		'cache-prime' => Command\CachePrime::class,
21
-		'cache-clear' => Command\CacheClear::class,
22
-		'clear-cache' => Command\CacheClear::class,
23
-		'sync-lists' => Command\SyncKitsuWithMal::class,
19
+		'cache:refresh' => Command\CachePrime::class,
20
+		'lists:sync' => Command\SyncLists::class,
24 21
 	]))->run();
25 22
 }
26 23
 catch (\Exception $e)

+ 14
- 7
src/Command/BaseCommand.php View File

@@ -27,7 +27,7 @@ use Aviat\AnimeClient\API\MAL\MALRequestBuilder;
27 27
 use Aviat\Banker\Pool;
28 28
 use Aviat\Ion\Config;
29 29
 use Aviat\Ion\Di\{Container, ContainerAware};
30
-use ConsoleKit\Command;
30
+use ConsoleKit\{Command, ConsoleException};
31 31
 use ConsoleKit\Widgets\Box;
32 32
 use Monolog\Handler\RotatingFileHandler;
33 33
 use Monolog\Logger;
@@ -47,10 +47,17 @@ class BaseCommand extends Command {
47 47
 	 */
48 48
 	protected function echoBox($message)
49 49
 	{
50
-		echo "\n";
51
-		$box = new Box($this->getConsole(), $message);
52
-		$box->write();
53
-		echo "\n";
50
+		try
51
+		{
52
+			echo "\n";
53
+			$box = new Box($this->getConsole(), $message);
54
+			$box->write();
55
+			echo "\n";
56
+		}
57
+		catch (ConsoleException $e)
58
+		{
59
+			// oops
60
+		}
54 61
 	}
55 62
 
56 63
 	/**
@@ -58,12 +65,12 @@ class BaseCommand extends Command {
58 65
 	 *
59 66
 	 * @return Container
60 67
 	 */
61
-	protected function setupContainer()
68
+	protected function setupContainer(): Container
62 69
 	{
63 70
 		$APP_DIR = realpath(__DIR__ . '/../../app');
64 71
 		$APPCONF_DIR = realpath("{$APP_DIR}/appConf/");
65 72
 		$CONF_DIR = realpath("{$APP_DIR}/config/");
66
-		$base_config = require_once $APPCONF_DIR . '/base_config.php';
73
+		$base_config = require $APPCONF_DIR . '/base_config.php';
67 74
 
68 75
 		$config = loadToml($CONF_DIR);
69 76
 		$config_array = array_merge($base_config, $config);

+ 3
- 2
src/Command/CacheClear.php View File

@@ -25,10 +25,11 @@ class CacheClear extends BaseCommand {
25 25
 	 *
26 26
 	 * @param array $args
27 27
 	 * @param array $options
28
+	 * @throws \Aviat\Ion\Di\ContainerException
29
+	 * @throws \Aviat\Ion\Di\NotFoundException
28 30
 	 * @return void
29
-	 * @throws \ConsoleKit\ConsoleException
30 31
 	 */
31
-	public function execute(array $args, array $options = [])
32
+	public function execute(array $args, array $options = []): void
32 33
 	{
33 34
 		$this->setContainer($this->setupContainer());
34 35
 		$cache = $this->container->get('cache');

+ 4
- 3
src/Command/CachePrime.php View File

@@ -25,10 +25,11 @@ class CachePrime extends BaseCommand {
25 25
 	 *
26 26
 	 * @param array $args
27 27
 	 * @param array $options
28
+	 * @throws \Aviat\Ion\Di\ContainerException
29
+	 * @throws \Aviat\Ion\Di\NotFoundException
28 30
 	 * @return void
29
-	 * @throws \ConsoleKit\ConsoleException
30 31
 	 */
31
-	public function execute(array $args, array $options = [])
32
+	public function execute(array $args, array $options = []): void
32 33
 	{
33 34
 		$this->setContainer($this->setupContainer());
34 35
 
@@ -42,7 +43,7 @@ class CachePrime extends BaseCommand {
42 43
 
43 44
 		$this->echoBox('Cache cleared, re-priming...');
44 45
 
45
-		if ( ! is_null($userId))
46
+		if ($userId !== NULL)
46 47
 		{
47 48
 			$userIdItem = $cache->getItem('kitsu-auth-token');
48 49
 			$userIdItem->set($userId);

src/Command/SyncKitsuWithMal.php → src/Command/SyncLists.php View File

@@ -27,11 +27,12 @@ use Aviat\AnimeClient\API\MAL\Transformer\{
27 27
 	AnimeListTransformer as ALT
28 28
 };
29 29
 use Aviat\Ion\Json;
30
+use DateTime;
30 31
 
31 32
 /**
32 33
  * Clears the API Cache
33 34
  */
34
-class SyncKitsuWithMal extends BaseCommand {
35
+class SyncLists extends BaseCommand {
35 36
 
36 37
 	/**
37 38
 	 * Model for making requests to Kitsu API
@@ -50,9 +51,11 @@ class SyncKitsuWithMal extends BaseCommand {
50 51
 	 *
51 52
 	 * @param array $args
52 53
 	 * @param array $options
54
+	 * @throws \Aviat\Ion\Di\ContainerException
55
+	 * @throws \Aviat\Ion\Di\NotFoundException
53 56
 	 * @return void
54 57
 	 */
55
-	public function execute(array $args, array $options = [])
58
+	public function execute(array $args, array $options = []): void
56 59
 	{
57 60
 		$this->setContainer($this->setupContainer());
58 61
 		$this->setCache($this->container->get('cache'));
@@ -63,7 +66,13 @@ class SyncKitsuWithMal extends BaseCommand {
63 66
 		$this->sync('manga');
64 67
 	}
65 68
 
66
-	public function sync(string $type)
69
+	/**
70
+	 * Attempt to synchronize external apis
71
+	 *
72
+	 * @param string $type anime|manga
73
+	 * @return void
74
+	 */
75
+	protected function sync(string $type): void
67 76
 	{
68 77
 		$uType = ucfirst($type);
69 78
 
@@ -124,7 +133,14 @@ class SyncKitsuWithMal extends BaseCommand {
124 133
 		}
125 134
 	}
126 135
 
127
-	public function filterMappings(array $includes, string $type = 'anime'): array
136
+	/**
137
+	 * Filter Kitsu mappings for the specified type
138
+	 *
139
+	 * @param array $includes
140
+	 * @param string $type
141
+	 * @return array
142
+	 */
143
+	protected function filterMappings(array $includes, string $type = 'anime'): array
128 144
 	{
129 145
 		$output = [];
130 146
 
@@ -139,20 +155,25 @@ class SyncKitsuWithMal extends BaseCommand {
139 155
 		return $output;
140 156
 	}
141 157
 
142
-	public function formatMALList(string $type): array
158
+	/**
159
+	 * Format a MAL list for comparison
160
+	 *
161
+	 * @param string $type
162
+	 * @return array
163
+	 */
164
+	protected function formatMALList(string $type): array
143 165
 	{
144
-		if ($type === 'anime')
145
-		{
146
-			return $this->formatMALAnimeList();
147
-		}
148
-
149
-		if ($type === 'manga')
150
-		{
151
-			return $this->formatMALMangaList();
152
-		}
166
+		$type = ucfirst($type);
167
+		$method = "formatMAL{$type}List";
168
+		return $this->$method();
153 169
 	}
154 170
 
155
-	public function formatMALAnimeList()
171
+	/**
172
+	 * Format a MAL anime list for comparison
173
+	 *
174
+	 * @return array
175
+	 */
176
+	protected function formatMALAnimeList(): array
156 177
 	{
157 178
 		$orig = $this->malModel->getList('anime');
158 179
 		$output = [];
@@ -191,7 +212,12 @@ class SyncKitsuWithMal extends BaseCommand {
191 212
 		return $output;
192 213
 	}
193 214
 
194
-	public function formatMALMangaList()
215
+	/**
216
+	 * Format a MAL manga list for comparison
217
+	 *
218
+	 * @return array
219
+	 */
220
+	protected function formatMALMangaList(): array
195 221
 	{
196 222
 		$orig = $this->malModel->getList('manga');
197 223
 		$output = [];
@@ -232,7 +258,13 @@ class SyncKitsuWithMal extends BaseCommand {
232 258
 		return $output;
233 259
 	}
234 260
 
235
-	public function formatKitsuList(string $type = 'anime'): array
261
+	/**
262
+	 * Format a kitsu list for the sake of comparision
263
+	 *
264
+	 * @param string $type
265
+	 * @return array
266
+	 */
267
+	protected function formatKitsuList(string $type = 'anime'): array
236 268
 	{
237 269
 		$data = $this->kitsuModel->{'getFull' . ucfirst($type) . 'List'}();
238 270
 
@@ -262,7 +294,7 @@ class SyncKitsuWithMal extends BaseCommand {
262 294
 			}
263 295
 
264 296
 			// Skip to the next item if there isn't a MAL ID
265
-			if (is_null($malId))
297
+			if ($malId === NULL)
266 298
 			{
267 299
 				continue;
268 300
 			}
@@ -277,7 +309,13 @@ class SyncKitsuWithMal extends BaseCommand {
277 309
 		return $output;
278 310
 	}
279 311
 
280
-	public function diffLists(string $type = 'anime'): array
312
+	/**
313
+	 * Go through lists of the specified type, and determine what kind of action each item needs
314
+	 *
315
+	 * @param string $type
316
+	 * @return array
317
+	 */
318
+	protected function diffLists(string $type = 'anime'): array
281 319
 	{
282 320
 		// Get libraryEntries with media.mappings from Kitsu
283 321
 		// Organize mappings, and ignore entries without mappings
@@ -305,21 +343,21 @@ class SyncKitsuWithMal extends BaseCommand {
305 343
 
306 344
 		foreach($kitsuList as $kitsuItem)
307 345
 		{
308
-			if (in_array($kitsuItem['malId'], $malIds))
346
+			if (\in_array($kitsuItem['malId'], $malIds, TRUE))
309 347
 			{
310 348
 				$item = $this->compareListItems($kitsuItem, $malList[$kitsuItem['malId']]);
311 349
 
312
-				if (is_null($item))
350
+				if ($item === NULL)
313 351
 				{
314 352
 					continue;
315 353
 				}
316 354
 
317
-				if (in_array('kitsu', $item['updateType']))
355
+				if (\in_array('kitsu', $item['updateType'], TRUE))
318 356
 				{
319 357
 					$kitsuUpdateItems[] = $item['data'];
320 358
 				}
321 359
 
322
-				if (in_array('mal', $item['updateType']))
360
+				if (\in_array('mal', $item['updateType'], TRUE))
323 361
 				{
324 362
 					$malUpdateItems[] = $item['data'];
325 363
 				}
@@ -343,11 +381,18 @@ class SyncKitsuWithMal extends BaseCommand {
343 381
 		];
344 382
 	}
345 383
 
346
-	public function compareListItems(array $kitsuItem, array $malItem)
384
+	/**
385
+	 * Compare two list items, and return the out of date one, if one exists
386
+	 *
387
+	 * @param array $kitsuItem
388
+	 * @param array $malItem
389
+	 * @return array|null
390
+	 */
391
+	protected function compareListItems(array $kitsuItem, array $malItem): ?array
347 392
 	{
348 393
 		$compareKeys = ['status', 'progress', 'rating', 'reconsuming'];
349 394
 		$diff = [];
350
-		$dateDiff = (new \DateTime($kitsuItem['data']['updatedAt'])) <=> (new \DateTime($malItem['data']['updatedAt']));
395
+		$dateDiff = new DateTime($kitsuItem['data']['updatedAt']) <=> new DateTime($malItem['data']['updatedAt']);
351 396
 
352 397
 		foreach($compareKeys as $key)
353 398
 		{
@@ -359,7 +404,7 @@ class SyncKitsuWithMal extends BaseCommand {
359 404
 		$diffValues = array_unique($diffValues);
360 405
 		if (count($diffValues) === 1 && $diffValues[0] === 0)
361 406
 		{
362
-			return;
407
+			return NULL;
363 408
 		}
364 409
 
365 410
 		$update = [
@@ -460,7 +505,14 @@ class SyncKitsuWithMal extends BaseCommand {
460 505
 		return $return;
461 506
 	}
462 507
 
463
-	public function updateKitsuListItems($itemsToUpdate, string $action = 'update', string $type = 'anime'): void
508
+	/**
509
+	 * Create/Update list items on Kitsu
510
+	 *
511
+	 * @param array $itemsToUpdate
512
+	 * @param string $action
513
+	 * @param string $type
514
+	 */
515
+	protected function updateKitsuListItems(array $itemsToUpdate, string $action = 'update', string $type = 'anime'): void
464 516
 	{
465 517
 		$requester = new ParallelAPIRequest();
466 518
 		foreach($itemsToUpdate as $item)
@@ -496,7 +548,14 @@ class SyncKitsuWithMal extends BaseCommand {
496 548
 		}
497 549
 	}
498 550
 
499
-	public function updateMALListItems($itemsToUpdate, string $action = 'update', string $type = 'anime'): void
551
+	/**
552
+	 * Create/Update list items on MAL
553
+	 *
554
+	 * @param array $itemsToUpdate
555
+	 * @param string $action
556
+	 * @param string $type
557
+	 */
558
+	protected function updateMALListItems(array$itemsToUpdate, string $action = 'update', string $type = 'anime'): void
500 559
 	{
501 560
 		$transformer = new ALT();
502 561
 		$requester = new ParallelAPIRequest();