chore: complete update
This commit is contained in:
106
.archive/Media/Repositories/ImageRepository.php
Normal file
106
.archive/Media/Repositories/ImageRepository.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace Media\Repositories;
|
||||
|
||||
use App\Framework\Database\Connection;
|
||||
use App\Framework\Database\ConnectionInterface;
|
||||
use Media\Entities\Image;
|
||||
|
||||
class ImageRepository
|
||||
{
|
||||
public function __construct(
|
||||
private ConnectionInterface $db
|
||||
) {
|
||||
|
||||
}
|
||||
|
||||
public function save(Image $image): Image
|
||||
{
|
||||
$query = "INSERT INTO images "
|
||||
. "(filename, original_filename, mime_type, file_size, width, height, hash, upload_path, created_at) "
|
||||
. "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";
|
||||
|
||||
$this->db->execute(
|
||||
$query,
|
||||
[
|
||||
$image->filename,
|
||||
$image->originalFilename,
|
||||
$image->mimeType,
|
||||
$image->fileSize,
|
||||
$image->width,
|
||||
$image->height,
|
||||
$image->hash,
|
||||
$image->uploadPath,
|
||||
$image->createdAt->format('Y-m-d H:i:s'),
|
||||
]
|
||||
);
|
||||
|
||||
$image->id = (int)$this->db->lastInsertId();
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
public function update(Image $image): void
|
||||
{
|
||||
$query = "UPDATE images SET "
|
||||
. "filename = ?, original_filename = ?, mime_type = ?, file_size = ?, "
|
||||
. "width = ?, height = ?, hash = ?, upload_path = ?, updated_at = CURRENT_TIMESTAMP "
|
||||
. "WHERE id = ?;";
|
||||
|
||||
$this->db->execute(
|
||||
$query,
|
||||
[
|
||||
$image->filename,
|
||||
$image->originalFilename,
|
||||
$image->mimeType,
|
||||
$image->fileSize,
|
||||
$image->width,
|
||||
$image->height,
|
||||
$image->hash,
|
||||
$image->uploadPath,
|
||||
$image->id,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function findById(int $id): ?Image
|
||||
{
|
||||
$query = "SELECT * FROM images WHERE id = ?;";
|
||||
$result = $this->db->query($query, [$id])->fetch();
|
||||
|
||||
if (null === $result) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->mapToEntity($result);
|
||||
}
|
||||
|
||||
public function findByHash(string $hash): ?Image
|
||||
{
|
||||
$query = "SELECT * FROM images WHERE hash = ?;";
|
||||
$result = $this->db->query($query, [$hash])->fetch();
|
||||
|
||||
if (!$result) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->mapToEntity($result);
|
||||
}
|
||||
|
||||
private function mapToEntity(array $data): Image
|
||||
{
|
||||
return new Image(
|
||||
id: (int)$data['id'],
|
||||
filename: $data['filename'],
|
||||
originalFilename: $data['original_filename'],
|
||||
mimeType: $data['mime_type'],
|
||||
fileSize: (int)$data['file_size'],
|
||||
width: (int)$data['width'],
|
||||
height: (int)$data['height'],
|
||||
hash: $data['hash'],
|
||||
uploadPath: $data['upload_path'],
|
||||
createdAt: new \DateTimeImmutable($data['created_at']),
|
||||
updatedAt: $data['updated_at'] ? new \DateTimeImmutable($data['updated_at']) : null,
|
||||
);
|
||||
}
|
||||
}
|
||||
74
.archive/Media/Repositories/ImageVariantRepository.php
Normal file
74
.archive/Media/Repositories/ImageVariantRepository.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace Media\Repositories;
|
||||
|
||||
use App\Framework\Database\Connection;
|
||||
use App\Framework\Database\ConnectionInterface;
|
||||
use Media\Entities\ImageVariant;
|
||||
|
||||
class ImageVariantRepository
|
||||
{
|
||||
public function __construct(
|
||||
private ConnectionInterface $db
|
||||
) {}
|
||||
|
||||
public function save(ImageVariant $variant): ImageVariant
|
||||
{
|
||||
$query = "INSERT INTO image_variants "
|
||||
. "(image_id, variant, format, width, height, file_size, filename, created_at) "
|
||||
. "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
|
||||
$this->db->execute(
|
||||
$query,
|
||||
[
|
||||
$variant->imageId,
|
||||
$variant->variant,
|
||||
$variant->format,
|
||||
$variant->width,
|
||||
$variant->height,
|
||||
$variant->fileSize,
|
||||
$variant->filename,
|
||||
$variant->createdAt->format('Y-m-d H:i:s'),
|
||||
]
|
||||
);
|
||||
|
||||
$variant->id = (int)$this->db->lastInsertId();
|
||||
|
||||
return $variant;
|
||||
}
|
||||
|
||||
public function findByImageId(int $imageId): array
|
||||
{
|
||||
$query = "SELECT * FROM image_variants WHERE image_id = ?;";
|
||||
$results = $this->db->query($query, [$imageId]);
|
||||
|
||||
return array_map(fn($data) => $this->mapToEntity($data), $results);
|
||||
}
|
||||
|
||||
public function findByImageIdAndVariant(int $imageId, string $variant, string $format): ?ImageVariant
|
||||
{
|
||||
$query = "SELECT * FROM image_variants WHERE image_id = ? AND variant = ? AND format = ?;";
|
||||
$result = $this->db->query($query, [$imageId, $variant, $format]);
|
||||
|
||||
if (empty($result)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->mapToEntity($result[0]);
|
||||
}
|
||||
|
||||
private function mapToEntity(array $data): ImageVariant
|
||||
{
|
||||
return new ImageVariant(
|
||||
id: (int)$data['id'],
|
||||
imageId: (int)$data['image_id'],
|
||||
variant: $data['variant'],
|
||||
format: $data['format'],
|
||||
width: (int)$data['width'],
|
||||
height: (int)$data['height'],
|
||||
fileSize: (int)$data['file_size'],
|
||||
filename: $data['filename'],
|
||||
createdAt: new \DateTimeImmutable($data['created_at']),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user