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
Pull Request — master (#57)
by Faton
01:28
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
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
    /**
65
     * Get logger.
66
     * @return LoggerInterface
67
     */
68
    public function getLogger()
69
    {
70
        return $this->logger;
71
    }
72
73
    public function optimize(string $pathToImage, string $pathToOutput = null)
74
    {
75
        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...
76
            copy($pathToImage, $pathToOutput);
77
78
            $pathToImage = $pathToOutput;
79
        }
80
81
        $image = new Image($pathToImage);
82
83
        $this->logger->info("Start optimizing {$pathToImage}");
84
85
        foreach ($this->optimizers as $optimizer) {
86
            $this->applyOptimizer($optimizer, $image);
87
        }
88
    }
89
90
    protected function applyOptimizer(Optimizer $optimizer, Image $image)
91
    {
92
        if (! $optimizer->canHandle($image)) {
93
            return;
94
        }
95
96
        $optimizerClass = get_class($optimizer);
97
98
        $this->logger->info("Using optimizer: `{$optimizerClass}`");
99
100
        $optimizer->setImagePath($image->path());
101
102
        $command = $optimizer->getCommand();
103
104
        $this->logger->info("Executing `{$command}`");
105
106
        $process = new Process($command);
107
108
        $process
109
            ->setTimeout($this->timeout)
110
            ->run();
111
112
        $this->logResult($process);
113
    }
114
115
    protected function logResult(Process $process)
116
    {
117
        if (! $process->isSuccessful()) {
118
            $this->logger->error("Process errored with `{$process->getErrorOutput()}`}");
119
120
            return;
121
        }
122
123
        $this->logger->info("Process successfully ended with output `{$process->getOutput()}`");
124
    }
125
}
126