GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 73bff3...74d293 )
by Freek
01:19
created

OptimizerChain::useLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Spatie\ImageOptimizer;
4
5
use Psr\Log\LoggerInterface;
6
use Symfony\Component\Process\Process;
7
use Spatie\ImageOptimizer\Optimizers\Optimizer;
8
9
class OptimizerChain
10
{
11
    protected $optimizers = [];
12
13
    /** @var \Psr\Log\LoggerInterface */
14
    protected $logger;
15
16
    protected $timeout = 60;
17
18
    public function __construct()
19
    {
20
        $this->useLogger(new DummyLogger());
21
    }
22
23
    public function getOptimizers(): array
24
    {
25
        return $this->optimizers;
26
    }
27
28
    public function addOptimizer(Optimizer $optimizer)
29
    {
30
        $this->optimizers[] = $optimizer;
31
32
        return $this;
33
    }
34
35
    public function setOptimizers(array $optimizers)
36
    {
37
        $this->optimizers = [];
38
39
        foreach ($optimizers as $optimizer) {
40
            $this->addOptimizer($optimizer);
41
        }
42
43
        return $this;
44
    }
45
46
    /*
47
     * Set the amount of seconds each separte optimizer may use
48
     */
49
    public function setTimeout(int $timeoutInSeconds)
50
    {
51
        $this->timeout = $timeoutInSeconds;
52
    }
53
54
    public function useLogger(LoggerInterface $log)
55
    {
56
        $this->logger = $log;
57
58
        return $this;
59
    }
60
61
    public function optimize(string $pathToImage)
62
    {
63
        $image = new Image($pathToImage);
64
65
        $this->logger->info("Start optimizing {$pathToImage}");
66
67
        foreach ($this->optimizers as $optimizer) {
68
            $this->applyOptimizer($optimizer, $image);
69
        }
70
    }
71
72
    protected function applyOptimizer(Optimizer $optimizer, Image $image)
73
    {
74
        if (! $optimizer->canHandle($image)) {
75
            return;
76
        }
77
78
        $optimizerClass = get_class($optimizer);
79
80
        $this->logger->info("Using optimizer: `{$optimizerClass}`");
81
82
        $optimizer->setImagePath($image->path());
83
84
        $command = $optimizer->getCommand();
85
86
        $this->logger->info("Executing `{$command}`");
87
88
        $process = new Process($command);
89
90
        $process
91
            ->setTimeout($this->timeout)
92
            ->run();
93
94
        $this->logResult($process);
95
    }
96
97
    protected function logResult(Process $process)
98
    {
99
        if ($process->isSuccessful()) {
100
            $this->logger->info("Process successfully ended with output `{$process->getOutput()}`");
101
102
            return;
103
        }
104
105
        $this->logger->error("Process errored with `{$process->getErrorOutput()}`}");
106
    }
107
}
108