Passed
Push — master ( af6a35...84cc34 )
by Eugene
29s
created

Processor   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 20
c 4
b 0
f 0
lcom 1
cbo 6
dl 0
loc 149
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A initCompilers() 0 7 1
A attachFiles() 0 15 4
A concatOutput() 0 13 3
A saveOutput() 0 14 3
B processFiles() 0 23 6
A getFormatterClass() 0 8 2
1
<?php
2
3
namespace EM\CssCompiler\Processor;
4
5
use Composer\IO\IOInterface;
6
use EM\CssCompiler\Container\File;
7
use EM\CssCompiler\Exception\CompilerException;
8
use Leafo\ScssPhp\Compiler as SASSCompiler;
9
use lessc as LESSCompiler;
10
use scss_compass as CompassCompiler;
11
12
class Processor
13
{
14
    const FORMATTER_COMPRESSED = 'compressed';
15
    const FORMATTER_CRUNCHED   = 'crunched';
16
    const FORMATTER_EXPANDED   = 'expanded';
17
    const FORMATTER_NESTED     = 'nested';
18
    const FORMATTER_COMPACT    = 'compact';
19
    const SUPPORTED_FORMATTERS = [
20
        self::FORMATTER_COMPRESSED,
21
        self::FORMATTER_CRUNCHED,
22
        self::FORMATTER_EXPANDED,
23
        self::FORMATTER_NESTED,
24
        self::FORMATTER_COMPACT
25
    ];
26
    /**
27
     * @var IOInterface
28
     */
29
    private $io;
30
    /**
31
     * @var File[]
32
     */
33
    private $files = [];
34
    /**
35
     * @var SASSCompiler
36
     */
37
    private $sass;
38
    /**
39
     * @var LESSCompiler
40
     */
41
    private $less;
42
    /**
43
     * @var CompassCompiler
44
     */
45
    private $compass;
46
47
    public function __construct(IOInterface $io)
48
    {
49
        $this->io = $io;
50
        $this->initCompilers();
51
    }
52
53
    protected function initCompilers()
54
    {
55
        $this->less = new LESSCompiler();
56
        $this->sass = new SASSCompiler();
57
        /** attaches compass functionality to the SASS compiler */
58
        $this->compass = new CompassCompiler($this->sass);
59
    }
60
61
    /**
62
     * @param string $inputPath
63
     * @param string $outputPath
64
     *
65
     * @throws \Exception
66
     */
67
    public function attachFiles($inputPath, $outputPath)
68
    {
69
        if (is_dir($inputPath)) {
70
            $files = scandir($inputPath);
71
            unset($files[0], $files[1]);
72
73
            foreach ($files as $file) {
74
                $this->attachFiles("$inputPath/$file", $outputPath);
75
            }
76
        } else if (is_file($inputPath)) {
77
            $this->files[] = new File($inputPath, $outputPath);
78
        } else {
79
            throw new \Exception("file doesn't exists");
80
        }
81
    }
82
83
    /**
84
     * @return string[]
85
     */
86
    public function concatOutput()
87
    {
88
        $outputMap = [];
89
        foreach ($this->files as $file) {
90
            if (!isset($outputMap[$file->getOutputPath()])) {
91
                $outputMap[$file->getOutputPath()] = $file->getParsedContent();
92
            } else {
93
                $outputMap[$file->getOutputPath()] .= $file->getParsedContent();
94
            }
95
        }
96
97
        return $outputMap;
98
    }
99
100
    /**
101
     * save output into file
102
     */
103
    public function saveOutput()
104
    {
105
        foreach ($this->concatOutput() as $path => $content) {
106
107
            $directory = dirname($path);
108
            if (!is_dir($dir = $directory)) {
109
                $this->io->write("<info>creating directory</info>: {$directory}");
110
                mkdir($directory, 0755, true);
111
            }
112
113
            $this->io->write("<info>save output into</info>: {$path}");
114
            file_put_contents($path, $content);
115
        }
116
    }
117
118
    /**
119
     * @param string $formatter
120
     *
121
     * @throws CompilerException
122
     */
123
    public function processFiles($formatter)
124
    {
125
        foreach ($this->files as $file) {
126
            $this->io->write("<info>processing</info>: {$file->getSourcePath()}");
127
            $file->setSourceContentFromSourcePath();
128
129
            switch ($file->getType()) {
130
                case File::TYPE_COMPASS:
131
                case File::TYPE_SCSS:
132
                case File::TYPE_SASS:
133
                    $this->sass->setFormatter($this->getFormatterClass($formatter));
134
                    $content = $this->sass->compile($file->getSourceContent());
135
                    break;
136
                case File::TYPE_LESS:
137
                    $content = $this->less->compile($file->getSourceContent());
138
                    break;
139
                default:
140
                    throw new CompilerException('unknown compiler');
141
            }
142
143
            $file->setParsedContent($content);
144
        }
145
    }
146
147
    /**
148
     * @param string $formatter
149
     *
150
     * @return string
151
     */
152
    protected function getFormatterClass($formatter)
153
    {
154
        if (!in_array($formatter, static::SUPPORTED_FORMATTERS)) {
155
            throw new \InvalidArgumentException('unknown formatter, available options are: ' . print_r(static::SUPPORTED_FORMATTERS, true));
156
        }
157
158
        return 'Leafo\\ScssPhp\\Formatter\\' . ucfirst($formatter);
159
    }
160
}
161