diff --git a/app/src/Domain/Aggregate/ChannelAggregate.php b/app/src/Domain/Aggregate/ChannelAggregate.php new file mode 100644 index 0000000..d6c543b --- /dev/null +++ b/app/src/Domain/Aggregate/ChannelAggregate.php @@ -0,0 +1,49 @@ + $episodes + */ + public function __construct( + protected Channel $channel, + protected array $episodes + ) { + $this->setEpisodes($episodes); + } + + public function hasEpisodes(): bool + { + return (bool) count($this->episodes); + } + + /** + * @return array + */ + public function getEpisodes(): array + { + return $this->episodes; + } + + public function addEpisode(Episode $episode): void + { + $this->episodes[] = $episode; + } + + /** + * @param array $episodes + * @return void + */ + public function setEpisodes(array $episodes): void + { + foreach ($episodes as $episode) { + $this->addEpisode($episode); + } + } +} diff --git a/app/src/Domain/Entity/Channel.php b/app/src/Domain/Entity/Channel.php index 700e5a7..ca35e1e 100644 --- a/app/src/Domain/Entity/Channel.php +++ b/app/src/Domain/Entity/Channel.php @@ -16,7 +16,7 @@ class Channel private ?string $language = '', private ?string $copyright = '', private ?bool $explicit = false, - ) {} + ) { } public function getName(): string { diff --git a/app/src/Domain/Repository/Channel/ChannelAggregateRepository.php b/app/src/Domain/Repository/Channel/ChannelAggregateRepository.php new file mode 100644 index 0000000..d7b28b3 --- /dev/null +++ b/app/src/Domain/Repository/Channel/ChannelAggregateRepository.php @@ -0,0 +1,40 @@ +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']); + } +} diff --git a/app/src/Domain/Repository/Episode/EpisodeRepository.php b/app/src/Domain/Repository/Episode/EpisodeRepository.php index ebc1da0..334e8dc 100644 --- a/app/src/Domain/Repository/Episode/EpisodeRepository.php +++ b/app/src/Domain/Repository/Episode/EpisodeRepository.php @@ -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 {