Start with a channel aggregate.
This commit is contained in:
parent
7f322080c9
commit
25b9f2b798
49
app/src/Domain/Aggregate/ChannelAggregate.php
Normal file
49
app/src/Domain/Aggregate/ChannelAggregate.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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']);
|
||||||
|
}
|
||||||
|
}
|
@ -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
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user