Minor refactor of Commands

This commit is contained in:
Timothy Warren 2018-01-31 15:44:48 -05:00
parent 79f6ae8db7
commit 70832f8b63
5 changed files with 112 additions and 47 deletions

11
console
View File

@ -5,6 +5,7 @@
require_once __DIR__ . '/vendor/autoload.php'; require_once __DIR__ . '/vendor/autoload.php';
use Aviat\AnimeClient\Command; use Aviat\AnimeClient\Command;
use ConsoleKit\Console;
$_SERVER['HTTP_HOST'] = 'localhost'; $_SERVER['HTTP_HOST'] = 'localhost';
@ -13,14 +14,10 @@ $_SERVER['HTTP_HOST'] = 'localhost';
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
try try
{ {
(new \ConsoleKit\Console([ (new Console([
'cache:clear' => Command\CacheClear::class, 'cache:clear' => Command\CacheClear::class,
'cache:prime' => Command\CachePrime::class, 'cache:refresh' => Command\CachePrime::class,
'lists:sync' => Command\SyncKitsuWithMal::class, 'lists:sync' => Command\SyncLists::class,
'cache-prime' => Command\CachePrime::class,
'cache-clear' => Command\CacheClear::class,
'clear-cache' => Command\CacheClear::class,
'sync-lists' => Command\SyncKitsuWithMal::class,
]))->run(); ]))->run();
} }
catch (\Exception $e) catch (\Exception $e)

View File

