HummingBirdAnimeClient/src/Command/UpdateThumbnails.php

88 lines
2.2 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
/**
* Hummingbird Anime List Client
*
* An API client for Kitsu to manage anime and manga watch lists
*
2019-12-03 15:17:25 -05:00
* PHP version 7.2
*
* @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net>
2019-12-06 09:16:35 -05:00
* @copyright 2015 - 2019 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2019-12-06 09:16:35 -05:00
* @version 4.2
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\Command;
use Aviat\AnimeClient\API\JsonAPI;
2019-12-09 14:34:23 -05:00
use Aviat\AnimeClient\API\Kitsu\Model;
2019-05-08 13:19:03 -04:00
use Aviat\AnimeClient\Controller\Images;
/**
* Clears out image cache directories, then re-creates the image cache
* for manga and anime
*/
final class UpdateThumbnails extends ClearThumbnails {
/**
* Model for making requests to Kitsu API
2019-12-09 14:34:23 -05:00
* @var Model
*/
protected $kitsuModel;
/**
* The default controller, which has the method to cache the images
*/
protected $controller;
public function execute(array $args, array $options = []): void
{
$this->setContainer($this->setupContainer());
$this->setCache($this->container->get('cache'));
2019-05-08 13:19:03 -04:00
$this->controller = new Images($this->container);
$this->kitsuModel = $this->container->get('kitsu-model');
// Clear the existing thunbnails
parent::execute($args, $options);
$ids = $this->getImageList();
// Resave the images
foreach($ids as $type => $typeIds)
{
foreach ($typeIds as $id)
{
2019-05-08 13:19:03 -04:00
$this->controller->cache($type, "{$id}.jpg", FALSE);
}
$this->echoBox("Finished regenerating {$type} thumbnails");
}
$this->echoBox('Finished regenerating all thumbnails');
}
2019-12-06 15:46:56 -05:00
/**
2019-12-09 14:34:23 -05:00
* @return array array-key[][]
2019-12-06 15:46:56 -05:00
* @psalm-return array{anime: list<array-key>, manga: list<array-key>}
*/
public function getImageList(): array
{
$mangaList = $this->kitsuModel->getFullRawMangaList();
$includes = JsonAPI::organizeIncludes($mangaList['included']);
$mangaIds = array_keys($includes['manga']);
$animeList = $this->kitsuModel->getFullRawAnimeList();
$includes = JsonAPI::organizeIncludes($animeList['included']);
$animeIds = array_keys($includes['anime']);
// print_r($mangaIds);
// die();
return [
'anime' => $animeIds,
'manga' => $mangaIds,
];
}
}