2021-09-09 19:18:41 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Aweos\Resizer\Compressors;
|
|
|
|
|
|
|
|
use Aweos\Resizer\Lib\MediaPath;
|
|
|
|
|
|
|
|
class Factory
|
|
|
|
{
|
|
|
|
|
|
|
|
private string $default = DefaultCompressor::class;
|
|
|
|
|
|
|
|
public array $types = [
|
|
|
|
'image/jpeg' => JpgCompressor::class,
|
2021-09-14 02:13:29 +02:00
|
|
|
'image/png' => PngCompressor::class,
|
2021-09-17 14:22:19 +02:00
|
|
|
'application/pdf' => PdfCompressor::class,
|
2021-09-09 19:18:41 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
public function fromMedia(string $mediaPath): Compressor
|
|
|
|
{
|
|
|
|
return $this->resolve(new MediaPath($mediaPath));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function resolve(MediaPath $path): Compressor
|
|
|
|
{
|
2021-09-16 01:11:00 +02:00
|
|
|
$compiler = $this->resolveType($path->type());
|
2021-09-09 19:18:41 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|