Start with a channel aggregate.

This commit is contained in:
Dave Smith-Hayes 2024-12-31 09:55:58 -05:00
parent 7f322080c9
commit 25b9f2b798
4 changed files with 92 additions and 2 deletions

View File

@ -0,0 +1,49 @@
<?php
namespace Slovocast\Domain\Aggregate;
use Slovocast\Domain\Entity\Episode;
use Slovocast\Domain\Entity\Channel;
class ChannelAggregate
{
/**
* @param Channel $channel
* @param array<Episode> $episodes
*/
public function __construct(
protected Channel $channel,
protected array $episodes
) {
$this->setEpisodes($episodes);
}
public function hasEpisodes(): bool
{
return (bool) count($this->episodes);
}
/**
* @return array<Episode>
*/
public function getEpisodes(): array
{
return $this->episodes;
}
public function addEpisode(Episode $episode): void
{
$this->episodes[] = $episode;
}
/**
* @param array<Episode> $episodes
* @return void
*/
public function setEpisodes(array $episodes): void
{
foreach ($episodes as $episode) {
$this->addEpisode($episode);
}
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace Slovocast\Domain\Repository\Channel;
use Slovocast\Domain\Entity\Episode;
use Slovocast\Domain\Aggregate\ChannelAggregate;
use Slovocast\Domain\Repository\Channel\ChannelRepositoryInterface;
use Slovocast\Domain\Repository\Episode\EpisodeRepositoryInterface;
use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface;
class ChannelAggregateRepository
{
public function __construct(
protected DatabaseHandlerInterface $database,
protected ChannelRepositoryInterface $channelRepository,
protected EpisodeRepositoryInterface $episodeRepository,
) { }
public function getChannelWithEpisodes(int $id): ChannelAggregate
{
$channel = $this->channelRepository->get($id);
$episodes = $this->episodeRepository->getFromChannel($channel);
return new ChannelAggregate($channel, $episodes);
}
public function getChannelByEpisode(Episode $episode): ChannelAggregate
{
$query = "SELECT c.id as channel_id, e.id as episode_id
FROM channel c
LEFT JOIN episodes e ON e.channel_id = c.id
WHERE e.id = :id";
$results = $this->database->query($query, [
":id" => $episode->getId()
]);
$row = array_shift($results);
return $this->getChannelByEpisode($row['id']);
}
}

View File

@ -17,7 +17,8 @@ class EpisodeRepository implements EpisodeRepositoryInterface
) { } ) { }
/** /**
* @param array The query results from the database * @param array $results The query results from the database
* @return Episode
*/ */
protected function getEpisodeFromResults(array $results): Episode protected function getEpisodeFromResults(array $results): Episode
{ {