From 2aeea4cc0990b2f37b6e46e684f677941d6a3d32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Mon, 24 Oct 2022 10:26:16 +0200 Subject: [PATCH] Move business logic to Configuration entity. --- src/Configuration.php | 22 +++++++++++++++++++++- src/Convertor.php | 30 ++---------------------------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/src/Configuration.php b/src/Configuration.php index 8899b49..7fcb3a6 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -7,6 +7,14 @@ final class Configuration { + public const + FormatJpg = 'jpg', + FormatPng = 'png', + FormatGif = 'gif'; + + public const SupportedFormats = [self::FormatJpg, self::FormatPng, self::FormatGif]; + + public function __construct( public string $pdfPath, public string $savePath, @@ -17,12 +25,24 @@ public function __construct( public bool $bestfit = false ) { $this->format = strtolower($format); + if (in_array($this->format, self::SupportedFormats, true) === false) { + throw new \InvalidArgumentException( + sprintf( + 'Format "%s" is not supported. Did you mean "%s"?', + $this->format, + implode('", "', self::SupportedFormats), + ) + ); + } + if (is_file($pdfPath) === false) { + throw new ConvertorException(sprintf('File "%s" does not exist.', $pdfPath)); + } } public static function from( string $pdfPath, - ?string $savePath = null, + string $savePath, string $format = 'jpg', bool $trim = false, ): self { diff --git a/src/Convertor.php b/src/Convertor.php index 331f0de..b19e428 100644 --- a/src/Convertor.php +++ b/src/Convertor.php @@ -7,14 +7,6 @@ final class Convertor { - public const - FormatJpg = 'jpg', - FormatPng = 'png', - FormatGif = 'gif'; - - public const SupportedFormats = [self::FormatJpg, self::FormatPng, self::FormatGif]; - - /** @throws \Error */ public function __construct() { @@ -27,26 +19,8 @@ public function __construct() * * @throws ConvertorException */ - public static function convert( - string|Configuration $pdfPath, - ?string $savePath = null, - string $format = 'jpg', - bool $trim = false - ): void { - $configuration = is_string($pdfPath) - ? Configuration::from($pdfPath, $savePath, $format, $trim) - : $pdfPath; - - if (in_array($configuration->format, self::SupportedFormats, true) === false) { - throw new \InvalidArgumentException(sprintf( - 'Format "%s" is not supported. Did you mean "%s"?', - $configuration->format, - implode('", "', self::SupportedFormats), - )); - } - if (is_file($configuration->pdfPath) === false) { - throw new ConvertorException(sprintf('File "%s" does not exist.', $configuration->pdfPath)); - } + public static function convert(Configuration $configuration): void + { try { self::process($configuration); } catch (\ImagickException $e) {