This commit is contained in:
Oliver Davies 2025-06-16 22:17:33 +01:00
parent cddb3af518
commit f08fb4cd67
5 changed files with 50 additions and 14 deletions

View file

@ -1,3 +1,6 @@
services:
Drupal\opd_presentations\PresentationCounter:
autowire: true
Drupal\opd_presentations\Repository\PresentationNodeRepository:
autowire: true
Drupal\opd_presentations\Repository\PresentationRepositoryInterface: '@Drupal\opd_presentations\Repository\PresentationNodeRepository'

View file

@ -4,26 +4,15 @@ declare(strict_types=1);
namespace Drupal\opd_presentations;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node\NodeInterface;
use Drupal\opd_presentations\Repository\PresentationRepositoryInterface;
final class PresentationCounter {
public function __construct(private EntityTypeManagerInterface $entityTypeManager) {
public function __construct(private PresentationRepositoryInterface $presentationRepository) {
}
public function getPastCount(): int {
$nodeStorage = $this->entityTypeManager->getStorage('node');
$query = $nodeStorage->getQuery();
$query->condition('status', NodeInterface::PUBLISHED);
$query->condition('type', Presentation::NODE_TYPE);
$query->accessCheck();
$nodeIds = $query->execute();
/** @var Presentation[] */
$presentations = $nodeStorage->loadMultiple($nodeIds);
$presentations = $this->presentationRepository->getPublished();
return array_reduce(
array: $presentations,

View file

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_presentations\Repository;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node\NodeInterface;
use Drupal\opd_presentations\Presentation;
final class PresentationNodeRepository implements PresentationRepositoryInterface {
public function __construct(private EntityTypeManagerInterface $entityTypeManager) {
}
public function getPublished(): array {
$nodeStorage = $this->entityTypeManager->getStorage('node');
$query = $nodeStorage->getQuery();
$query->accessCheck();
$query->condition('status', NodeInterface::PUBLISHED);
$query->condition('type', Presentation::NODE_TYPE);
$nodeIds = $query->execute();
return $nodeStorage->loadMultiple($nodeIds);
}
}

View file

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_presentations\Repository;
interface PresentationRepositoryInterface {
/**
* @return Presentation[]
*/
public function getPublished(): array;
}

View file

@ -9,6 +9,7 @@ use Drupal\Tests\opd_presentations\Traits\PresentationCreationTrait;
use Drupal\opd_presentations\Date;
use Drupal\opd_presentations\Events;
use Drupal\opd_presentations\PresentationCounter;
use Drupal\opd_presentations\Repository\PresentationRepositoryInterface;
use weitzman\DrupalTestTraits\ExistingSiteBase;
final class PresentationCounterTest extends ExistingSiteBase {