refactor: Extract a TalkCollection

This commit is contained in:
Oliver Davies 2021-08-28 00:59:44 +01:00
parent ac552fb05b
commit be71d9f255
2 changed files with 32 additions and 8 deletions

View file

@ -4,8 +4,7 @@ declare(strict_types=1);
namespace App\TwigExtension;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use App\Collection\TalkCollection;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
@ -23,15 +22,13 @@ final class TalkExtension extends AbstractExtension
return $this->getEventsFromTalks($talks)->count();
}
private function getEventsFromTalks(iterable $talks): Collection
private function getEventsFromTalks(iterable $talks): TalkCollection
{
$talkCollection = new Collection($talks);
$today = Carbon::today()->format('Y-m-d');
$talkCollection = new TalkCollection($talks);
return $talkCollection
->flatMap(fn($talk): array => (array) $talk['events'])
->filter(fn(array $event): bool => $event['date'] < $today);
->getEvents()
->onlyPastTalks();
}
}