From 88f6a3fc4ff79e881870c0d2f3ee0d581fa36356 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Wed, 1 Mar 2017 22:07:51 -0500 Subject: [PATCH] Rework the rest of the mappings --- src/API/Enum/AnimeWatchingStatus/Route.php | 31 ++++++++++++++ src/API/Enum/AnimeWatchingStatus/Title.php | 31 ++++++++++++++ src/API/Kitsu.php | 38 +--------------- src/API/Mapping/AnimeWatchingStatus.php | 50 +++++++++++++--------- src/API/Title.java | 33 ++++++++++++++ src/Controller/Anime.php | 1 - src/Controller/Manga.php | 8 +--- src/Model/Anime.php | 40 ++++------------- tests/API/KitsuTest.php | 33 ++------------ 9 files changed, 140 insertions(+), 125 deletions(-) create mode 100644 src/API/Enum/AnimeWatchingStatus/Route.php create mode 100644 src/API/Enum/AnimeWatchingStatus/Title.php create mode 100644 src/API/Title.java diff --git a/src/API/Enum/AnimeWatchingStatus/Route.php b/src/API/Enum/AnimeWatchingStatus/Route.php new file mode 100644 index 00000000..acec9f02 --- /dev/null +++ b/src/API/Enum/AnimeWatchingStatus/Route.php @@ -0,0 +1,31 @@ + + * @copyright 2015 - 2017 Timothy J. Warren + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @version 4.0 + * @link https://github.com/timw4mail/HummingBirdAnimeClient + */ + +namespace Aviat\AnimeClient\API\Enum\AnimeWatchingStatus; + +use Aviat\Ion\Enum as Enum; + +/** + * Possible values for current watching status of anime + */ +class Route extends Enum { + const ALL = 'all'; + const WATCHING = 'watching'; + const PLAN_TO_WATCH = 'plan_to_watch'; + const DROPPED = 'dropped'; + const ON_HOLD = 'on_hold'; + const COMPLETED = 'completed'; +} diff --git a/src/API/Enum/AnimeWatchingStatus/Title.php b/src/API/Enum/AnimeWatchingStatus/Title.php new file mode 100644 index 00000000..5aa61277 --- /dev/null +++ b/src/API/Enum/AnimeWatchingStatus/Title.php @@ -0,0 +1,31 @@ + + * @copyright 2015 - 2017 Timothy J. Warren + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @version 4.0 + * @link https://github.com/timw4mail/HummingBirdAnimeClient + */ + +namespace Aviat\AnimeClient\API\Enum\AnimeWatchingStatus; + +use Aviat\Ion\Enum as Enum; + +/** + * Possible values for current watching status of anime + */ +class Title extends Enum { + const ALL = 'All'; + const WATCHING = 'Currently Watching'; + const PLAN_TO_WATCH = 'Plan to Watch'; + const DROPPED = 'Dropped'; + const ON_HOLD = 'On Hold'; + const COMPLETED = 'Completed'; +} diff --git a/src/API/Kitsu.php b/src/API/Kitsu.php index 5ee5f09d..1fec6750 100644 --- a/src/API/Kitsu.php +++ b/src/API/Kitsu.php @@ -16,11 +16,7 @@ namespace Aviat\AnimeClient\API; -use Aviat\AnimeClient\API\Kitsu\Enum\{ - AnimeAiringStatus, - AnimeWatchingStatus, - MangaReadingStatus -}; +use Aviat\AnimeClient\API\Kitsu\Enum\AnimeAiringStatus; use DateTimeImmutable; const AUTH_URL = 'https://kitsu.io/api/oauth/token'; @@ -35,38 +31,6 @@ class Kitsu { const AUTH_USER_ID_KEY = 'kitsu-auth-userid'; const AUTH_TOKEN_CACHE_KEY = 'kitsu-auth-token'; - /** - * Map of Kitsu status to label for select menus - * - * @return array - */ - public static function getStatusToSelectMap() - { - return [ - AnimeWatchingStatus::WATCHING => 'Currently Watching', - AnimeWatchingStatus::PLAN_TO_WATCH => 'Plan to Watch', - AnimeWatchingStatus::COMPLETED => 'Completed', - AnimeWatchingStatus::ON_HOLD => 'On Hold', - AnimeWatchingStatus::DROPPED => 'Dropped' - ]; - } - - /** - * Map of Kitsu Manga status to label for select menus - * - * @return array - */ - public static function getStatusToMangaSelectMap() - { - return [ - MangaReadingStatus::READING => 'Currently Reading', - MangaReadingStatus::PLAN_TO_READ => 'Plan to Read', - MangaReadingStatus::COMPLETED => 'Completed', - MangaReadingStatus::ON_HOLD => 'On Hold', - MangaReadingStatus::DROPPED => 'Dropped' - ]; - } - /** * Determine whether an anime is airing, finished airing, or has not yet aired * diff --git a/src/API/Mapping/AnimeWatchingStatus.php b/src/API/Mapping/AnimeWatchingStatus.php index edc53236..8134ae71 100644 --- a/src/API/Mapping/AnimeWatchingStatus.php +++ b/src/API/Mapping/AnimeWatchingStatus.php @@ -15,7 +15,7 @@ */ namespace Aviat\AnimeClient\API\Mapping; - +use Aviat\AnimeClient\API\Enum\AnimeWatchingStatus\{Route, Title}; use Aviat\AnimeClient\API\{ Kitsu\Enum\AnimeWatchingStatus as Kitsu, MAL\Enum\AnimeWatchingStatus as MAL @@ -39,29 +39,39 @@ class AnimeWatchingStatus extends Enum { MAL::DROPPED => Kitsu::DROPPED ]; + const KITSU_TO_TITLE = [ + 'all' => Title::ALL, + Kitsu::WATCHING => Title::WATCHING, + Kitsu::PLAN_TO_WATCH => Title::PLAN_TO_WATCH, + Kitsu::ON_HOLD => Title::ON_HOLD, + Kitsu::DROPPED => Title::DROPPED, + Kitsu::COMPLETED => Title::COMPLETED + ]; + const ROUTE_TO_KITSU = [ - 'watching' => Kitsu::WATCHING, - 'plan_to_watch' => Kitsu::PLAN_TO_WATCH, - 'on_hold' => Kitsu::ON_HOLD, - 'all' => 'all', - 'dropped' => Kitsu::DROPPED, - 'completed' => Kitsu::COMPLETED + Route::WATCHING => Kitsu::WATCHING, + Route::PLAN_TO_WATCH => Kitsu::PLAN_TO_WATCH, + Route::ON_HOLD => Kitsu::ON_HOLD, + Route::ALL => 'all', + Route::DROPPED => Kitsu::DROPPED, + Route::COMPLETED => Kitsu::COMPLETED ]; const ROUTE_TO_TITLE = [ - 'all' => 'All', - 'watching' => 'Currently Watching', - 'plan_to_watch' => 'Plan to Watch', - 'on_hold' => 'On Hold', - 'dropped' => 'Dropped', - 'completed' => 'Completed' + Route::ALL => Title::ALL, + Route::WATCHING => Title::WATCHING, + Route::PLAN_TO_WATCH => Title::PLAN_TO_WATCH, + Route::ON_HOLD => Title::ON_HOLD, + Route::DROPPED => Title::DROPPED, + Route::COMPLETED => Title::COMPLETED ]; - const KITSU_TO_TITLE = [ - Kitsu::WATCHING => 'Currently Watching', - Kitsu::PLAN_TO_WATCH => 'Plan to Watch', - Kitsu::ON_HOLD => 'On Hold', - Kitsu::DROPPED => 'Dropped', - Kitsu::COMPLETED => 'Completed' + const TITLE_TO_ROUTE = [ + Title::ALL => Route::ALL, + Title::WATCHING => Route::WATCHING, + Title::PLAN_TO_WATCH => Route::PLAN_TO_WATCH, + Title::ON_HOLD => Route::ON_HOLD, + Title::DROPPED => Route::DROPPED, + Title::COMPLETED => Route::COMPLETED ]; -} \ No newline at end of file +} diff --git a/src/API/Title.java b/src/API/Title.java new file mode 100644 index 00000000..1a79d5ee --- /dev/null +++ b/src/API/Title.java @@ -0,0 +1,33 @@ +package API; + + + * @copyright 2015 - 2017 Timothy J. Warren + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @version 4.0 + * @link https://github.com/timw4mail/HummingBirdAnimeClient + */ + +namespace Aviat\AnimeClient\API\Enum\MangaReadingStatus; + +use Aviat\Ion\Enum as Enum; + +/** + * Possible values for current reading status of manga + */ +class Title extends Enum { + const ALL = 'All'; + const READING = 'Currently Reading'; + const PLAN_TO_READ = 'Plan to Read'; + const DROPPED = 'Dropped'; + const ON_HOLD = 'On Hold'; + const COMPLETED = 'Completed'; +} diff --git a/src/Controller/Anime.php b/src/Controller/Anime.php index fae30a6c..a8b2b511 100644 --- a/src/Controller/Anime.php +++ b/src/Controller/Anime.php @@ -17,7 +17,6 @@ namespace Aviat\AnimeClient\Controller; use Aviat\AnimeClient\Controller as BaseController; -use Aviat\AnimeClient\API\Kitsu; use Aviat\AnimeClient\API\Kitsu\{ Enum\AnimeWatchingStatus as KitsuWatchingStatus, Transformer\AnimeListTransformer diff --git a/src/Controller/Manga.php b/src/Controller/Manga.php index 6cc37465..7ccea37b 100644 --- a/src/Controller/Manga.php +++ b/src/Controller/Manga.php @@ -17,11 +17,7 @@ namespace Aviat\AnimeClient\Controller; use Aviat\AnimeClient\Controller; -use Aviat\AnimeClient\API\Kitsu; -use Aviat\AnimeClient\API\Kitsu\{ - Enum\MangaReadingStatus as KitsuReadingStatus, - Transformer\MangaListTransformer -}; +use Aviat\AnimeClient\API\Kitsu\Transformer\MangaListTransformer; use Aviat\AnimeClient\API\Mapping\MangaReadingStatus; use Aviat\AnimeClient\Model\Manga as MangaModel; use Aviat\Ion\Di\ContainerInterface; @@ -219,7 +215,7 @@ class Manga extends Controller { { if ($this->request->getHeader('content-type')[0] === 'application/json') { - $data = JSON::decode((string)$this->request->getBody()); + $data = Json::decode((string)$this->request->getBody()); } else { diff --git a/src/Model/Anime.php b/src/Model/Anime.php index 50a984fa..b012c81d 100644 --- a/src/Model/Anime.php +++ b/src/Model/Anime.php @@ -16,12 +16,8 @@ namespace Aviat\AnimeClient\Model; -use function Amp\some; -use function Amp\wait; - -use Amp\Artax\Client; use Aviat\AnimeClient\API\ParallelAPIRequest; -use Aviat\AnimeClient\API\Kitsu\Enum\AnimeWatchingStatus; +use Aviat\AnimeClient\API\Mapping\AnimeWatchingStatus; use Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\Json; @@ -29,26 +25,6 @@ use Aviat\Ion\Json; * Model for handling requests dealing with the anime list */ class Anime extends API { - - // Display constants - const WATCHING = 'Watching'; - const PLAN_TO_WATCH = 'Plan to Watch'; - const DROPPED = 'Dropped'; - const ON_HOLD = 'On Hold'; - const COMPLETED = 'Completed'; - - /** - * Map of API status constants to display constants - * @var array - */ - protected $const_map = [ - AnimeWatchingStatus::WATCHING => self::WATCHING, - AnimeWatchingStatus::PLAN_TO_WATCH => self::PLAN_TO_WATCH, - AnimeWatchingStatus::ON_HOLD => self::ON_HOLD, - AnimeWatchingStatus::DROPPED => self::DROPPED, - AnimeWatchingStatus::COMPLETED => self::COMPLETED, - ]; - /** * Model for making requests to Kitsu API * @@ -75,7 +51,7 @@ class Anime extends API { * * @param ContainerInterface $container */ - public function __construct(ContainerInterface $container) + public function __construct(ContainerInterface $container) { $config = $container->get('config'); $this->kitsuModel = $container->get('kitsu-model'); @@ -95,8 +71,10 @@ class Anime extends API { $data = $this->kitsuModel->getAnimeList($status); $this->sortByName($data, 'anime'); + $key = AnimeWatchingStatus::KITSU_TO_TITLE[$status]; + $output = []; - $output[$this->const_map[$status]] = $data; + $output[$key] = $data; return $output; } @@ -167,7 +145,7 @@ class Anime extends API { $requester->addRequest($this->malModel->createListItem($malData), 'mal'); } } - + $requester->addRequest($this->kitsuModel->createListItem($data), 'kitsu'); $results = $requester->makeRequests(TRUE); @@ -183,8 +161,8 @@ class Anime extends API { */ public function updateLibraryItem(array $data): array { - $requester = new ParallelAPIRequest(); - + $requester = new ParallelAPIRequest(); + if ($this->useMALAPI) { $requester->addRequest($this->malModel->updateListItem($data), 'mal'); @@ -193,7 +171,7 @@ class Anime extends API { $requester->addRequest($this->kitsuModel->updateListItem($data), 'kitsu'); $results = $requester->makeRequests(TRUE); - + return [ 'body' => Json::decode($results[1]['kitsu']->getBody()), 'statusCode' => $results[1]['kitsu']->getStatus() diff --git a/tests/API/KitsuTest.php b/tests/API/KitsuTest.php index a26c70c4..848fcbc6 100644 --- a/tests/API/KitsuTest.php +++ b/tests/API/KitsuTest.php @@ -17,48 +17,21 @@ namespace Aviat\AnimeClient\Tests\API; use Aviat\AnimeClient\API\Kitsu; -use Aviat\AnimeClient\API\Kitsu\Enum\{ - AnimeAiringStatus, - AnimeWatchingStatus, - MangaReadingStatus -}; -use DateTimeImmutable; +use Aviat\AnimeClient\API\Kitsu\Enum\AnimeAiringStatus; use PHPUnit\Framework\TestCase; class KitsuTest extends TestCase { - public function testGetStatusToSelectMap() - { - $this->assertEquals([ - AnimeWatchingStatus::WATCHING => 'Currently Watching', - AnimeWatchingStatus::PLAN_TO_WATCH => 'Plan to Watch', - AnimeWatchingStatus::COMPLETED => 'Completed', - AnimeWatchingStatus::ON_HOLD => 'On Hold', - AnimeWatchingStatus::DROPPED => 'Dropped' - ], Kitsu::getStatusToSelectMap()); - } - - public function testGetStatusToMangaSelectMap() - { - $this->assertEquals([ - MangaReadingStatus::READING => 'Currently Reading', - MangaReadingStatus::PLAN_TO_READ => 'Plan to Read', - MangaReadingStatus::COMPLETED => 'Completed', - MangaReadingStatus::ON_HOLD => 'On Hold', - MangaReadingStatus::DROPPED => 'Dropped' - ], Kitsu::getStatusToMangaSelectMap()); - } - public function testGetAiringStatus() { $actual = Kitsu::getAiringStatus('next week', 'next year'); $this->assertEquals(AnimeAiringStatus::NOT_YET_AIRED, $actual); } - + public function testParseStreamingLinksEmpty() { $this->assertEquals([], Kitsu::parseStreamingLinks([])); } - + public function testTitleIsUniqueEmpty() { $actual = Kitsu::filterTitles([