Completed
Push — feature-images-optim ( 9032c8...3693ab )
by Arnaud
04:32 queued 02:21
created

OptimizeImages::init()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Step;
10
11
use Spatie\ImageOptimizer\OptimizerChainFactory;
12
use Symfony\Component\Finder\Finder;
13
14
/**
15
 * Images Optimization.
16
 */
17
class OptimizeImages extends AbstractStep
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function init($options)
23
    {
24
        if (false === $this->builder->getConfig()->get('optimize.images.enabled')) {
25
            $this->process = false;
26
27
            return;
28
        }
29
        if ($options['dry-run']) {
30
            $this->process = false;
31
32
            return;
33
        }
34
        if (is_dir($this->builder->getConfig()->getOutputPath())) {
35
            $this->process = true;
36
        }
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function process()
43
    {
44
        call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE', 'Optimizing images']);
45
46
        $optimizerChain = OptimizerChainFactory::create();
47
48
        $data = Finder::create()
49
            ->files()
50
            ->in($this->builder->getConfig()->getOutputPath())
51
            ->name('/\.('.implode('|', $this->builder->getConfig()->get('optimize.images.ext')).')$/')
52
            ->sortByName(true);
53
54
        $count = 0;
55
        $max = count($data);
56
        $optimized = 0;
57
58 View Code Duplication
        if ($max <= 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
            $message = 'No images';
60
            call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE_PROGRESS', $message]);
61
62
            return;
63
        }
64
65
        /* @var $file \Symfony\Component\Finder\SplFileInfo */
66
        foreach ($data as $file) {
67
            $count++;
68
69
            $sizeBefore = $file->getSize();
70
            $optimizerChain->optimize($file->getPathname());
71
            $sizeAfter = $file->getSize();
72
73
            $subpath = \Cecil\Util::getFS()->makePathRelative(
74
                $file->getPath(),
75
                $this->builder->getConfig()->getOutputPath()
76
            );
77
            $subpath = trim($subpath, './');
78
            $path = $subpath ? $subpath.'/'.$file->getFilename() : $file->getFilename();
79
80
            $message = sprintf(
81
                '%s: %s Ko -> %s Ko',
82
                $path,
83
                ceil($sizeBefore / 1000),
84
                ceil($sizeAfter / 1000)
85
            );
86
            if ($sizeAfter < $sizeBefore) {
87
                call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE_PROGRESS', $message, $count, $max]);
88
                $optimized++;
89
            }
90
        }
91 View Code Duplication
        if ($optimized == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
            $message = 'Nothing to do (not optimizers installed?)';
93
            call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE_PROGRESS', $message]);
94
        }
95
    }
96
}
97