Passed
Push — analysis-RvmJ6Y ( 19d6c6 )
by Arnaud
10:57
created

AbstractPostProcess::init()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 14
rs 10
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 Cecil\Exception\Exception;
12
use Symfony\Component\Finder\Finder;
13
14
/**
15
 * Post Processing.
16
 */
17
abstract class AbstractPostProcess extends AbstractStep
18
{
19
    /** @var string File type (ie: 'css') */
20
    protected $type = 'type';
21
    /** @var mixed File processor */
22
    protected $processor;
23
    /** @var \Symfony\Component\Finder\SplFileInfo Input file */
24
    protected $inputFile;
25
    /** @var \SplFileInfo Output file */
26
    protected $outputFile;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function init($options)
32
    {
33
        if ($options['dry-run']) {
34
            $this->process = false;
35
36
            return;
37
        }
38
        if (false === $this->builder->getConfig()->get(sprintf('postprocess.%s.enabled', $this->type))) {
39
            $this->process = false;
40
41
            return;
42
        }
43
        if (true === $this->builder->getConfig()->get('postprocess.enabled')) {
44
            $this->process = true;
45
        }
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function process()
52
    {
53
        $this->setProcessor();
54
55
        call_user_func_array($this->builder->getMessageCb(), ['POSTPROCESS', sprintf('Post-processing %s', $this->type)]);
56
57
        $extensions = $this->builder->getConfig()->get(sprintf('postprocess.%s.ext', $this->type));
58
        if (empty($extensions)) {
59
            throw new Exception(sprintf('The config key "postprocess.%s.ext" is empty', $this->type));
60
        }
61
62
        $files = Finder::create()
63
            ->files()
64
            ->in($this->builder->getConfig()->getOutputPath())
65
            ->name('/\.('.implode('|', $extensions).')$/')
66
            ->notName('/\.min\.('.implode('|', $extensions).')$/')
67
            ->sortByName(true);
68
        $max = count($files);
69
70
        if ($max <= 0) {
71
            $message = 'No files';
72
            call_user_func_array($this->builder->getMessageCb(), ['POSTPROCESS_PROGRESS', $message]);
73
74
            return;
75
        }
76
77
        $count = 0;
78
        $postprocessed = 0;
79
80
        /** @var \Symfony\Component\Finder\SplFileInfo $file */
81
        foreach ($files as $file) {
82
            $count++;
83
84
            $sizeBefore = $file->getSize();
85
86
            $this->inputFile = $file;
87
88
            $this->processFile();
89
90
            $sizeAfter = $this->outputFile->getSize();
91
92
            $message = sprintf(
93
                '%s: %s Ko -> %s Ko',
94
                $file->getRelativePathname(),
95
                ceil($sizeBefore / 1000),
96
                ceil($sizeAfter / 1000)
97
            );
98
            call_user_func_array($this->builder->getMessageCb(), ['POSTPROCESS_PROGRESS', $message, $count, $max]);
99
            if ($sizeAfter < $sizeBefore) {
100
                $postprocessed++;
101
            }
102
        }
103
        if ($postprocessed == 0) {
104
            $message = 'Nothing to do';
105
            call_user_func_array($this->builder->getMessageCb(), ['POSTPROCESS_PROGRESS', $message]);
106
        }
107
    }
108
109
    /**
110
     * Set file processor object.
111
     *
112
     * @return void
113
     */
114
    abstract public function setProcessor();
115
116
    /**
117
     * Process a file.
118
     *
119
     * @param \Symfony\Component\Finder\SplFileInfo $file
120
     *
121
     * @return void
122
     */
123
    abstract public function processFile();
124
}
125