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

OptimizeImages::process()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 54

Duplication

Lines 10
Ratio 18.52 %

Importance

Changes 0
Metric Value
dl 10
loc 54
rs 8.3814
c 0
b 0
f 0
cc 6
nc 11
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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