ChainOptimizer.php
1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
declare(strict_types=1);
namespace ImageOptimizer;
use ImageOptimizer\Exception\Exception;
use Psr\Log\LoggerInterface;
class ChainOptimizer implements Optimizer
{
/**
* @var Optimizer[]
*/
private $optimizers;
private $executeFirst;
private $logger;
public function __construct(array $optimizers, bool $executeFirst, LoggerInterface $logger)
{
$this->optimizers = $optimizers;
$this->executeFirst = $executeFirst;
$this->logger = $logger;
}
public function optimize(string $filepath): void
{
$exceptions = [];
foreach($this->optimizers as $optimizer) {
try {
$optimizer->optimize($filepath);
if($this->executeFirst) break;
} catch (Exception $e) {
$this->logger->error('Error during image optimization. See exception for more details.', [ 'exception' => $e ]);
$exceptions[] = $e;
}
}
if(count($exceptions) === count($this->optimizers)) {
throw new Exception(sprintf('All optimizers failed to optimize the file: %s', $filepath));
}
}
}