From be71d9f255c834734f6450ee21af12af75870850 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sat, 28 Aug 2021 00:59:44 +0100 Subject: [PATCH] refactor: Extract a TalkCollection --- src/Collection/TalkCollection.php | 27 +++++++++++++++++++++++++++ src/TwigExtension/TalkExtension.php | 13 +++++-------- 2 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 src/Collection/TalkCollection.php diff --git a/src/Collection/TalkCollection.php b/src/Collection/TalkCollection.php new file mode 100644 index 000000000..7389990f4 --- /dev/null +++ b/src/Collection/TalkCollection.php @@ -0,0 +1,27 @@ +flatMap(fn($talk): array => (array) $talk[self::KEY_EVENTS]); + } + + public function onlyPastTalks(): self + { + $today = Carbon::today()->format(self::DATE_FORMAT); + + return $this->filter(fn(array $event): bool => $event[self::KEY_EVENT_DATE] < $today); + } +} diff --git a/src/TwigExtension/TalkExtension.php b/src/TwigExtension/TalkExtension.php index 941e8d0c8..782ec6a3a 100644 --- a/src/TwigExtension/TalkExtension.php +++ b/src/TwigExtension/TalkExtension.php @@ -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(); } }