Completed
Push — feature-images-optim ( 953358 )
by Arnaud
05:17
created

ImagesOptim   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 7
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 7 7 3
A process() 0 25 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 ImagesOptim extends AbstractStep
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 View Code Duplication
    public function init($options)
23
    {
24
        if (!is_dir($this->builder->getConfig()->get('process.images.dir'))
25
        && false === $this->builder->getConfig()->get('process.images.enabled')) {
26
            $this->process = false;
27
        }
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function process()
34
    {
35
        call_user_func_array($this->builder->getMessageCb(), ['PROCESS', 'Images optimization']);
36
37
        $optimizerChain = OptimizerChainFactory::create();
38
39
        $data = Finder::create()
40
            ->files()
41
            ->in($this->builder->getConfig()->getStaticPath())
42
            ->name('/\.('.implode('|', $this->builder->getConfig()->get('process.images.ext')).')$/')
43
            ->sortByName(true);
44
45
        $count = 0;
46
        $max = count($data);
47
48
        /* @var $file \Symfony\Component\Finder\SplFileInfo */
49
        foreach ($data as $file) {
50
            $count++;
51
52
            $optimizerChain->optimize($file->getPathname());
53
54
            $message = sprintf('"%s" processed', $file->getPathname());
55
            call_user_func_array($this->builder->getMessageCb(), ['PROCESS_PROGRESS', $message, $count, $max]);
56
        }
57
    }
58
}
59