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.

OptimizerChain::setOptimizers()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\ImageOptimizer;
4
5
use Psr\Log\LoggerInterface;
6
use Symfony\Component\Process\Process;
7
8
class OptimizerChain
9
{
10
    /* @var \Spatie\ImageOptimizer\Optimizer[] */
11
    protected $optimizers = [];
12
13
    /** @var \Psr\Log\LoggerInterface */
14
    protected $logger;
15
16
    /** @var int */
17
    protected $timeout = 60;
18
19
    public function __construct()
20
    {
21
        $this->useLogger(new DummyLogger());
22
    }
23
24
    public function getOptimizers(): array
25
    {
26
        return $this->optimizers;
27
    }
28
29
    public function addOptimizer(Optimizer $optimizer)
30
    {
31
        $this->optimizers[] = $optimizer;
32
33
        return $this;
34
    }
35
36
    public function setOptimizers(array $optimizers)
37
    {
38
        $this->optimizers = [];
39
40
        foreach ($optimizers as $optimizer) {
41
            $this->addOptimizer($optimizer);
42
        }
43
44
        return $this;
45
    }
46
47
    /*
48
     * Set the amount of seconds each separate optimizer may use.
49
     */
50
    public function setTimeout(int $timeoutInSeconds)
51
    {
52
        $this->timeout = $timeoutInSeconds;
53
54
        return $this;
55
    }
56
57
    public function useLogger(LoggerInterface $log)
58
    {
59
        $this->logger = $log;
60
61
        return $this;
62
    }
63
64
    public function optimize(string $pathToImage, string $pathToOutput = null)
65
    {
66
        if ($pathToOutput) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $pathToOutput of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
67
            copy($pathToImage, $pathToOutput);
68
69
            $pathToImage = $pathToOutput;
70
        }
71
72
        $image = new Image($pathToImage);
73
74
        $this->logger->info("Start optimizing {$pathToImage}");
75
76
        foreach ($this->optimizers as $optimizer) {
77
            $this->applyOptimizer($optimizer, $image);
78
        }
79
    }
80
81
    protected function applyOptimizer(Optimizer $optimizer, Image $image)
82
    {
83
        if (! $optimizer->canHandle($image)) {
84
            return;
85
        }
86
87
        $optimizerClass = get_class($optimizer);
88
89
        $this->logger->info("Using optimizer: `{$optimizerClass}`");
90
91
        $optimizer->setImagePath($image->path());
92
93
        $command = $optimizer->getCommand();
94
95
        $this->logger->info("Executing `{$command}`");
96
97
        $process = Process::fromShellCommandline($command);
98
99
        $process
100
            ->setTimeout($this->timeout)
101
            ->run();
102
103
        $this->logResult($process);
104
    }
105
106
    protected function logResult(Process $process)
107
    {
108
        if (! $process->isSuccessful()) {
109
            $this->logger->error("Process errored with `{$process->getErrorOutput()}`");
110
111
            return;
112
        }
113
114
        $this->logger->info("Process successfully ended with output `{$process->getOutput()}`");
115
    }
116
}
117