Clean code
This commit is contained in:
@@ -26,14 +26,6 @@ class Media extends DB\SQL\Mapper
|
||||
$db->exec('CREATE INDEX IF NOT EXISTS idx_media_created_at ON media(created_at DESC)');
|
||||
}
|
||||
|
||||
public function all(): array
|
||||
{
|
||||
return array_map(
|
||||
fn(self $m): array => $this->decorate($m->cast()),
|
||||
$this->find(null, ['order' => 'created_at DESC, id DESC']) ?: []
|
||||
);
|
||||
}
|
||||
|
||||
public function paginateLibrary(int $page = 1, int $perPage = 24): array
|
||||
{
|
||||
$result = $this->paginate(
|
||||
@@ -58,11 +50,6 @@ class Media extends DB\SQL\Mapper
|
||||
);
|
||||
}
|
||||
|
||||
public function countAll(): int
|
||||
{
|
||||
return $this->count();
|
||||
}
|
||||
|
||||
public function findById(int $id): ?array
|
||||
{
|
||||
if ($id <= 0) {
|
||||
@@ -205,7 +192,7 @@ class Media extends DB\SQL\Mapper
|
||||
$image = match ($mime) {
|
||||
'image/jpeg' => @imagecreatefromjpeg($srcPath),
|
||||
'image/png' => @imagecreatefrompng($srcPath),
|
||||
'image/webp' => function_exists('imagecreatefromwebp') ? @imagecreatefromwebp($srcPath) : false,
|
||||
'image/webp' => @imagecreatefromwebp($srcPath),
|
||||
default => false,
|
||||
};
|
||||
|
||||
@@ -228,7 +215,7 @@ class Media extends DB\SQL\Mapper
|
||||
private static function writeImage(GdImage $image, string $target, string $format): void
|
||||
{
|
||||
if ($format === 'png') {
|
||||
if (function_exists('imagepalettetotruecolor') && !imageistruecolor($image)) {
|
||||
if (!imageistruecolor($image)) {
|
||||
imagepalettetotruecolor($image);
|
||||
}
|
||||
imagealphablending($image, false);
|
||||
@@ -272,7 +259,6 @@ class Media extends DB\SQL\Mapper
|
||||
'width' => (int) $row['width'],
|
||||
'height' => (int) $row['height'],
|
||||
'created_at' => (string) $row['created_at'],
|
||||
'created_at_label' => app_format_datetime_fr((string) $row['created_at']),
|
||||
'url' => app_media_url((string) $row['file_name']),
|
||||
'markdown' => '',
|
||||
];
|
||||
|
||||
@@ -133,9 +133,11 @@ class Post extends DB\SQL\Mapper
|
||||
public function delete(int $id): void
|
||||
{
|
||||
$this->load(['id = ?', $id]);
|
||||
if (!$this->dry()) {
|
||||
$this->erase();
|
||||
if ($this->dry()) {
|
||||
throw new RuntimeException('Article introuvable.');
|
||||
}
|
||||
|
||||
$this->erase();
|
||||
}
|
||||
|
||||
private function payload(array $input): array
|
||||
@@ -168,7 +170,7 @@ class Post extends DB\SQL\Mapper
|
||||
}
|
||||
}
|
||||
|
||||
$bodyHtml = MarkdownService::compile($bodyMarkdown, $media);
|
||||
$bodyHtml = MarkdownService::instance()->compile($bodyMarkdown, $media);
|
||||
|
||||
return [
|
||||
'title' => $title,
|
||||
@@ -193,10 +195,7 @@ class Post extends DB\SQL\Mapper
|
||||
'excerpt' => (string) $row['excerpt'],
|
||||
'cover_media_id' => (int) ($row['cover_media_id'] ?? 0),
|
||||
'created_at' => (string) $row['created_at'],
|
||||
'created_at_label' => app_format_datetime_fr((string) $row['created_at']),
|
||||
'updated_at' => (string) $row['updated_at'],
|
||||
'updated_at_label' => app_format_datetime_fr((string) $row['updated_at']),
|
||||
'has_updated_at' => (string) $row['updated_at'] !== (string) $row['created_at'],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user