oc-resizer-plugin/compressors/Factory.php

53 lines
1.2 KiB
PHP

<?php
namespace Aweos\Resizer\Compressors;
use Aweos\Resizer\Lib\MediaPath;
class Factory
{
private string $default = DefaultCompressor::class;
public array $types = [
'image/jpeg' => JpgCompressor::class,
'image/png' => PngCompressor::class,
'application/pdf' => PdfCompressor::class,
];
public array $extensions = [
'jpg' => JpgCompressor::class,
'png' => PngCompressor::class,
'pdf' => PdfCompressor::class,
];
public function fromMedia(string $mediaPath): Compressor
{
return $this->resolve(new MediaPath($mediaPath));
}
public function resolve(MediaPath $path): Compressor
{
$compiler = is_null($path->type())
? $this->resolveExtension($path->extension())
: $this->resolveType($path->type());
if (is_null($compiler)) {
return new $this->default($path);
}
return new $compiler($path);
}
private function resolveType(string $type): ?string
{
return collect($this->types)->get($type);
}
private function resolveExtension(string $extension): ?string
{
return collect($this->extensions)->get($extension);
}
}