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 ( 2c6410...35a4b9 )
by Freek
01:16
created

ImageOptimizer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 0
loc 87
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getOptimizers() 0 4 1
A addOptimizer() 0 6 1
A setOptimizers() 0 10 2
A useLogger() 0 6 1
A optimize() 0 10 2
A applyOptimizer() 0 22 2
A logResult() 0 10 2
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 ImageOptimizer
10
{
11
    protected $optimizers = [];
12
13
    /** @var \Psr\Log\LoggerInterface */
14
    protected $logger;
15
16
    public function __construct()
17
    {
18
        $this->useLogger(new DummyLogger());
19
    }
20
21
    public function getOptimizers(): array
22
    {
23
        return $this->optimizers;
24
    }
25
26
    public function addOptimizer(Optimizer $optimizer)
27
    {
28
        $this->optimizers[] = $optimizer;
29
30
        return $this;
31
    }
32
33
    public function setOptimizers(array $optimizers)
34
    {
35
        $this->optimizers = [];
36
37
        foreach ($optimizers as $optimizer) {
38
            $this->addOptimizer($optimizer);
39
        }
40
41
        return $this;
42
    }
43
44
    public function useLogger(LoggerInterface $log)
45
    {
46
        $this->logger = $log;
47
48
        return $this;
49
    }
50
51
    public function optimize(string $pathToImage)
52
    {
53
        $image = new Image($pathToImage);
54
55
        $this->logger->info("Start optimizing {$pathToImage}");
56
57
        foreach($this->optimizers as $optimizer) {
58
            $this->applyOptimizer($optimizer, $image);
59
        }
60
    }
61
62
    protected function applyOptimizer(Optimizer $optimizer, Image $image)
63
    {
64
        if (! $optimizer->canHandle($image)) {
65
            return;
66
        }
67
68
        $optimizerClass = get_class($optimizer);
69
70
        $this->logger->info("Using optimizer: `{$optimizerClass}`");
71
72
        $optimizer->setImagePath($image->path());
73
74
        $command = $optimizer->getCommand();
75
76
        $this->logger->info("Executing `{$command}`");
77
78
        $process = new Process($command);
79
80
        $process->run();
81
82
        $this->logResult($process);
83
    }
84
85
    protected function logResult(Process $process)
86
    {
87
        if ($process->isSuccessful()) {
88
            $this->logger->info("Process successfully ended with output `{$process->getOutput()}`");
89
90
            return;
91
        }
92
93
        $this->logger->error("Process errored with `{$process->getErrorOutput()}`}");
94
    }
95
}
96