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';
use Aviat\AnimeClient\Command;
use ConsoleKit\Console;
$_SERVER['HTTP_HOST'] = 'localhost';
@ -13,14 +14,10 @@ $_SERVER['HTTP_HOST'] = 'localhost';
// -----------------------------------------------------------------------------
try
{
(new \ConsoleKit\Console([
(new Console([
'cache:clear' => Command\CacheClear::class,
'cache:prime' => Command\CachePrime::class,
'lists:sync' => Command\SyncKitsuWithMal::class,
'cache-prime' => Command\CachePrime::class,
'cache-clear' => Command\CacheClear::class,
'clear-cache' => Command\CacheClear::class,
'sync-lists' => Command\SyncKitsuWithMal::class,
'cache:refresh' => Command\CachePrime::class,
'lists:sync' => Command\SyncLists::class,
]))->run();
}
catch (\Exception $e)

View File

@ -27,7 +27,7 @@ use Aviat\AnimeClient\API\MAL\MALRequestBuilder;
use Aviat\Banker\Pool;
use Aviat\Ion\Config;
use Aviat\Ion\Di\{Container, ContainerAware};
use ConsoleKit\Command;
use ConsoleKit\{Command, ConsoleException};
use ConsoleKit\Widgets\Box;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
@ -47,10 +47,17 @@ class BaseCommand extends Command {
*/
protected function echoBox($message)
{
echo "\n";
$box = new Box($this->getConsole(), $message);
$box->write();
echo "\n";
try
{
echo "\n";
$box = new Box($this->getConsole(), $message);
$box->write();
echo "\n";
}
catch (ConsoleException $e)
{
// oops
}
}
/**
@ -58,12 +65,12 @@ class BaseCommand extends Command {
*
* @return Container
*/
protected function setupContainer()
protected function setupContainer(): Container
{
$APP_DIR = realpath(__DIR__ . '/../../app');
$APPCONF_DIR = realpath("{$APP_DIR}/appConf/");
$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_array = array_merge($base_config, $config);

View File

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

View File

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

View File

@ -27,11 +27,12 @@ use Aviat\AnimeClient\API\MAL\Transformer\{
AnimeListTransformer as ALT
};
use Aviat\Ion\Json;
use DateTime;
/**
* Clears the API Cache
*/
class SyncKitsuWithMal extends BaseCommand {
class SyncLists extends BaseCommand {
/**
* Model for making requests to Kitsu API
@ -50,9 +51,11 @@ class SyncKitsuWithMal extends BaseCommand {
*
* @param array $args
* @param array $options
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @return void
*/
public function execute(array $args, array $options = [])
public function execute(array $args, array $options = []): void
{
$this->setContainer($this->setupContainer());
$this->setCache($this->container->get('cache'));
@ -63,7 +66,13 @@ class SyncKitsuWithMal extends BaseCommand {
$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);
@ -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 = [];
@ -139,20 +155,25 @@ class SyncKitsuWithMal extends BaseCommand {
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')
{
return $this->formatMALAnimeList();
}
if ($type === 'manga')
{
return $this->formatMALMangaList();
}
$type = ucfirst($type);
$method = "formatMAL{$type}List";
return $this->$method();
}
public function formatMALAnimeList()
/**
* Format a MAL anime list for comparison
*
* @return array
*/
protected function formatMALAnimeList(): array
{
$orig = $this->malModel->getList('anime');
$output = [];
@ -191,7 +212,12 @@ class SyncKitsuWithMal extends BaseCommand {
return $output;
}
public function formatMALMangaList()
/**
* Format a MAL manga list for comparison
*
* @return array
*/
protected function formatMALMangaList(): array
{
$orig = $this->malModel->getList('manga');
$output = [];
@ -232,7 +258,13 @@ class SyncKitsuWithMal extends BaseCommand {
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'}();
@ -262,7 +294,7 @@ class SyncKitsuWithMal extends BaseCommand {
}
// Skip to the next item if there isn't a MAL ID
if (is_null($malId))
if ($malId === NULL)
{
continue;
}
@ -277,7 +309,13 @@ class SyncKitsuWithMal extends BaseCommand {
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
// Organize mappings, and ignore entries without mappings
@ -305,21 +343,21 @@ class SyncKitsuWithMal extends BaseCommand {
foreach($kitsuList as $kitsuItem)
{
if (in_array($kitsuItem['malId'], $malIds))
if (\in_array($kitsuItem['malId'], $malIds, TRUE))
{
$item = $this->compareListItems($kitsuItem, $malList[$kitsuItem['malId']]);
if (is_null($item))
if ($item === NULL)
{
continue;
}
if (in_array('kitsu', $item['updateType']))
if (\in_array('kitsu', $item['updateType'], TRUE))
{
$kitsuUpdateItems[] = $item['data'];
}
if (in_array('mal', $item['updateType']))
if (\in_array('mal', $item['updateType'], TRUE))
{
$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'];
$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)
{
@ -359,7 +404,7 @@ class SyncKitsuWithMal extends BaseCommand {
$diffValues = array_unique($diffValues);
if (count($diffValues) === 1 && $diffValues[0] === 0)
{
return;
return NULL;
}
$update = [
@ -460,7 +505,14 @@ class SyncKitsuWithMal extends BaseCommand {
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();
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();
$requester = new ParallelAPIRequest();