Images::processFile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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