Inject the TalkRepository into TalkCounter
This commit is contained in:
parent
29d88893fa
commit
afc0a0e448
|
@ -6,6 +6,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;
|
||||
|
||||
|
@ -32,4 +33,16 @@ final class TalkRepository {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection|Talk[]
|
||||
*/
|
||||
public function findAllPublished(): Collection {
|
||||
$talks = $this->nodeStorage->loadByProperties([
|
||||
'status' => NodeInterface::PUBLISHED,
|
||||
'type' => 'talk',
|
||||
]);
|
||||
|
||||
return new Collection($talks);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -5,37 +5,26 @@ declare(strict_types=1);
|
|||
namespace Drupal\opdavies_talks\Service;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\node\NodeInterface;
|
||||
use Drupal\opdavies_talks\Entity\Node\Talk;
|
||||
use Drupal\opdavies_talks\Repository\TalkRepository;
|
||||
use Drupal\paragraphs\ParagraphInterface;
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
|
||||
final class TalkCounter {
|
||||
|
||||
private EntityStorageInterface $nodeStorage;
|
||||
private TalkRepository $talkRepository;
|
||||
|
||||
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
|
||||
$this->nodeStorage = $entityTypeManager->getStorage('node');
|
||||
public function __construct(TalkRepository $talkRepository) {
|
||||
$this->talkRepository = $talkRepository;
|
||||
}
|
||||
|
||||
public function getCount(): int {
|
||||
$today = Carbon::today()->format('Y-m-d H:i:s');
|
||||
|
||||
return $this->getTalks()
|
||||
return $this->talkRepository
|
||||
->findAllPublished()
|
||||
->flatMap(fn(Talk $talk) => $talk->getEvents())
|
||||
->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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -47,7 +47,7 @@ final class TalkRepositoryTest extends TalksTestBase {
|
|||
'status' => NodeInterface::NOT_PUBLISHED,
|
||||
]);
|
||||
|
||||
$talks = $this->talkRepository->getAll(TRUE);
|
||||
$talks = $this->talkRepository->findAllPublished();
|
||||
|
||||
$this->assertCount(1, $talks);
|
||||
$this->assertSame('TDD - Test Driven Drupal', $talks->first()->label());
|
||||
|
|
Loading…
Reference in a new issue