Start the Episode repository.

This commit is contained in:
Dave Smith-Hayes 2024-11-13 03:32:48 +00:00
parent 36522292b0
commit 4f94b80902
2 changed files with 69 additions and 0 deletions

View File

@ -65,4 +65,35 @@ class Episode
return "";
}
/**
* @param array $props The properties of the method
*/
public static function fromArray(array $props): self
{
$episode = new self(
$props['title'],
$props['link'],
(int) $props['length'],
$props['description'],
$props['serialNumber'],
(bool) $props['explicit'],
$props['publishedDate'],
$props['episdoeType']
);
if ($props['id']) {
$episode->setId($props['id']);
}
if ($props['created_at']) {
$episode->setCreatedAt($props['createdAt']);
}
if ($props['updated_id']) {
$episode->setUpdatedAt($props['updatedAt']);
}
return $episode;
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace Slovocast\Domain\Repository\Episode;
use DateTimeImmutable;
use DateTime;
use Slovocast\Domain\Entity\Episode;
use Slovocast\Domain\Repository\Episode\EpisodeRepositoryInterface;
use Slovocast\Exception\EntityNotFoundException;
use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface;
class EpisodeRepository implements EpisodeRepositoryInterface
{
public function __construct(
private DatabaseHandlerInterface $db
) { }
protected function getEpisodeFromResults(array $results): Episode
{
$results['createdAt'] = new DateTimeImmutable($results['created_at']);
$results['updatedAt'] = new DateTime($results['updated_at']);
return Episode::fromArray($results);
}
public function get(int $id): Episode
{
$query = "SELECT * FROM episodes WHERE id = :id";
$statement = $this->db->getConnection()->query($query);
$results = $statement->fetch(\PDO::FETCH_ASSOC);
if (!is_array($results) || !count($results)) {
throw new EntityNotFoundException("Unable to find Episode.");
}
}
}