Skip to content

Commit

Permalink
Move business logic to Configuration entity.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Oct 24, 2022
1 parent 310434b commit 2aeea4c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
22 changes: 21 additions & 1 deletion src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
30 changes: 2 additions & 28 deletions src/Convertor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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) {
Expand Down

0 comments on commit 2aeea4c

Please sign in to comment.