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.
This commit is contained in:
parent
1f0926ac16
commit
0c1321fed5
22
web/modules/custom/talks/src/Collection/TalkCollection.php
Normal file
22
web/modules/custom/talks/src/Collection/TalkCollection.php
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Drupal\opdavies_talks\Collection;
|
||||||
|
|
||||||
|
use Drupal\opdavies_talks\Entity\Node\Talk;
|
||||||
|
use Drupal\paragraphs\ParagraphInterface;
|
||||||
|
use Tightenco\Collect\Support\Collection;
|
||||||
|
|
||||||
|
final class TalkCollection extends Collection {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the events for the talks in the Collection.
|
||||||
|
*
|
||||||
|
* @return Collection|ParagraphInterface[]
|
||||||
|
*/
|
||||||
|
public function getEvents(): Collection {
|
||||||
|
return $this->flatMap(fn(Talk $talk): Collection => $talk->getEvents());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -7,8 +7,7 @@ namespace Drupal\opdavies_talks\Repository;
|
||||||
use Drupal\Core\Entity\EntityStorageInterface;
|
use Drupal\Core\Entity\EntityStorageInterface;
|
||||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||||
use Drupal\node\NodeInterface;
|
use Drupal\node\NodeInterface;
|
||||||
use Drupal\opdavies_talks\Entity\Node\Talk;
|
use Drupal\opdavies_talks\Collection\TalkCollection;
|
||||||
use Tightenco\Collect\Support\Collection;
|
|
||||||
|
|
||||||
final class TalkRepository {
|
final class TalkRepository {
|
||||||
|
|
||||||
|
@ -18,19 +17,13 @@ final class TalkRepository {
|
||||||
$this->nodeStorage = $entityTypeManager->getStorage('node');
|
$this->nodeStorage = $entityTypeManager->getStorage('node');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function findAll(): TalkCollection {
|
||||||
* @return Collection|Talk[]
|
|
||||||
*/
|
|
||||||
public function findAll(): Collection {
|
|
||||||
$talks = $this->nodeStorage->loadByProperties($this->defaultProperties());
|
$talks = $this->nodeStorage->loadByProperties($this->defaultProperties());
|
||||||
|
|
||||||
return new Collection($talks);
|
return new TalkCollection($talks);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function findAllPublished(): TalkCollection {
|
||||||
* @return Collection|Talk[]
|
|
||||||
*/
|
|
||||||
public function findAllPublished(): Collection {
|
|
||||||
$talks = $this->nodeStorage->loadByProperties(array_merge(
|
$talks = $this->nodeStorage->loadByProperties(array_merge(
|
||||||
$this->defaultProperties(),
|
$this->defaultProperties(),
|
||||||
[
|
[
|
||||||
|
@ -38,7 +31,7 @@ final class TalkRepository {
|
||||||
],
|
],
|
||||||
));
|
));
|
||||||
|
|
||||||
return new Collection($talks);
|
return new TalkCollection($talks);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function defaultProperties(): array {
|
private function defaultProperties(): array {
|
||||||
|
|
|
@ -5,7 +5,6 @@ declare(strict_types=1);
|
||||||
namespace Drupal\opdavies_talks\Service;
|
namespace Drupal\opdavies_talks\Service;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Drupal\opdavies_talks\Entity\Node\Talk;
|
|
||||||
use Drupal\opdavies_talks\Repository\TalkRepository;
|
use Drupal\opdavies_talks\Repository\TalkRepository;
|
||||||
use Drupal\paragraphs\ParagraphInterface;
|
use Drupal\paragraphs\ParagraphInterface;
|
||||||
|
|
||||||
|
@ -22,8 +21,9 @@ final class TalkCounter {
|
||||||
|
|
||||||
return $this->talkRepository
|
return $this->talkRepository
|
||||||
->findAllPublished()
|
->findAllPublished()
|
||||||
->flatMap(fn(Talk $talk) => $talk->getEvents())
|
->getEvents()
|
||||||
->filter(fn(ParagraphInterface $event) => $event->get('field_date')->getString() <= $today)
|
->filter(fn(ParagraphInterface $event) => $event->get('field_date')
|
||||||
|
->getString() <= $today)
|
||||||
->count();
|
->count();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue