More refactoring of History transformers

This commit is contained in:
Timothy Warren 2020-04-22 17:53:25 -04:00
parent b614505499
commit 8094ff5927
3 changed files with 35 additions and 90 deletions

View File

@ -21,5 +21,11 @@ use Aviat\AnimeClient\API\Mapping\AnimeWatchingStatus;
class AnimeHistoryTransformer extends HistoryTransformer {
protected string $type = 'anime';
protected string $progressAction = 'Watched episode';
protected string $smallAggregateAction = 'Watched episodes';
protected string $largeAggregateAction = 'Marathoned episodes';
protected array $statusMap = AnimeWatchingStatus::KITSU_TO_TITLE;
}

View File

@ -27,6 +27,21 @@ abstract class HistoryTransformer {
*/
protected string $type;
/**
* @var string The message for watching/reading a single episode/chapter
*/
protected string $progressAction;
/**
* @var string The message for going though a small number of media in a series
*/
protected string $smallAggregateAction;
/**
* @var string The message for going through a large number of media in a series
*/
protected string $largeAggregateAction;
/**
* @var array The mapping of api status to display status
*/
@ -101,24 +116,24 @@ abstract class HistoryTransformer {
if (count($entries) > 1)
{
$episodes = [];
$items = [];
$updated = [];
foreach ($entries as $e)
{
$episodes[] = max($e['original']['attributes']['changedData']['progress']);
$items[] = max($e['original']['attributes']['changedData']['progress']);
$updated[] = $e['updated'];
}
$firstEpisode = min($episodes);
$lastEpisode = max($episodes);
$firstItem = min($items);
$lastItem = max($items);
$firstUpdate = min($updated);
$lastUpdate = max($updated);
$title = $entries[0]['title'];
$action = (count($entries) > 3)
? "Marathoned episodes {$firstEpisode}-{$lastEpisode}"
: "Watched episodes {$firstEpisode}-{$lastEpisode}";
? "{$this->largeAggregateAction} {$firstItem}-{$lastItem}"
: "{$this->smallAggregateAction} {$firstItem}-{$lastItem}";
$output[] = HistoryItem::from([
'action' => $action,
@ -147,14 +162,10 @@ abstract class HistoryTransformer {
$data = $entry['relationships'][$this->type][$id]['attributes'];
$title = $this->linkTitle($data);
$imgUrl = "images/{$this->type}/{$id}.webp";
$episode = max($entry['attributes']['changedData']['progress']);
$action = ($this->type === 'anime')
? "Watched episode {$episode}"
: "Read chapter {$episode}";
$item = max($entry['attributes']['changedData']['progress']);
return HistoryItem::from([
'action' => $action,
'action' => "{$this->progressAction} {$item}",
'coverImg' => $imgUrl,
'kind' => 'progressed',
'original' => $entry,

View File

@ -17,87 +17,15 @@
namespace Aviat\AnimeClient\API\Kitsu\Transformer;
use Aviat\AnimeClient\API\Mapping\MangaReadingStatus;
use Aviat\AnimeClient\Types\HistoryItem;
class MangaHistoryTransformer extends HistoryTransformer {
protected string $type = 'manga';
protected string $progressAction = 'Read chapter';
protected string $smallAggregateAction = 'Read chapters';
protected string $largeAggregateAction = 'Blew through chapters';
protected array $statusMap = MangaReadingStatus::KITSU_TO_TITLE;
/**
* Combine consecutive 'progressed' events
*
* @param array $singles
* @return array
*/
protected function aggregate (array $singles): array
{
$output = [];
$count = count($singles);
for ($i = 0; $i < $count; $i++)
{
$entries = [];
$entry = $singles[$i];
$prevTitle = $entry['title'];
$nextId = $i;
$next = $singles[$nextId];
while (
$next['kind'] === 'progressed' &&
$next['title'] === $prevTitle
) {
$entries[] = $next;
$prevTitle = $next['title'];
if ($nextId + 1 < $count)
{
$nextId++;
$next = $singles[$nextId];
continue;
}
break;
}
if (count($entries) > 1)
{
$chapters = [];
$updated = [];
foreach ($entries as $e)
{
$chapters[] = max($e['original']['attributes']['changedData']['progress']);
$updated[] = $e['updated'];
}
$firstChapter = min($chapters);
$lastChapter = max($chapters);
$firstUpdate = min($updated);
$lastUpdate = max($updated);
$title = $entries[0]['title'];
$action = (count($entries) > 3)
? "Marathoned chapters {$firstChapter}-{$lastChapter}"
: "Watched chapters {$firstChapter}-{$lastChapter}";
$output[] = HistoryItem::from([
'action' => $action,
'coverImg' => $entries[0]['coverImg'],
'dateRange' => [$firstUpdate, $lastUpdate],
'isAggregate' => true,
'title' => $title,
'updated' => $entries[0]['updated'],
'url' => $entries[0]['url'],
]);
// Skip the rest of the aggregate in the main loop
$i += count($entries) - 1;
continue;
}
$output[] = $entry;
}
return $output;
}
}