From 70832f8b63a3b3d2cc2248a0a661876915352152 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Wed, 31 Jan 2018 15:44:48 -0500 Subject: [PATCH 1/2] Minor refactor of Commands --- console | 11 +- src/Command/BaseCommand.php | 21 ++-- src/Command/CacheClear.php | 5 +- src/Command/CachePrime.php | 7 +- .../{SyncKitsuWithMal.php => SyncLists.php} | 115 +++++++++++++----- 5 files changed, 112 insertions(+), 47 deletions(-) rename src/Command/{SyncKitsuWithMal.php => SyncLists.php} (82%) diff --git a/console b/console index 6869e202..11b12688 100755 --- a/console +++ b/console @@ -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) diff --git a/src/Command/BaseCommand.php b/src/Command/BaseCommand.php index 7a870197..ea06394d 100644 --- a/src/Command/BaseCommand.php +++ b/src/Command/BaseCommand.php @@ -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); diff --git a/src/Command/CacheClear.php b/src/Command/CacheClear.php index 77754da2..81ff5d59 100644 --- a/src/Command/CacheClear.php +++ b/src/Command/CacheClear.php @@ -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'); diff --git a/src/Command/CachePrime.php b/src/Command/CachePrime.php index 660f43f8..ae6cf5d3 100644 --- a/src/Command/CachePrime.php +++ b/src/Command/CachePrime.php @@ -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); diff --git a/src/Command/SyncKitsuWithMal.php b/src/Command/SyncLists.php similarity index 82% rename from src/Command/SyncKitsuWithMal.php rename to src/Command/SyncLists.php index 4fb248e3..c64d4194 100644 --- a/src/Command/SyncKitsuWithMal.php +++ b/src/Command/SyncLists.php @@ -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(); From fa2d79c77eee75021038f5c816dcca6241f79421 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Fri, 2 Feb 2018 09:50:58 -0500 Subject: [PATCH 2/2] Miscellaneous style updates --- src/API/AbstractListItem.php | 21 ------------ src/API/Kitsu/ListItem.php | 10 +++--- src/API/Kitsu/Model.php | 2 +- src/API/MAL/ListItem.php | 1 - src/API/MAL/MALRequestBuilder.php | 3 +- src/API/MAL/MALTrait.php | 2 +- src/API/ParallelAPIRequest.php | 2 +- src/API/XML.php | 16 ++++----- src/Controller.php | 15 ++++----- src/Controller/Anime.php | 50 +++++++++++++++++++++------ src/Controller/AnimeCollection.php | 26 +++++++++++--- src/Controller/Character.php | 28 ++++++++++------ src/Controller/Index.php | 39 ++++++++++++++++++--- src/Controller/Manga.php | 54 ++++++++++++++++++++++++------ src/Controller/MangaCollection.php | 34 ++++++++++++++----- src/Model/Manga.php | 16 +++++---- src/RoutingBase.php | 25 +++++++++----- src/UrlGenerator.php | 16 +++++---- src/Util.php | 13 ++++--- 19 files changed, 249 insertions(+), 124 deletions(-) delete mode 100644 src/API/AbstractListItem.php diff --git a/src/API/AbstractListItem.php b/src/API/AbstractListItem.php deleted file mode 100644 index c4739e8c..00000000 --- a/src/API/AbstractListItem.php +++ /dev/null @@ -1,21 +0,0 @@ - - * @copyright 2015 - 2018 Timothy J. Warren - * @license http://www.opensource.org/licenses/mit-license.html MIT License - * @version 4.0 - * @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient - */ - -namespace Aviat\AnimeClient\API; - -abstract class AbstractListItem implements ListItemInterface { - -} \ No newline at end of file diff --git a/src/API/Kitsu/ListItem.php b/src/API/Kitsu/ListItem.php index 2f01be8a..aaa77c85 100644 --- a/src/API/Kitsu/ListItem.php +++ b/src/API/Kitsu/ListItem.php @@ -21,15 +21,17 @@ use const Aviat\AnimeClient\SESSION_SEGMENT; use function Amp\Promise\wait; use Amp\Artax\Request; -use Aviat\AnimeClient\API\{AbstractListItem, HummingbirdClient}; +use Aviat\AnimeClient\API\{ + HummingbirdClient, + ListItemInterface +}; use Aviat\Ion\Di\ContainerAware; use Aviat\Ion\Json; -use RuntimeException; /** * CRUD operations for Kitsu list items */ -class ListItem extends AbstractListItem { +class ListItem implements ListItemInterface { use ContainerAware; use KitsuTrait; @@ -41,7 +43,7 @@ class ListItem extends AbstractListItem { ->get('session') ->getSegment(SESSION_SEGMENT); - if ( ! is_null($sessionSegment->get('auth_token'))) + if ($sessionSegment->get('auth_token') !== NULL) { $token = $sessionSegment->get('auth_token'); return "bearer {$token}"; diff --git a/src/API/Kitsu/Model.php b/src/API/Kitsu/Model.php index b40b5d8f..3df6f9c4 100644 --- a/src/API/Kitsu/Model.php +++ b/src/API/Kitsu/Model.php @@ -169,7 +169,7 @@ class Model { */ public function getUserIdByUsername(string $username = NULL): string { - if (is_null($username)) + if ($username === NULL) { $username = $this->getUsername(); } diff --git a/src/API/MAL/ListItem.php b/src/API/MAL/ListItem.php index 0c81844a..7df6fad0 100644 --- a/src/API/MAL/ListItem.php +++ b/src/API/MAL/ListItem.php @@ -18,7 +18,6 @@ namespace Aviat\AnimeClient\API\MAL; use Amp\Artax\{FormBody, Request}; use Aviat\AnimeClient\API\{ - AbstractListItem, XML }; use Aviat\Ion\Di\ContainerAware; diff --git a/src/API/MAL/MALRequestBuilder.php b/src/API/MAL/MALRequestBuilder.php index cc8899a5..c7539729 100644 --- a/src/API/MAL/MALRequestBuilder.php +++ b/src/API/MAL/MALRequestBuilder.php @@ -18,8 +18,7 @@ namespace Aviat\AnimeClient\API\MAL; use Aviat\AnimeClient\API\{ APIRequestBuilder, - MAL as M, - XML + MAL as M }; class MALRequestBuilder extends APIRequestBuilder { diff --git a/src/API/MAL/MALTrait.php b/src/API/MAL/MALTrait.php index c10775bd..4922b451 100644 --- a/src/API/MAL/MALTrait.php +++ b/src/API/MAL/MALTrait.php @@ -178,7 +178,7 @@ trait MALTrait { $response = $this->getResponse('POST', ...$args); $validResponseCodes = [200, 201]; - if ( ! in_array((int) $response->getStatus(), $validResponseCodes)) + if ( ! \in_array((int) $response->getStatus(), $validResponseCodes, TRUE)) { if ($logger) { diff --git a/src/API/ParallelAPIRequest.php b/src/API/ParallelAPIRequest.php index a446fc62..f89b11d7 100644 --- a/src/API/ParallelAPIRequest.php +++ b/src/API/ParallelAPIRequest.php @@ -40,7 +40,7 @@ class ParallelAPIRequest { */ public function addRequest($request, $key = NULL): self { - if ( ! is_null($key)) + if ($key !== NULL) { $this->requests[$key] = $request; return $this; diff --git a/src/API/XML.php b/src/API/XML.php index 09e5fd4f..48c35e6d 100644 --- a/src/API/XML.php +++ b/src/API/XML.php @@ -174,14 +174,12 @@ class XML { */ private static function stripXMLWhitespace(string $xml): string { - // Get rid of unimportant text nodes by removing // whitespace characters from between xml tags, // except for the xml declaration tag, Which looks // something like: /* */ - - return preg_replace('/([^\?])>\s+<', $xml); + return preg_replace('/([^?])>\s+<', $xml); } /** @@ -191,7 +189,7 @@ class XML { * @param DOMNodeList $nodeList The current NodeList object * @return void */ - private static function childNodesToArray(array &$root, DOMNodelist $nodeList) + private static function childNodesToArray(array &$root, DOMNodelist $nodeList): void { $length = $nodeList->length; for ($i = 0; $i < $length; $i++) @@ -200,14 +198,14 @@ class XML { $current =& $root[$el->nodeName]; // It's a top level element! - if (is_a($el->childNodes->item(0), 'DomText') OR ( ! $el->hasChildNodes())) + if (( ! $el->hasChildNodes()) || is_a($el->childNodes->item(0), 'DomText')) { $current = $el->textContent; continue; } // An empty value at the current root - if (is_null($current)) + if ($current === NULL) { $current = []; static::childNodesToArray($current, $el->childNodes); @@ -230,7 +228,7 @@ class XML { $current = [$current]; } - array_push($current, []); + $current[] = []; $index = count($current) - 1; static::childNodesToArray($current[$index], $el->childNodes); @@ -245,7 +243,7 @@ class XML { * @param array $data The data for the current node * @return void */ - private static function arrayPropertiesToXmlNodes(DOMDocument &$dom, DOMNode &$parent, array $data) + private static function arrayPropertiesToXmlNodes(DOMDocument $dom, DOMNode $parent, array $data): void { foreach($data as $key => $props) { @@ -260,7 +258,7 @@ class XML { $node = $dom->createElement($key); - if (is_array($props)) + if (\is_array($props)) { static::arrayPropertiesToXmlNodes($dom, $node, $props); } diff --git a/src/Controller.php b/src/Controller.php index aecf7323..f06f2e38 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -22,10 +22,6 @@ use Aviat\Ion\Di\{ ContainerAware, ContainerInterface }; -use Aviat\Ion\Di\Exception\{ - ContainerException, - NotFoundException -}; use Aviat\Ion\Exception\DoubleRenderException; use Aviat\Ion\View\{HtmlView, HttpView, JsonView}; use InvalidArgumentException; @@ -92,9 +88,9 @@ class Controller { /** * Constructor * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException * @param ContainerInterface $container - * @throws ContainerException - * @throws NotFoundException */ public function __construct(ContainerInterface $container) { @@ -135,7 +131,7 @@ class Controller { * * @return void */ - public function redirectToPrevious() + public function redirectToPrevious(): void { $previous = $this->session->getFlash('previous'); $this->redirect($previous, 303); @@ -144,9 +140,9 @@ class Controller { /** * Set the current url in the session as the target of a future redirect * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException * @param string|null $url - * @throws ContainerException - * @throws NotFoundException * @return void */ public function setSessionRedirect(string $url = NULL): void @@ -312,6 +308,7 @@ class Controller { /** * Redirect to the default controller/url from an empty path * + * @throws InvalidArgumentException * @return void */ public function redirectToDefaultRoute(): void diff --git a/src/Controller/Anime.php b/src/Controller/Anime.php index d41c63b8..013c4a25 100644 --- a/src/Controller/Anime.php +++ b/src/Controller/Anime.php @@ -41,6 +41,8 @@ class Anime extends BaseController { * Constructor * * @param ContainerInterface $container + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException */ public function __construct(ContainerInterface $container) { @@ -63,11 +65,14 @@ class Anime extends BaseController { * * @param string|int $type - The section of the list * @param string $view - List or cover view + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \InvalidArgumentException * @return void */ - public function index($type = KitsuWatchingStatus::WATCHING, string $view = NULL) + public function index($type = KitsuWatchingStatus::WATCHING, string $view = NULL): void { - $title = (array_key_exists($type, AnimeWatchingStatus::ROUTE_TO_TITLE)) + $title = array_key_exists($type, AnimeWatchingStatus::ROUTE_TO_TITLE) ? $this->formatTitle( $this->config->get('whose_list') . "'s Anime List", AnimeWatchingStatus::ROUTE_TO_TITLE[$type] @@ -92,9 +97,13 @@ class Anime extends BaseController { /** * Form to add an anime * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \Aura\Router\Exception\RouteNotFound + * @throws \InvalidArgumentException * @return void */ - public function addForm() + public function addForm(): void { $this->setSessionRedirect(); $this->outputHTML('anime/add', [ @@ -110,9 +119,11 @@ class Anime extends BaseController { /** * Add an anime to the list * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException * @return void */ - public function add() + public function add(): void { $data = $this->request->getParsedBody(); if ( ! array_key_exists('id', $data)) @@ -140,9 +151,12 @@ class Anime extends BaseController { * * @param int $id * @param string $status + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \InvalidArgumentException * @return void */ - public function edit($id, $status = "all") + public function edit($id, $status = 'all'): void { $item = $this->model->getLibraryItem($id); $this->setSessionRedirect(); @@ -165,7 +179,7 @@ class Anime extends BaseController { * * @return void */ - public function search() + public function search(): void { $queryParams = $this->request->getQueryParams(); $query = $queryParams['query']; @@ -175,9 +189,11 @@ class Anime extends BaseController { /** * Update an anime item via a form submission * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException * @return void */ - public function formUpdate() + public function formUpdate(): void { $data = $this->request->getParsedBody(); @@ -205,7 +221,7 @@ class Anime extends BaseController { * * @return void */ - public function update() + public function update(): void { if (stripos($this->request->getHeader('content-type')[0], 'application/json') !== FALSE) { @@ -225,9 +241,11 @@ class Anime extends BaseController { /** * Remove an anime from the list * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException * @return void */ - public function delete() + public function delete(): void { $body = $this->request->getParsedBody(); $response = $this->model->deleteLibraryItem($body['id'], $body['mal_id']); @@ -249,9 +267,12 @@ class Anime extends BaseController { * View details of an anime * * @param string $animeId + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \InvalidArgumentException * @return void */ - public function details(string $animeId) + public function details(string $animeId): void { $show_data = $this->model->getAnime($animeId); $characters = []; @@ -287,5 +308,14 @@ class Anime extends BaseController { ]); } + /** + * Find anime matching the selected genre + * + * @param string $genre + */ + public function genre(string $genre): void + { + // @TODO: implement + } } // End of AnimeController.php \ No newline at end of file diff --git a/src/Controller/AnimeCollection.php b/src/Controller/AnimeCollection.php index e83ce631..520408f0 100644 --- a/src/Controller/AnimeCollection.php +++ b/src/Controller/AnimeCollection.php @@ -44,6 +44,8 @@ class AnimeCollection extends BaseController { * Constructor * * @param ContainerInterface $container + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException */ public function __construct(ContainerInterface $container) { @@ -63,6 +65,7 @@ class AnimeCollection extends BaseController { /** * Search for anime * + * @throws \Aviat\Ion\Exception\DoubleRenderException * @return void */ public function search() @@ -76,6 +79,9 @@ class AnimeCollection extends BaseController { * Show the anime collection page * * @param string $view + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \InvalidArgumentException * @return void */ public function index($view) @@ -98,13 +104,17 @@ class AnimeCollection extends BaseController { * Show the anime collection add/edit form * * @param integer|null $id + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \Aura\Router\Exception\RouteNotFound + * @throws \InvalidArgumentException * @return void */ public function form($id = NULL) { $this->setSessionRedirect(); - $action = (is_null($id)) ? "Add" : "Edit"; + $action = $id === NULL ? 'Add' : 'Edit'; $urlAction = strtolower($action); $this->outputHTML('collection/' . $urlAction, [ @@ -115,13 +125,16 @@ class AnimeCollection extends BaseController { $action ), 'media_items' => $this->animeCollectionModel->getMediaTypeList(), - 'item' => ($action === "Edit") ? $this->animeCollectionModel->get($id) : [] + 'item' => ($action === 'Edit') ? $this->animeCollectionModel->get($id) : [] ]); } /** * Update a collection item * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \InvalidArgumentException * @return void */ public function edit() @@ -143,6 +156,9 @@ class AnimeCollection extends BaseController { /** * Add a collection item * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \InvalidArgumentException * @return void */ public function add() @@ -171,13 +187,13 @@ class AnimeCollection extends BaseController { $data = $this->request->getParsedBody(); if ( ! array_key_exists('hummingbird_id', $data)) { - $this->redirect("/anime-collection/view", 303); + $this->redirect('/anime-collection/view', 303); } $this->animeCollectionModel->delete($data); - $this->setFlashMessage("Successfully removed anime from collection.", 'success'); + $this->setFlashMessage('Successfully removed anime from collection.', 'success'); - $this->redirect("/anime-collection/view", 303); + $this->redirect('/anime-collection/view', 303); } } // End of CollectionController.php \ No newline at end of file diff --git a/src/Controller/Character.php b/src/Controller/Character.php index 4381cd5e..2619833a 100644 --- a/src/Controller/Character.php +++ b/src/Controller/Character.php @@ -27,7 +27,16 @@ class Character extends BaseController { use ArrayWrapper; - public function index(string $slug) + /** + * Show information about a character + * + * @param string $slug + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \InvalidArgumentException + * @return void + */ + public function index(string $slug): void { $model = $this->container->get('kitsu-model'); @@ -88,8 +97,9 @@ class Character extends BaseController { $person = current($role['relationships']['person']['people'])['attributes']; + $hasName = array_key_exists($person['name'], $people); - if ( ! array_key_exists($person['name'], $people)) + if ( ! $hasName) { $people[$person['name']] = $i; $role['relationships']['media']['anime'] = [current($role['relationships']['media']['anime'])]; @@ -99,15 +109,13 @@ class Character extends BaseController { continue; } - else if(array_key_exists($person['name'], $people)) + + if (array_key_exists('anime', $role['relationships']['media'])) { - if (array_key_exists('anime', $role['relationships']['media'])) - { - $key = $people[$person['name']]; - $output[$key]['relationships']['media']['anime'][] = current($role['relationships']['media']['anime']); - } - continue; + $key = $people[$person['name']]; + $output[$key]['relationships']['media']['anime'][] = current($role['relationships']['media']['anime']); } + continue; } return $output; @@ -122,7 +130,7 @@ class Character extends BaseController { if ( array_key_exists('attributes', $role) && array_key_exists('role', $role['attributes']) && - ( ! is_null($role['attributes']['role'])) + $role['attributes']['role'] !== NULL ) { $count++; } diff --git a/src/Controller/Index.php b/src/Controller/Index.php index 358c6d60..985dc92f 100644 --- a/src/Controller/Index.php +++ b/src/Controller/Index.php @@ -30,6 +30,9 @@ class Index extends BaseController { /** * Purges the API cache * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \InvalidArgumentException * @return void */ public function clearCache() @@ -37,13 +40,16 @@ class Index extends BaseController { $this->cache->clear(); $this->outputHTML('blank', [ 'title' => 'Cache cleared' - ], NULL, 200); + ]); } /** * Show the login form * * @param string $status + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \InvalidArgumentException * @return void */ public function login(string $status = '') @@ -69,6 +75,10 @@ class Index extends BaseController { /** * Attempt login authentication * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \Aura\Router\Exception\RouteNotFound + * @throws \InvalidArgumentException * @return void */ public function loginAction() @@ -88,6 +98,9 @@ class Index extends BaseController { /** * Deauthorize the current user * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \InvalidArgumentException * @return void */ public function logout() @@ -101,6 +114,9 @@ class Index extends BaseController { /** * Show the user profile page * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \InvalidArgumentException * @return void */ public function me() @@ -111,8 +127,8 @@ class Index extends BaseController { $orgData = JsonAPI::organizeData($data)[0]; $rels = $orgData['relationships'] ?? []; $favorites = array_key_exists('favorites', $rels) ? $rels['favorites'] : []; - - + + $this->outputHTML('me', [ 'title' => 'About ' . $this->config->get('whose_list'), 'data' => $orgData, @@ -125,9 +141,17 @@ class Index extends BaseController { /** * Get image covers from kitsu * + * @param string $type The category of image + * @param string $file The filename to look for + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \InvalidArgumentException + * @throws \TypeError + * @throws \Error + * @throws \Throwable * @return void */ - public function images($type, $file) + public function images(string $type, string $file): void { $kitsuUrl = 'https://media.kitsu.io/'; list($id, $ext) = explode('.', basename($file)); @@ -164,9 +188,14 @@ class Index extends BaseController { echo $data; } + /** + * Reorganize favorites data to be more useful + * + * @param array $rawfavorites + * @return array + */ private function organizeFavorites(array $rawfavorites): array { - // return $rawfavorites; $output = []; unset($rawfavorites['data']); diff --git a/src/Controller/Manga.php b/src/Controller/Manga.php index 78d30fe6..c6ed6de1 100644 --- a/src/Controller/Manga.php +++ b/src/Controller/Manga.php @@ -40,6 +40,8 @@ class Manga extends Controller { * Constructor * * @param ContainerInterface $container + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException */ public function __construct(ContainerInterface $container) { @@ -59,9 +61,12 @@ class Manga extends Controller { * * @param string $status * @param string $view + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \InvalidArgumentException * @return void */ - public function index($status = "all", $view = "") + public function index($status = 'all', $view = ''): void { $statusTitle = MangaReadingStatus::ROUTE_TO_TITLE[$status]; @@ -88,9 +93,13 @@ class Manga extends Controller { /** * Form to add an manga * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \Aura\Router\Exception\RouteNotFound + * @throws \InvalidArgumentException * @return void */ - public function addForm() + public function addForm(): void { $statuses = MangaReadingStatus::KITSU_TO_TITLE; @@ -108,14 +117,16 @@ class Manga extends Controller { /** * Add an manga to the list * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException * @return void */ - public function add() + public function add(): void { $data = $this->request->getParsedBody(); if ( ! array_key_exists('id', $data)) { - $this->redirect("manga/add", 303); + $this->redirect('manga/add', 303); } $result = $this->model->createLibraryItem($data); @@ -138,9 +149,13 @@ class Manga extends Controller { * * @param string $id * @param string $status + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \Aura\Router\Exception\RouteNotFound + * @throws \InvalidArgumentException * @return void */ - public function edit($id, $status = "All") + public function edit($id, $status = 'All'): void { $this->setSessionRedirect(); $item = $this->model->getLibraryItem($id); @@ -164,7 +179,7 @@ class Manga extends Controller { * * @return void */ - public function search() + public function search(): void { $query_data = $this->request->getQueryParams(); $this->outputJSON($this->model->search($query_data['query'])); @@ -173,9 +188,11 @@ class Manga extends Controller { /** * Update an manga item via a form submission * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException * @return void */ - public function formUpdate() + public function formUpdate(): void { $data = $this->request->getParsedBody(); @@ -202,9 +219,11 @@ class Manga extends Controller { /** * Update a manga item * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException * @return void */ - public function update() + public function update(): void { if (stripos($this->request->getHeader('content-type')[0], 'application/json') !== FALSE) { @@ -224,9 +243,11 @@ class Manga extends Controller { /** * Remove an manga from the list * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException * @return void */ - public function delete() + public function delete(): void { $body = $this->request->getParsedBody(); $id = $body['id']; @@ -250,9 +271,12 @@ class Manga extends Controller { * View details of an manga * * @param string $manga_id + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \InvalidArgumentException * @return void */ - public function details($manga_id) + public function details($manga_id): void { $data = $this->model->getManga($manga_id); $characters = []; @@ -286,5 +310,15 @@ class Manga extends Controller { 'data' => $data, ]); } + + /** + * Find manga matching the selected genre + * + * @param string $genre + */ + public function genre(string $genre): void + { + // @TODO: implement + } } // End of MangaController.php \ No newline at end of file diff --git a/src/Controller/MangaCollection.php b/src/Controller/MangaCollection.php index b03f1970..cbceefcc 100644 --- a/src/Controller/MangaCollection.php +++ b/src/Controller/MangaCollection.php @@ -21,7 +21,6 @@ use Aviat\AnimeClient\Model\{ Manga as MangaModel, MangaCollection as MangaCollectionModel }; -use Aviat\AnimeClient\UrlGenerator; use Aviat\Ion\Di\ContainerInterface; /** @@ -45,6 +44,9 @@ class MangaCollection extends BaseController { * Constructor * * @param ContainerInterface $container + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \InvalidArgumentException */ public function __construct(ContainerInterface $container) { @@ -64,9 +66,10 @@ class MangaCollection extends BaseController { /** * Search for manga * + * @throws \Aviat\Ion\Exception\DoubleRenderException * @return void */ - public function search() + public function search(): void { $queryParams = $this->request->getQueryParams(); $query = $queryParams['query']; @@ -77,9 +80,12 @@ class MangaCollection extends BaseController { * Show the manga collection page * * @param string $view + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \InvalidArgumentException * @return void */ - public function index($view) + public function index($view): void { $viewMap = [ '' => 'cover', @@ -99,13 +105,17 @@ class MangaCollection extends BaseController { * Show the manga collection add/edit form * * @param integer|null $id + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \Aura\Router\Exception\RouteNotFound + * @throws \InvalidArgumentException * @return void */ - public function form($id = NULL) + public function form($id = NULL): void { $this->setSessionRedirect(); - $action = (is_null($id)) ? "Add" : "Edit"; + $action = $id === NULL ? 'Add' : 'Edit'; $urlAction = strtolower($action); $this->outputHTML('collection/' . $urlAction, [ @@ -116,13 +126,16 @@ class MangaCollection extends BaseController { $action ), 'media_items' => $this->mangaCollectionModel->getMediaTypeList(), - 'item' => ($action === "Edit") ? $this->mangaCollectionModel->get($id) : [] + 'item' => ($action === 'Edit') ? $this->mangaCollectionModel->get($id) : [] ]); } /** * Update a collection item * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \InvalidArgumentException * @return void */ public function edit() @@ -144,6 +157,9 @@ class MangaCollection extends BaseController { /** * Add a collection item * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \InvalidArgumentException * @return void */ public function add() @@ -172,13 +188,13 @@ class MangaCollection extends BaseController { $data = $this->request->getParsedBody(); if ( ! array_key_exists('hummingbird_id', $data)) { - $this->redirect("/manga-collection/view", 303); + $this->redirect('/manga-collection/view', 303); } $this->mangaCollectionModel->delete($data); - $this->setFlashMessage("Successfully removed manga from collection.", 'success'); + $this->setFlashMessage('Successfully removed manga from collection.', 'success'); - $this->redirect("/manga-collection/view", 303); + $this->redirect('/manga-collection/view', 303); } } // End of CollectionController.php \ No newline at end of file diff --git a/src/Model/Manga.php b/src/Model/Manga.php index 0ca1a732..b3ddcf41 100644 --- a/src/Model/Manga.php +++ b/src/Model/Manga.php @@ -45,6 +45,8 @@ class Manga extends API * Constructor * * @param ContainerInterface $container + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException */ public function __construct(ContainerInterface $container) { @@ -61,7 +63,7 @@ class Manga extends API * @param string $status * @return array */ - public function getList($status) + public function getList($status): array { if ($status === 'All') { @@ -79,7 +81,7 @@ class Manga extends API * @param string $manga_id * @return array */ - public function getManga($manga_id) + public function getManga($manga_id): array { return $this->kitsuModel->getManga($manga_id); } @@ -122,7 +124,7 @@ class Manga extends API $malData = $data; $malId = $this->kitsuModel->getMalIdForManga($malData['id']); - if ( ! is_null($malId)) + if ($malId !== NULL) { $malData['id'] = $malId; $requester->addRequest($this->malModel->createListItem($malData, 'manga'), 'mal'); @@ -155,7 +157,7 @@ class Manga extends API $results = $requester->makeRequests(); $body = Json::decode($results['kitsu']); - $statusCode = (array_key_exists('error', $body)) ? 400: 200; + $statusCode = array_key_exists('error', $body) ? 400: 200; return [ 'body' => Json::decode($results['kitsu']), @@ -174,7 +176,7 @@ class Manga extends API { $requester = new ParallelAPIRequest(); - if ($this->useMALAPI && ! is_null($malId)) + if ($this->useMALAPI && $malId !== NULL) { $requester->addRequest($this->malModel->deleteListItem($malId, 'manga'), 'MAL'); } @@ -192,7 +194,7 @@ class Manga extends API * @param string $name * @return array */ - public function search($name) + public function search($name): array { return $this->kitsuModel->search('manga', $name); } @@ -203,7 +205,7 @@ class Manga extends API * @param array $data * @return array */ - private function mapByStatus($data) + private function mapByStatus(array $data): array { $output = [ Title::READING => [], diff --git a/src/RoutingBase.php b/src/RoutingBase.php index d4269cc3..315c7ac9 100644 --- a/src/RoutingBase.php +++ b/src/RoutingBase.php @@ -54,6 +54,9 @@ class RoutingBase { * Constructor * * @param ContainerInterface $container + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException + * @throws \Aviat\Ion\Exception\ConfigException */ public function __construct(ContainerInterface $container) { @@ -64,7 +67,7 @@ class RoutingBase { } /** - * Retreive the appropriate value for the routing key + * Retrieve the appropriate value for the routing key * * @param string $key * @return mixed @@ -79,10 +82,11 @@ class RoutingBase { /** * Get the current url path - * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException * @return string */ - public function path() + public function path(): string { $request = $this->container->get('request'); $path = $request->getUri()->getPath(); @@ -97,10 +101,11 @@ class RoutingBase { /** * Get the url segments - * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException * @return array */ - public function segments() + public function segments(): array { $path = $this->path(); return explode('/', $path); @@ -110,20 +115,24 @@ class RoutingBase { * Get a segment of the current url * * @param int $num + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException * @return string|null */ - public function getSegment($num) + public function getSegment($num): ?string { $segments = $this->segments(); - return (array_key_exists($num, $segments)) ? $segments[$num] : NULL; + return $segments[$num] ?? NULL; } /** * Retrieve the last url segment * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException * @return string */ - public function lastSegment() + public function lastSegment(): string { $segments = $this->segments(); return end($segments); diff --git a/src/UrlGenerator.php b/src/UrlGenerator.php index 39862ca6..df30409e 100644 --- a/src/UrlGenerator.php +++ b/src/UrlGenerator.php @@ -34,6 +34,8 @@ class UrlGenerator extends RoutingBase { * Constructor * * @param ContainerInterface $container + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException */ public function __construct(ContainerInterface $container) { @@ -44,17 +46,17 @@ class UrlGenerator extends RoutingBase { /** * Get the base url for css/js/images * - * @param string ...$args url segments to apend to the base asset url + * @param string[] ...$args * @return string */ - public function assetUrl(...$args): string + public function assetUrl(string ...$args): string { - $baseUrl = rtrim($this->url(""), '/'); - $baseUrl = "{$baseUrl}" . $this->__get("asset_path"); + $baseUrl = rtrim($this->url(''), '/') + . $this->__get('asset_path'); array_unshift($args, $baseUrl); - return implode("/", $args); + return implode('/', $args); } /** @@ -82,7 +84,7 @@ class UrlGenerator extends RoutingBase { $segments[$i + 1] = ""; } - $path_segments[$i] = preg_replace('`{.*?}`i', $segments[$i + 1], $path_segments[$i]); + $path_segments[$i] = preg_replace('`{.*?}`', $segments[$i + 1], $path_segments[$i]); } $path = implode('/', $path_segments); @@ -101,7 +103,7 @@ class UrlGenerator extends RoutingBase { $type = trim($type); $defaultPath = $this->__get("default_{$type}_list_path"); - if ( ! is_null($defaultPath)) + if ($defaultPath !== NULL) { return $this->url("{$type}/{$defaultPath}"); } diff --git a/src/Util.php b/src/Util.php index dd87758f..5c3fab63 100644 --- a/src/Util.php +++ b/src/Util.php @@ -18,7 +18,6 @@ namespace Aviat\AnimeClient; use Aviat\Ion\ConfigInterface; use Aviat\Ion\Di\{ContainerAware, ContainerInterface}; -use DomainException; /** * Utility method class @@ -53,6 +52,8 @@ class Util { * Set up the Util class * * @param ContainerInterface $container + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException */ public function __construct(ContainerInterface $container) { @@ -87,12 +88,14 @@ class Util { /** * Determine whether to show the sub-menu * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException * @return bool */ - public function isViewPage() + public function isViewPage(): bool { $url = $this->container->get('request')->getUri(); - $pageSegments = explode("/", (string) $url); + $pageSegments = explode('/', (string) $url); $intersect = array_intersect($pageSegments, self::$formPages); @@ -103,9 +106,11 @@ class Util { * Determine whether the page is a page with a form, and * not suitable for redirection * + * @throws \Aviat\Ion\Di\ContainerException + * @throws \Aviat\Ion\Di\NotFoundException * @return boolean */ - public function isFormPage() + public function isFormPage(): bool { return ! $this->isViewPage(); }