Add interfaces for the repository class.

This commit is contained in:
Dave Smith-Hayes 2024-12-31 10:17:31 -05:00
parent e300f2b679
commit 057a61b3ee
2 changed files with 31 additions and 4 deletions

View File

@ -3,12 +3,13 @@
namespace Slovocast\Domain\Repository\Channel; namespace Slovocast\Domain\Repository\Channel;
use Slovocast\Domain\Entity\Episode; use Slovocast\Domain\Entity\Episode;
use Slovocast\Domain\Entity\Channel;
use Slovocast\Domain\Aggregate\ChannelAggregate; use Slovocast\Domain\Aggregate\ChannelAggregate;
use Slovocast\Domain\Repository\Channel\ChannelRepositoryInterface; use Slovocast\Domain\Repository\Channel\ChannelRepositoryInterface;
use Slovocast\Domain\Repository\Episode\EpisodeRepositoryInterface; use Slovocast\Domain\Repository\Episode\EpisodeRepositoryInterface;
use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface; use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface;
class ChannelAggregateRepository class ChannelAggregateRepository implements ChannelAggregateRepositoryInterface
{ {
public function __construct( public function __construct(
protected DatabaseHandlerInterface $database, protected DatabaseHandlerInterface $database,
@ -16,14 +17,20 @@ class ChannelAggregateRepository
protected EpisodeRepositoryInterface $episodeRepository, protected EpisodeRepositoryInterface $episodeRepository,
) { } ) { }
public function getChannelWithEpisodes(int $id): ChannelAggregate public function get(int $id): ChannelAggregate
{ {
$channel = $this->channelRepository->get($id); $channel = $this->channelRepository->get($id);
$episodes = $this->episodeRepository->getFromChannel($channel); $episodes = $this->episodeRepository->getFromChannel($channel);
return new ChannelAggregate($channel, $episodes); return new ChannelAggregate($channel, $episodes);
} }
public function getChannelByEpisode(Episode $episode): ChannelAggregate public function getFromChannel(Channel $channel): ChannelAggregate
{
$episodes = $this->episodeRepository->getFromChannel($channel);
return new ChannelAggregate($channel, $episodes);
}
public function getFromEpisode(Episode $episode): ChannelAggregate
{ {
$query = "SELECT c.id as channel_id, e.id as episode_id $query = "SELECT c.id as channel_id, e.id as episode_id
FROM channel c FROM channel c
@ -35,6 +42,6 @@ class ChannelAggregateRepository
]); ]);
$row = array_shift($results); $row = array_shift($results);
return $this->getChannelByEpisode($row['id']); return $this->get($row['id']);
} }
} }

View File

@ -0,0 +1,20 @@
<?php
namespace Slovocast\Domain\Repository\Channel;
use Slovocast\Domain\Aggregate\ChannelAggregate;
use Slovocast\Domain\Entity\Channel;
use Slovocast\Domain\Entity\Episode;
/**
* The ChannelAggregateRepository should only be used to retrieve data from
* the data source, and not write anything specifically. As the Aggregate is
* a combination of Channel and Episode models, we should use those
* respective repositories for writing data to the datastore.
*/
interface ChannelAggregateRepositoryInterface
{
public function get(int $id): ChannelAggregate;
public function getFromChannel(Channel $channel): ChannelAggregate;
public function getFromEpisode(Episode $episode): ChannelAggregate;
}