Passed
Pull Request — master (#1676)
by Arnaud
09:49 queued 04:14
created

Images   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 55
ccs 14
cts 16
cp 0.875
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setProcessor() 0 3 1
A getName() 0 3 1
A init() 0 4 1
A decode() 0 3 1
A processFile() 0 9 2
A encode() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Step\Optimize;
15
16
use Cecil\Assets\Image\Optimizer;
17
18
/**
19
 * Optimize image files.
20
 */
21
class Images extends AbstractOptimize
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 1
    public function getName(): string
27
    {
28 1
        return 'Optimizing images';
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 1
    public function init(array $options): void
35
    {
36 1
        $this->type = 'images';
37 1
        parent::init($options);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 1
    public function setProcessor(): void
44
    {
45 1
        $this->processor = Optimizer::create($this->config->get('assets.images.quality') ?? 75);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 1
    public function processFile(\Symfony\Component\Finder\SplFileInfo $file): string
52
    {
53
        try {
54 1
            $this->processor->optimize($file->getPathname());
55
        } catch (\Exception $e) {
56
            $this->builder->getLogger()->error(sprintf('Can\'t optimize image "%s": "%s"', $file->getPathname(), $e->getMessage()));
57
        }
58
59 1
        return $file->getContents();
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 1
    public function encode(string $content = null): ?string
66
    {
67 1
        return base64_encode((string) $content);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 1
    public function decode(string $content = null): ?string
74
    {
75 1
        return base64_decode((string) $content);
76
    }
77
}
78