From 0c1321fed5c2fe31c889c8fde7a92ab8dde0845e Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 10 Feb 2021 07:54:43 +0000 Subject: [PATCH] Add TalkCollection, move logic for getting events Add a `TalkCollection` which extends Laravel/Tighten's, and add a method there for getting the events from the talks. This makes this logic more reusable and also makes the code in the `TalkCounter` service simpler. --- .../talks/src/Collection/TalkCollection.php | 22 +++++++++++++++++++ .../talks/src/Repository/TalkRepository.php | 17 +++++--------- .../custom/talks/src/Service/TalkCounter.php | 6 ++--- 3 files changed, 30 insertions(+), 15 deletions(-) create mode 100644 web/modules/custom/talks/src/Collection/TalkCollection.php diff --git a/web/modules/custom/talks/src/Collection/TalkCollection.php b/web/modules/custom/talks/src/Collection/TalkCollection.php new file mode 100644 index 0000000..f0644e4 --- /dev/null +++ b/web/modules/custom/talks/src/Collection/TalkCollection.php @@ -0,0 +1,22 @@ +flatMap(fn(Talk $talk): Collection => $talk->getEvents()); + } + +} diff --git a/web/modules/custom/talks/src/Repository/TalkRepository.php b/web/modules/custom/talks/src/Repository/TalkRepository.php index 6f847b1..1a6a728 100644 --- a/web/modules/custom/talks/src/Repository/TalkRepository.php +++ b/web/modules/custom/talks/src/Repository/TalkRepository.php @@ -7,8 +7,7 @@ namespace Drupal\opdavies_talks\Repository; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\node\NodeInterface; -use Drupal\opdavies_talks\Entity\Node\Talk; -use Tightenco\Collect\Support\Collection; +use Drupal\opdavies_talks\Collection\TalkCollection; final class TalkRepository { @@ -18,19 +17,13 @@ final class TalkRepository { $this->nodeStorage = $entityTypeManager->getStorage('node'); } - /** - * @return Collection|Talk[] - */ - public function findAll(): Collection { + public function findAll(): TalkCollection { $talks = $this->nodeStorage->loadByProperties($this->defaultProperties()); - return new Collection($talks); + return new TalkCollection($talks); } - /** - * @return Collection|Talk[] - */ - public function findAllPublished(): Collection { + public function findAllPublished(): TalkCollection { $talks = $this->nodeStorage->loadByProperties(array_merge( $this->defaultProperties(), [ @@ -38,7 +31,7 @@ final class TalkRepository { ], )); - return new Collection($talks); + return new TalkCollection($talks); } private function defaultProperties(): array { diff --git a/web/modules/custom/talks/src/Service/TalkCounter.php b/web/modules/custom/talks/src/Service/TalkCounter.php index 185c19b..4d132b0 100644 --- a/web/modules/custom/talks/src/Service/TalkCounter.php +++ b/web/modules/custom/talks/src/Service/TalkCounter.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace Drupal\opdavies_talks\Service; use Carbon\Carbon; -use Drupal\opdavies_talks\Entity\Node\Talk; use Drupal\opdavies_talks\Repository\TalkRepository; use Drupal\paragraphs\ParagraphInterface; @@ -22,8 +21,9 @@ final class TalkCounter { return $this->talkRepository ->findAllPublished() - ->flatMap(fn(Talk $talk) => $talk->getEvents()) - ->filter(fn(ParagraphInterface $event) => $event->get('field_date')->getString() <= $today) + ->getEvents() + ->filter(fn(ParagraphInterface $event) => $event->get('field_date') + ->getString() <= $today) ->count(); }