@ -27,7 +27,7 @@ use Aviat\AnimeClient\API\MAL\MALRequestBuilder;
use Aviat\Banker\Pool; use Aviat\Banker\Pool;
use Aviat\Ion\Config; use Aviat\Ion\Config;
use Aviat\Ion\Di\{Container, ContainerAware}; use Aviat\Ion\Di\{Container, ContainerAware};
use ConsoleKit\Command; use ConsoleKit\{Command, ConsoleException};
use ConsoleKit\Widgets\Box; use ConsoleKit\Widgets\Box;
use Monolog\Handler\RotatingFileHandler; use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger; use Monolog\Logger;
@ -46,24 +46,31 @@ class BaseCommand extends Command {
* @return void * @return void
*/ */
protected function echoBox($message) protected function echoBox($message)
{
try
{ {
echo "\n"; echo "\n";
$box = new Box($this->getConsole(), $message); $box = new Box($this->getConsole(), $message);
$box->write(); $box->write();
echo "\n"; echo "\n";
} }
catch (ConsoleException $e)
{
// oops
}
}
/** /**
* Setup the Di container * Setup the Di container
* *
* @return Container * @return Container
*/ */
protected function setupContainer() protected function setupContainer(): Container
{ {
$APP_DIR = realpath(__DIR__ . '/../../app'); $APP_DIR = realpath(__DIR__ . '/../../app');
$APPCONF_DIR = realpath("{$APP_DIR}/appConf/"); $APPCONF_DIR = realpath("{$APP_DIR}/appConf/");
$CONF_DIR = realpath("{$APP_DIR}/config/"); $CONF_DIR = realpath("{$APP_DIR}/config/");
$base_config = require_once $APPCONF_DIR . '/base_config.php'; $base_config = require $APPCONF_DIR . '/base_config.php';
$config = loadToml($CONF_DIR); $config = loadToml($CONF_DIR);
$config_array = array_merge($base_config, $config); $config_array = array_merge($base_config, $config);

View File

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

View File

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

View File

@ -27,11 +27,12 @@ use Aviat\AnimeClient\API\MAL\Transformer\{
AnimeListTransformer as ALT AnimeListTransformer as ALT
}; };
use Aviat\Ion\Json; use Aviat\Ion\Json;
use DateTime;
/** /**
* Clears the API Cache * Clears the API Cache
*/ */
class SyncKitsuWithMal extends BaseCommand { class SyncLists extends BaseCommand {
/** /**
* Model for making requests to Kitsu API * Model for making requests to Kitsu API
@ -50,9 +51,11 @@ class SyncKitsuWithMal extends BaseCommand {
* *
* @param array $args * @param array $args
* @param array $options * @param array $options
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @return void * @return void
*/ */
public function execute(array $args, array $options = []) public function execute(array $args, array $options = []): void
{ {
$this->setContainer($this->setupContainer()); $this->setContainer($this->setupContainer());
$this->setCache($this->container->get('cache')); $this->setCache($this->container->get('cache'));
@ -63,7 +66,13 @@ class SyncKitsuWithMal extends BaseCommand {
$this->sync('manga'); $this->sync('manga');
} }
public function sync(string $type) /**
* Attempt to synchronize external apis
*
* @param string $type anime|manga
* @return void
*/
protected function sync(string $type): void
{ {
$uType = ucfirst($type); $uType = ucfirst($type);
@ -124,7 +133,14 @@ class SyncKitsuWithMal extends BaseCommand {
} }
} }
public function filterMappings(array $includes, string $type = 'anime'): array /**
* Filter Kitsu mappings for the specified type
*
* @param array $includes
* @param string $type
* @return array
*/
protected function filterMappings(array $includes, string $type = 'anime'): array
{ {
$output = []; $output = [];
@ -139,20 +155,25 @@ class SyncKitsuWithMal extends BaseCommand {
return $output; return $output;
} }
public function formatMALList(string $type): array /**
* Format a MAL list for comparison
*
* @param string $type
* @return array
*/
protected function formatMALList(string $type): array
{ {
if ($type === 'anime') $type = ucfirst($type);
{ $method = "formatMAL{$type}List";
return $this->formatMALAnimeList(); return $this->$method();
} }
if ($type === 'manga') /**
{ * Format a MAL anime list for comparison
return $this->formatMALMangaList(); *
} * @return array
} */
protected function formatMALAnimeList(): array
public function formatMALAnimeList()
{ {
$orig = $this->malModel->getList('anime'); $orig = $this->malModel->getList('anime');
$output = []; $output = [];
@ -191,7 +212,12 @@ class SyncKitsuWithMal extends BaseCommand {
return $output; return $output;
} }
public function formatMALMangaList() /**
* Format a MAL manga list for comparison
*
* @return array
*/
protected function formatMALMangaList(): array
{ {
$orig = $this->malModel->getList('manga'); $orig = $this->malModel->getList('manga');
$output = []; $output = [];
@ -232,7 +258,13 @@ class SyncKitsuWithMal extends BaseCommand {
return $output; return $output;
} }
public function formatKitsuList(string $type = 'anime'): array /**
* Format a kitsu list for the sake of comparision
*
* @param string $type
* @return array
*/
protected function formatKitsuList(string $type = 'anime'): array
{ {
$data = $this->kitsuModel->{'getFull' . ucfirst($type) . 'List'}(); $data = $this->kitsuModel->{'getFull' . ucfirst($type) . 'List'}();
@ -262,7 +294,7 @@ class SyncKitsuWithMal extends BaseCommand {
} }
// Skip to the next item if there isn't a MAL ID // Skip to the next item if there isn't a MAL ID
if (is_null($malId)) if ($malId === NULL)
{ {
continue; continue;
} }
@ -277,7 +309,13 @@ class SyncKitsuWithMal extends BaseCommand {
return $output; return $output;
} }
public function diffLists(string $type = 'anime'): array /**
* Go through lists of the specified type, and determine what kind of action each item needs
*
* @param string $type
* @return array
*/
protected function diffLists(string $type = 'anime'): array
{ {
// Get libraryEntries with media.mappings from Kitsu // Get libraryEntries with media.mappings from Kitsu
// Organize mappings, and ignore entries without mappings // Organize mappings, and ignore entries without mappings
@ -305,21 +343,21 @@ class SyncKitsuWithMal extends BaseCommand {
foreach($kitsuList as $kitsuItem) foreach($kitsuList as $kitsuItem)
{ {
if (in_array($kitsuItem['malId'], $malIds)) if (\in_array($kitsuItem['malId'], $malIds, TRUE))
{ {
$item = $this->compareListItems($kitsuItem, $malList[$kitsuItem['malId']]); $item = $this->compareListItems($kitsuItem, $malList[$kitsuItem['malId']]);
if (is_null($item)) if ($item === NULL)
{ {
continue; continue;
} }
if (in_array('kitsu', $item['updateType'])) if (\in_array('kitsu', $item['updateType'], TRUE))
{ {
$kitsuUpdateItems[] = $item['data']; $kitsuUpdateItems[] = $item['data'];
} }
if (in_array('mal', $item['updateType'])) if (\in_array('mal', $item['updateType'], TRUE))
{ {
$malUpdateItems[] = $item['data']; $malUpdateItems[] = $item['data'];
} }
@ -343,11 +381,18 @@ class SyncKitsuWithMal extends BaseCommand {
]; ];
} }
public function compareListItems(array $kitsuItem, array $malItem) /**
* Compare two list items, and return the out of date one, if one exists
*
* @param array $kitsuItem
* @param array $malItem
* @return array|null
*/
protected function compareListItems(array $kitsuItem, array $malItem): ?array
{ {
$compareKeys = ['status', 'progress', 'rating', 'reconsuming']; $compareKeys = ['status', 'progress', 'rating', 'reconsuming'];
$diff = []; $diff = [];
$dateDiff = (new \DateTime($kitsuItem['data']['updatedAt'])) <=> (new \DateTime($malItem['data']['updatedAt'])); $dateDiff = new DateTime($kitsuItem['data']['updatedAt']) <=> new DateTime($malItem['data']['updatedAt']);
foreach($compareKeys as $key) foreach($compareKeys as $key)
{ {
@ -359,7 +404,7 @@ class SyncKitsuWithMal extends BaseCommand {
$diffValues = array_unique($diffValues); $diffValues = array_unique($diffValues);
if (count($diffValues) === 1 && $diffValues[0] === 0) if (count($diffValues) === 1 && $diffValues[0] === 0)
{ {
return; return NULL;
} }
$update = [ $update = [
@ -460,7 +505,14 @@ class SyncKitsuWithMal extends BaseCommand {
return $return; return $return;
} }
public function updateKitsuListItems($itemsToUpdate, string $action = 'update', string $type = 'anime'): void /**
* Create/Update list items on Kitsu
*
* @param array $itemsToUpdate
* @param string $action
* @param string $type
*/
protected function updateKitsuListItems(array $itemsToUpdate, string $action = 'update', string $type = 'anime'): void
{ {
$requester = new ParallelAPIRequest(); $requester = new ParallelAPIRequest();
foreach($itemsToUpdate as $item) foreach($itemsToUpdate as $item)
@ -496,7 +548,14 @@ class SyncKitsuWithMal extends BaseCommand {
} }
} }
public function updateMALListItems($itemsToUpdate, string $action = 'update', string $type = 'anime'): void /**
* Create/Update list items on MAL
*
* @param array $itemsToUpdate
* @param string $action
* @param string $type
*/
protected function updateMALListItems(array$itemsToUpdate, string $action = 'update', string $type = 'anime'): void
{ {
$transformer = new ALT(); $transformer = new ALT();
$requester = new ParallelAPIRequest(); $requester = new ParallelAPIRequest();