Start working on repository classes for the Image class

This commit is contained in:
Dave Smith-Hayes 2024-11-08 22:03:38 -05:00
parent fc0d816062
commit 1d00f2bda4
10 changed files with 183 additions and 10 deletions

View File

@ -38,7 +38,7 @@ use Slovocast\Domain\Repository\User\UserRepository;
use Slovocast\Domain\Repository\User\UserRepositoryInterface;
use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface;
use Slovocast\Infrastructure\Database\DatabaseConnection;
use Slovocast\Infrastructure\Database\DatabaseHandler;
use Slovocast\Infrastructure\Api\User\UserAuthorizationInterface;
use Slovocast\Infrastructure\User\BasicUserAuthorization;
@ -143,7 +143,7 @@ class Bootstrap
*/
DatabaseHandlerInterface::class => function (ContainerInterface $container) {
$config = $container->get('config');
return new DatabaseConnection(
return new DatabaseHandler(
$config->get('database.host'),
$config->get('database.username'),
$config->get('database.password'),

View File

@ -0,0 +1,65 @@
<?php
namespace Slovocast\Domain\Entity;
use Slovocast\Domain\Entity as EntityTrait;
class Image
{
use EntityTrait;
public function __construct(
private string $url,
private string $title,
private int $width,
private int $height,
) { }
public function getUrl(): string
{
return $this->url;
}
public function getTitle(): string
{
return $this->title;
}
public function getWidth(): int
{
return $this->width;
}
public function getHeight(): int
{
return $this->height;
}
/**
* @param array $props Each property name as the key, and then the value
*/
public static function fromArray(array $props): Image
{
$image = new self(
$props['url'],
$props['title'],
$props['height'],
$props['width'],
);
if ($props['id']) {
$image->setId($props['id']);
}
if ($props['updatedAt']) {
$image->setUpdatedAt($props['updatedAt']);
}
if ($props['createdAt']) {
$image->setCreatedAt($props['createdAt']);
}
return $image;
}
}

View File

@ -98,7 +98,7 @@ class ChannelRepository implements ChannelRepositoryInterface
/**
* @throws Throwable
*/
public function create(Channel $channel, User $owner): int
public function create(Channel $channel, User $owner): Channel
{
$query = "
INSERT INTO channels (name, slug, description, link,
@ -118,7 +118,9 @@ class ChannelRepository implements ChannelRepositoryInterface
$owner->getId()
]);
return $this->db->getConnection()->lastInsertId();
$insertId = $this->db->getConnection()->lastInsertId();
$channel->setId($insertId);
return $channel;
}
/**

View File

@ -10,7 +10,7 @@ interface ChannelRepositoryInterface
public function get(int $id): Channel;
public function getFromUser(User $user): Channel;
public function getFromSlug(string $slug): Channel;
public function create(Channel $channel): int;
public function create(Channel $channel, User $user): int;
public function update(Channel $channel): bool;
public function delete(Channel $channel): bool;
}

View File

@ -0,0 +1,62 @@
<?php
namespace Slovocast\Domain\Repository\Image;
use DateTimeImmutable;
use DateTime;
use Slovocast\Exception\EntityNotFoundException;
use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface;
use Slovocast\Domain\Repository\Image\ImageRepositoryInterface;
use Slovocast\Domain\Entity\Image;
class ImageRepository implements ImageRepositoryInterface
{
public function __construct(
private DatabaseHandlerInterface $db
) { }
/**
* @param array $results The results of a query
*/
protected function createImageFromResults(array $results): Image
{
$results['createdAt'] = new DateTimeImmutable($results['created_at']);
$results['updatedAt'] = new DateTime($results['updated_at']);
return Image::fromArray($results);
}
public function get(int $id): Image
{
$query = "SELECT * FROM images WHERE id = :id";
$statement = $this->db->getConnection()->prepare($query);
$statement->execute([ ':id' => $id ]);
$results = $statement->fetch(\PDO::FETCH_ASSOC);
if (!is_array($results) || !count($results)) {
throw new EntityNotFoundException("Unable to find image.");
}
return Image::fromArray($results);
}
public function getFromUrl(string $url): Image
{
}
public function create(Image $image): Image
{
return $image;
}
public function update(Image $image): bool
{
}
public function delete(Image $image): bool
{
return false;
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace Slovocast\Domain\Repository\Image;
use Slovocast\Domain\RepositoryInterface;
use Slovocast\Domain\Entity\Image;
interface ImageRepositoryInterface extends RepositoryInterface
{
public function get(int $id): Image;
public function getFromUrl(string $url): Image;
public function create(Image $image): Image;
public function update(Image $image): bool;
public function delete(Image $image): bool;
}

View File

@ -65,7 +65,7 @@ class UserRepository implements UserRepositoryInterface
return $this->userFromQueryResults($results);
}
public function create(User $user): bool
public function create(User $user): User
{
$query = "INSERT INTO users (email, password, name)
VALUES (:email, :password, :name)";
@ -76,7 +76,10 @@ class UserRepository implements UserRepositoryInterface
':name' => $user->getName(),
]);
return (bool) $results;
$insertId = $this->db->getConnection()->lastInsertId();
$user->setId($insertId);
return $user;
}
public function update(User $user): bool
@ -96,6 +99,14 @@ class UserRepository implements UserRepositoryInterface
]);
}
/**
* @TODO Figure out soft/hard delete logic for users
*/
public function delete(User $user): bool
{
return false;
}
public function verifyPassword(string $email, string $password): bool
{
try {

View File

@ -2,13 +2,15 @@
namespace Slovocast\Domain\Repository\User;
use Slovocast\Domain\RepositoryInterface;
use Slovocast\Domain\Entity\User;
interface UserRepositoryInterface
interface UserRepositoryInterface extends RepositoryInterface
{
public function get(int $id): User;
public function getFromEmail(string $email): User;
public function create(User $user): bool;
public function create(User $user): User;
public function update(User $user): bool;
public function delete(User $user): bool;
public function verifyPassword(string $email, string $password): bool;
}

View File

@ -0,0 +1,16 @@
<?php
namespace Slovocast\Domain;
/**
* Base Repository Interfade that defines very simple versions of the CRUD
* methods. Most Repositories will implement these methods with overrides for
* their entities, plus more Domain specific methods.
*/
interface RepositoryInterface
{
public function get(int|string $id): object;
public function create(object $entity): object;
public function update(object $entity): bool;
public function delete(object $entity): bool;
}

View File

@ -5,7 +5,7 @@ namespace Slovocast\Infrastructure\Database;
use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface;
use PDO;
class DatabaseConnection implements DatabaseHandlerInterface
class DatabaseHandler implements DatabaseHandlerInterface
{
private PDO $pdo;