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

ImagesOptim::init()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 10
c 0
b 0
f 0
cc 3
nc 2
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 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