oliverdavies.uk/web/modules/custom/talks/src/Service/TalkCounter.php
Oliver Davies 0c1321fed5 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.
2021-02-11 08:53:16 +00:00

30 lines
675 B
PHP

<?php
declare(strict_types=1);
namespace Drupal\opdavies_talks\Service;
use Carbon\Carbon;
use Drupal\opdavies_talks\Repository\TalkRepository;
use Drupal\paragraphs\ParagraphInterface;
final class TalkCounter {
private TalkRepository $talkRepository;
public function __construct(TalkRepository $talkRepository) {
$this->talkRepository = $talkRepository;
}
public function getCount(): int {
$today = Carbon::today()->format('Y-m-d H:i:s');
return $this->talkRepository
->findAllPublished()
->getEvents()
->filter(fn(ParagraphInterface $event) => $event->get('field_date')
->getString() <= $today)
->count();
}
}