From 87ecc0dce4f68fc8508c59175c22f9f45af2bd14 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Wed, 15 Jan 2020 15:22:38 -0500 Subject: [PATCH] More error checking --- src/API/Anilist/Model.php | 7 ++++++- src/API/Kitsu/Model.php | 7 ++++++- src/Command/SyncLists.php | 13 ++++++++++--- src/Types/AbstractType.php | 4 +--- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/API/Anilist/Model.php b/src/API/Anilist/Model.php index dc6454ab..3f02b4eb 100644 --- a/src/API/Anilist/Model.php +++ b/src/API/Anilist/Model.php @@ -125,7 +125,10 @@ final class Model */ public function createListItem(array $data, string $type = 'anime'): ?Request { - $createData = []; + if ($data['mal_id'] === NULL) + { + return NULL; + } $mediaId = $this->getMediaIdFromMalId($data['mal_id'], mb_strtoupper($type)); @@ -134,6 +137,8 @@ final class Model return NULL; } + $createData = []; + if ($type === 'ANIME') { $createData = [ diff --git a/src/API/Kitsu/Model.php b/src/API/Kitsu/Model.php index 4c3068ee..c68b4d44 100644 --- a/src/API/Kitsu/Model.php +++ b/src/API/Kitsu/Model.php @@ -869,9 +869,14 @@ final class Model { * @return Request * @throws InvalidArgumentException */ - public function createListItem(array $data): Request + public function createListItem(array $data): ?Request { $data['user_id'] = $this->getUserIdByUsername($this->getUsername()); + if ($data['id'] === NULL) + { + return NULL; + } + return $this->listItem->create($data); } diff --git a/src/Command/SyncLists.php b/src/Command/SyncLists.php index 39f36b08..c7f5e7fc 100644 --- a/src/Command/SyncLists.php +++ b/src/Command/SyncLists.php @@ -26,6 +26,8 @@ use Aviat\AnimeClient\API\Anilist\Transformer\{ AnimeListTransformer as AALT, MangaListTransformer as AMLT }; +use Aviat\AnimeClient\API\Anilist\Model as AnilistModel; +use Aviat\AnimeClient\API\Kitsu\Model as KitsuModel; use Aviat\AnimeClient\API\Mapping\{AnimeWatchingStatus, MangaReadingStatus}; use Aviat\AnimeClient\Types\FormItem; use Aviat\Ion\Di\Exception\ContainerException; @@ -40,13 +42,13 @@ final class SyncLists extends BaseCommand { /** * Model for making requests to Anilist API - * @var \Aviat\AnimeClient\API\Anilist\Model + * @var AnilistModel */ protected $anilistModel; /** * Model for making requests to Kitsu API - * @var \Aviat\AnimeClient\API\Kitsu\Model + * @var KitsuModel */ protected $kitsuModel; @@ -602,6 +604,12 @@ final class SyncLists extends BaseCommand { } else if ($action === 'create') { + $maybeRequest = $this->kitsuModel->createListItem($item); + if ($maybeRequest === NULL) + { + $this->echoBox("Skipped creating Kitsu {$type} due to missing id ¯\_(ツ)_/¯"); + continue; + } $requester->addRequest($this->kitsuModel->createListItem($item)); } } @@ -682,7 +690,6 @@ final class SyncLists extends BaseCommand { $responseData = Json::decode($response); - // $id = $itemsToUpdate[$key]['id']; if ( ! array_key_exists('errors', $responseData)) { $verb = ($action === 'update') ? 'updated' : 'created'; diff --git a/src/Types/AbstractType.php b/src/Types/AbstractType.php index a38177f7..53b16062 100644 --- a/src/Types/AbstractType.php +++ b/src/Types/AbstractType.php @@ -63,9 +63,7 @@ abstract class AbstractType implements ArrayAccess, Countable { */ public function __isset($name): bool { - // This returns the expected results because unset - // properties no longer exist on the class - return property_exists($this, $name); + return property_exists($this, $name) && isset($this->$name); } /**