Return the email nodes, not just the node IDs

This commit is contained in:
Oliver Davies 2025-05-12 08:19:33 +01:00
parent 7e32227e7d
commit 1013305d38
4 changed files with 44 additions and 9 deletions

View file

@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_daily_emails\Collection;
use Drupal\node\NodeInterface;
final class DailyEmailCollection implements \Countable {
public function __construct(
private array $emails,
) {
}
public function count(): int {
return count($this->emails);
}
public function first(): NodeInterface {
return array_values($this->emails)[0];
}
}

View file

@ -6,6 +6,7 @@ namespace Drupal\opd_daily_emails\Repository;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node\NodeInterface; use Drupal\node\NodeInterface;
use Drupal\opd_daily_emails\Collection\DailyEmailCollection;
final class DailyEmailNodeRepository implements DailyEmailRepositoryInterface { final class DailyEmailNodeRepository implements DailyEmailRepositoryInterface {
@ -14,20 +15,21 @@ final class DailyEmailNodeRepository implements DailyEmailRepositoryInterface {
) { ) {
} }
public function getAll(): array { public function getAll(): DailyEmailCollection {
$query = $this->entityTypeManager $nodeStorage = $this->entityTypeManager
->getStorage('node') ->getStorage('node');
->getQuery();
$query = $nodeStorage->getQuery();
$query->condition('status', NodeInterface::PUBLISHED); $query->condition('status', NodeInterface::PUBLISHED);
$query->condition('type', 'daily_email'); $query->condition('type', 'daily_email');
$query->accessCheck(TRUE); $query->accessCheck(TRUE);
$result = $query->execute(); $nodeIds = $query->execute();
$nodes = $nodeStorage->loadMultiple($nodeIds);
// TODO: this returns a list of node IDs. Load and return the nodes. return new DailyEmailCollection($nodes);
return $result;
} }
} }

View file

@ -4,8 +4,10 @@ declare(strict_types=1);
namespace Drupal\opd_daily_emails\Repository; namespace Drupal\opd_daily_emails\Repository;
use Drupal\opd_daily_emails\Collection\DailyEmailCollection;
interface DailyEmailRepositoryInterface { interface DailyEmailRepositoryInterface {
public function getAll(): array; public function getAll(): DailyEmailCollection;
} }

View file

@ -39,9 +39,16 @@ final class DailyEmailNodeRepositoryTest extends EntityKernelTestBase {
expected: DailyEmailRepositoryInterface::class, expected: DailyEmailRepositoryInterface::class,
); );
$emails = $repository->getAll();
$this->assertCount( $this->assertCount(
expectedCount: 1, expectedCount: 1,
haystack: $repository->getAll(), haystack: $emails,
);
$this->assertSame(
expected: 'A published daily email',
actual: $emails->first()->label(),
); );
} }