Convert Type constructors to static methods
All checks were successful
timw4mail/HummingBirdAnimeClient/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2020-04-22 07:53:52 -04:00
parent 12be7b8a1e
commit f2991cd416
26 changed files with 74 additions and 54 deletions

View File

@ -61,7 +61,7 @@ $overrideConfig = file_exists($overrideFile)
$configArray = array_replace_recursive($baseConfig, $config, $overrideConfig);
$checkedConfig = (new ConfigType($configArray))->toArray();
$checkedConfig = ConfigType::check($configArray);
$container = $di($checkedConfig);
// Unset 'constants'

View File

@ -37,7 +37,7 @@ final class ListItem extends AbstractListItem {
*/
public function create(array $data): Request
{
$checkedData = (new Types\MediaListEntry($data))->toArray();
$checkedData = Types\MediaListEntry::check($data);
return $this->mutateRequest('CreateMediaListEntry', $checkedData);
}
@ -49,7 +49,7 @@ final class ListItem extends AbstractListItem {
*/
public function createFull(array $data): Request
{
$checkedData = (new Types\MediaListEntry($data))->toArray();
$checkedData = Types\MediaListEntry::check($data);
return $this->mutateRequest('CreateFullMediaListEntry', $checkedData);
}
@ -85,10 +85,10 @@ final class ListItem extends AbstractListItem {
*/
public function increment(string $id, FormItemData $data): Request
{
$checkedData = (new Types\MediaListEntry([
$checkedData = Types\MediaListEntry::check([
'id' => $id,
'progress' => $data->progress,
]))->toArray();
]);
return $this->mutateRequest('IncrementMediaListEntry', $checkedData);
}
@ -110,7 +110,7 @@ final class ListItem extends AbstractListItem {
? AnilistStatus::REPEATING
: AnimeWatchingStatus::KITSU_TO_ANILIST[$data->status];
$updateData = (new Types\MediaListEntry([
$updateData = Types\MediaListEntry::check([
'id' => (int)$id,
'status' => $status,
'score' => $rating * 5,
@ -118,7 +118,7 @@ final class ListItem extends AbstractListItem {
'repeat' => (int)$data['reconsumeCount'],
'private' => $private,
'notes' => $notes,
]))->toArray();
]);
return $this->mutateRequest('UpdateMediaListEntry', $updateData);
}

View File

@ -16,4 +16,6 @@
namespace Aviat\AnimeClient\API\Anilist;
class MissingIdException extends \InvalidArgumentException {}
use InvalidArgumentException;
class MissingIdException extends InvalidArgumentException {}

View File

@ -42,7 +42,7 @@ class AnimeListTransformer extends AbstractTransformer {
{
$reconsuming = $item['status'] === AnilistStatus::REPEATING;
return new FormItem([
return FormItem::from([
'id' => $item['id'],
'mal_id' => $item['media']['idMal'],
'data' => [

View File

@ -29,7 +29,7 @@ class MangaListTransformer extends AbstractTransformer {
public function transform($item)
{
return new MangaListItem([]);
return MangaListItem::from([]);
}
/**
@ -40,7 +40,7 @@ class MangaListTransformer extends AbstractTransformer {
*/
public function untransform(array $item): FormItem
{
return new FormItem([
return FormItem::from([
'id' => $item['id'],
'mal_id' => $item['media']['idMal'],
'data' => [

View File

@ -184,7 +184,9 @@ final class Model {
public function getAnimeHistory(): array
{
$raw = $this->getRawHistoryList('anime');
$organized = JsonAPI::organizeData($raw);
$organized = (array)JsonAPI::organizeData($raw);
$organized = array_filter($organized, fn ($item) => array_key_exists('relationships', $item));
$transformer = new AnimeHistoryTransformer();
$transformer->setContainer($this->getContainer());
@ -202,8 +204,11 @@ final class Model {
public function getMangaHistory(): array
{
$raw = $this->getRawHistoryList('manga');
$organized = (array)JsonAPI::organizeData($raw);
return JsonAPI::organizeData($raw);
$organized = array_filter($organized, fn ($item) => array_key_exists('relationships', $item));
return $organized;
}
/**
@ -413,7 +418,7 @@ final class Model {
if (empty($baseData))
{
return (new Anime([]))->toArray();
return Anime::from([]);
}
return $this->animeTransformer->transform($baseData);
@ -669,7 +674,7 @@ final class Model {
if (empty($baseData))
{
return new MangaPage([]);
return MangaPage::from([]);
}
return $this->mangaTransformer->transform($baseData);

View File

@ -47,11 +47,13 @@ class AnimeHistoryTransformer {
continue;
}
if ($entry['attributes']['kind'] === 'progressed')
$kind = $entry['attributes']['kind'];
if ($kind === 'progressed')
{
$output[] = $this->transformProgress($entry);
}
else if ($entry['attributes']['kind'] === 'updated')
else if ($kind === 'updated')
{
$output[] = $this->transformUpdated($entry);
}
@ -128,7 +130,6 @@ class AnimeHistoryTransformer {
{
$output[] = $entry;
}
}
return $output;

View File

@ -80,7 +80,7 @@ final class AnimeListTransformer extends AbstractTransformer {
$titles = Kitsu::filterTitles($anime);
$title = array_shift($titles);
return new AnimeListItem([
return AnimeListItem::from([
'id' => $item['id'],
'mal_id' => $MALid,
'episodes' => [
@ -127,7 +127,7 @@ final class AnimeListTransformer extends AbstractTransformer {
$privacy = (array_key_exists('private', $item) && $item['private']);
$rewatching = (array_key_exists('rewatching', $item) && $item['rewatching']);
$untransformed = new FormItem([
$untransformed = FormItem::from([
'id' => $item['id'],
'anilist_item_id' => $item['anilist_item_id'] ?? NULL,
'mal_id' => $item['mal_id'] ?? NULL,

View File

@ -105,7 +105,7 @@ final class AnimeTransformer extends AbstractTransformer {
ksort($characters);
ksort($staff);
return new AnimePage([
return AnimePage::from([
'age_rating' => $item['ageRating'],
'age_rating_guide' => $item['ageRatingGuide'],
'characters' => $characters,

View File

@ -66,7 +66,7 @@ final class CharacterTransformer extends AbstractTransformer {
}
}
return new Character([
return Character::from([
'castings' => $castings,
'description' => $attributes['description'],
'id' => $data[0]['id'],

View File

@ -82,7 +82,7 @@ final class MangaListTransformer extends AbstractTransformer {
$titles = Kitsu::filterTitles($manga);
$title = array_shift($titles);
return new MangaListItem([
return MangaListItem::from([
'id' => $item['id'],
'mal_id' => $MALid,
'chapters' => [
@ -93,7 +93,7 @@ final class MangaListTransformer extends AbstractTransformer {
'read' => '-', //$item['attributes']['volumes_read'],
'total' => $totalVolumes
],
'manga' => new MangaListItemDetail([
'manga' => MangaListItemDetail::from([
'genres' => $genres,
'id' => $mangaId,
'image' => $manga['posterImage']['small'],
@ -121,10 +121,10 @@ final class MangaListTransformer extends AbstractTransformer {
{
$rereading = array_key_exists('rereading', $item) && (bool)$item['rereading'];
$map = new FormItem([
$map = FormItem::from([
'id' => $item['id'],
'mal_id' => $item['mal_id'],
'data' => new FormItemData([
'data' => FormItemData::from([
'status' => $item['status'],
'reconsuming' => $rereading,
'reconsumeCount' => (int)$item['reread_count'],

View File

@ -113,7 +113,7 @@ final class MangaTransformer extends AbstractTransformer {
ksort($characters);
ksort($staff);
return new MangaPage([
return MangaPage::from([
'characters' => $characters,
'chapter_count' => $this->count($item['chapterCount']),
'cover_image' => $item['posterImage']['small'],

View File

@ -36,7 +36,7 @@ final class PersonTransformer extends AbstractTransformer {
$orgData = $this->organizeData($included);
return new Person([
return Person::from([
'id' => $data['id'],
'name' => $data['attributes']['name'],
'characters' => $orgData['characters'],

View File

@ -55,7 +55,7 @@ final class UserTransformer extends AbstractTransformer {
];
}
return new User([
return User::from([
'about' => $attributes['about'],
'avatar' => getLocalImg($attributes['avatar']['original'], FALSE),
'favorites' => $this->organizeFavorites($favorites),

View File

@ -34,6 +34,8 @@ use Aviat\Ion\Di\Exception\ContainerException;
use Aviat\Ion\Di\Exception\NotFoundException;
use Aviat\Ion\Json;
use DateTime;
use Throwable;
use function in_array;
/**
* Syncs list data between Anilist and Kitsu
@ -59,7 +61,7 @@ final class SyncLists extends BaseCommand {
* @param array $options
* @throws ContainerException
* @throws NotFoundException
* @throws \Throwable
* @throws Throwable
*/
public function execute(array $args, array $options = []): void
{
@ -88,7 +90,7 @@ final class SyncLists extends BaseCommand {
* Attempt to synchronize external APIs
*
* @param string $type
* @throws \Throwable
* @throws Throwable
*/
protected function sync(string $type): void
{
@ -344,12 +346,12 @@ final class SyncLists extends BaseCommand {
continue;
}
if (\in_array('kitsu', $item['updateType'], TRUE))
if (in_array('kitsu', $item['updateType'], TRUE))
{
$kitsuUpdateItems[] = $item['data'];
}
if (\in_array('anilist', $item['updateType'], TRUE))
if (in_array('anilist', $item['updateType'], TRUE))
{
$anilistUpdateItems[] = $item['data'];
}
@ -589,7 +591,7 @@ final class SyncLists extends BaseCommand {
* @param array $itemsToUpdate
* @param string $action
* @param string $type
* @throws \Throwable
* @throws Throwable
*/
protected function updateKitsuListItems(array $itemsToUpdate, string $action = 'update', string $type = 'anime'): void
{
@ -599,7 +601,7 @@ final class SyncLists extends BaseCommand {
if ($action === 'update')
{
$requester->addRequest(
$this->kitsuModel->updateListItem(new FormItem($item))
$this->kitsuModel->updateListItem(FormItem::from($item))
);
}
else if ($action === 'create')
@ -653,7 +655,7 @@ final class SyncLists extends BaseCommand {
* @param array $itemsToUpdate
* @param string $action
* @param string $type
* @throws \Throwable
* @throws Throwable
*/
protected function updateAnilistListItems(array $itemsToUpdate, string $action = 'update', string $type = 'anime'): void
{
@ -664,7 +666,7 @@ final class SyncLists extends BaseCommand {
if ($action === 'update')
{
$requester->addRequest(
$this->anilistModel->updateListItem(new FormItem($item), $type)
$this->anilistModel->updateListItem(FormItem::from($item), $type)
);
}
else if ($action === 'create')

View File

@ -179,7 +179,6 @@ final class Anime extends BaseController {
*
* @param string $id
* @param string $status
* @throws RouteNotFound
*/
public function edit(string $id, $status = 'all'): void
{
@ -231,7 +230,7 @@ final class Anime extends BaseController {
// large form-based updates
$transformer = new AnimeListTransformer();
$postData = $transformer->untransform($data);
$fullResult = $this->model->updateLibraryItem(new FormItem($postData));
$fullResult = $this->model->updateLibraryItem(FormItem::from($postData));
if ($fullResult['statusCode'] === 200)
{
@ -271,7 +270,7 @@ final class Anime extends BaseController {
die();
}
$response = $this->model->incrementLibraryItem(new FormItem($data));
$response = $this->model->incrementLibraryItem(FormItem::from($data));
$this->cache->clear();
$this->outputJSON($response['body'], $response['statusCode']);

View File

@ -27,7 +27,7 @@ use Aviat\Ion\Di\Exception\NotFoundException;
/**
* Controller for character description pages
*/
class Character extends BaseController {
final class Character extends BaseController {
/**
* @var Model

View File

@ -231,7 +231,7 @@ final class Manga extends Controller {
// large form-based updates
$transformer = new MangaListTransformer();
$post_data = $transformer->untransform($data);
$full_result = $this->model->updateLibraryItem(new FormItem($post_data));
$full_result = $this->model->updateLibraryItem(FormItem::from($post_data));
if ($full_result['statusCode'] === 200)
{
@ -264,7 +264,7 @@ final class Manga extends Controller {
$data = $this->request->getParsedBody();
}
$response = $this->model->incrementLibraryItem(new FormItem($data));
$response = $this->model->incrementLibraryItem(FormItem::from($data));
$this->cache->clear();
$this->outputJSON($response['body'], $response['statusCode']);

View File

@ -167,7 +167,7 @@ class Anime extends API {
$array['notes'] = '';
}
return new AnimeListItem($array);
return AnimeListItem::from($array);
}
/**

View File

@ -49,12 +49,23 @@ abstract class AbstractType implements ArrayAccess, Countable {
return NULL;
}
/**
* Static constructor
*
* @param mixed $data
* @return static
*/
final public static function from($data): self
{
return new static($data);
}
/**
* Sets the properties by using the constructor
*
* @param mixed $data
*/
final public function __construct($data = [])
final private function __construct($data = [])
{
$typeKeys = array_keys((array)$this);
$dataKeys = array_keys((array)$data);

View File

@ -92,6 +92,6 @@ final class AnimeListItem extends AbstractType {
public function setAnime($anime): void
{
$this->anime = new Anime($anime);
$this->anime = Anime::from($anime);
}
}

View File

@ -62,6 +62,6 @@ final class Character extends AbstractType {
public function setMedia ($media): void
{
$this->media = new Media($media);
$this->media = Media::from($media);
}
}

View File

@ -162,16 +162,16 @@ class Config extends AbstractType {
public function setAnilist ($data): void
{
$this->anilist = new Config\Anilist($data);
$this->anilist = Config\Anilist::from($data);
}
public function setCache ($data): void
{
$this->cache = new Config\Cache($data);
$this->cache = Config\Cache::from($data);
}
public function setDatabase ($data): void
{
$this->database = new Config\Database($data);
$this->database = Config\Database::from($data);
}
}

View File

@ -42,7 +42,7 @@ class FormItem extends AbstractType {
public function setData($value): void
{
$this->data = new FormItemData($value);
$this->data = FormItemData::from($value);
}
}

View File

@ -42,6 +42,6 @@ final class Person extends AbstractType {
public function setCharacters($characters): void
{
$this->characters = new Characters($characters);
$this->characters = Characters::from($characters);
}
}

View File

@ -85,10 +85,10 @@ class MangaListTransformerTest extends AnimeClientTestCase {
];
$actual = $this->transformer->untransform($input);
$expected = new FormItem([
$expected = FormItem::from([
'id' => '15084773',
'mal_id' => '26769',
'data' => new FormItemData([
'data' => FormItemData::from([
'status' => 'current',
'progress' => 67,
'reconsuming' => false,