2020-06-30 12:50:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-08-24 09:26:44 +01:00
|
|
|
namespace Drupal\opdavies_talks\Service;
|
2020-06-30 12:50:21 +01:00
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Drupal\Core\Entity\EntityStorageInterface;
|
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
|
|
|
use Drupal\node\NodeInterface;
|
2020-08-28 18:01:47 +01:00
|
|
|
use Drupal\opdavies_talks\Entity\Node\Talk;
|
2020-06-30 12:50:21 +01:00
|
|
|
use Drupal\paragraphs\ParagraphInterface;
|
2020-12-17 23:25:59 +00:00
|
|
|
use Tightenco\Collect\Support\Collection;
|
2020-06-30 12:50:21 +01:00
|
|
|
|
|
|
|
final class TalkCounter {
|
|
|
|
|
|
|
|
private EntityStorageInterface $nodeStorage;
|
|
|
|
|
|
|
|
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
|
|
|
|
$this->nodeStorage = $entityTypeManager->getStorage('node');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCount(): int {
|
|
|
|
$today = Carbon::today()->format('Y-m-d H:i:s');
|
|
|
|
|
|
|
|
return $this->getTalks()
|
2020-08-28 18:01:47 +01:00
|
|
|
->flatMap(fn(Talk $talk) => $talk->getEvents())
|
2020-06-30 12:50:21 +01:00
|
|
|
->filter(fn(ParagraphInterface $event) => $event->get('field_date')->getString() <= $today)
|
|
|
|
->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getTalks(): Collection {
|
|
|
|
$talks = $this->nodeStorage->loadByProperties([
|
|
|
|
'status' => NodeInterface::PUBLISHED,
|
|
|
|
'type' => 'talk',
|
|
|
|
]);
|
|
|
|
|
|
|
|
return new Collection($talks);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|