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.
30 lines
675 B
PHP
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();
|
|
}
|
|
|
|
}
|