Completed
Pull Request — master (#69)
by Vladimir
02:50
created

SassEngine   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 94
c 0
b 0
f 0
wmc 17
lcom 1
cbo 3
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getConfigurationNamespace() 0 4 1
A getDefaultConfiguration() 0 7 1
A getFolder() 0 4 1
A getExtensions() 0 4 1
A parse() 0 4 1
A setOptions() 0 8 1
A configureImportPath() 0 4 1
A configureOutputStyle() 0 23 5
A configureSourceMap() 0 17 4
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\AssetEngine;
9
10
use __;
11
use allejo\stakx\Service;
12
use Leafo\ScssPhp\Compiler;
13
use Leafo\ScssPhp\Formatter\Compact;
14
use Leafo\ScssPhp\Formatter\Crunched;
15
use Leafo\ScssPhp\Formatter\Expanded;
16
use Leafo\ScssPhp\Formatter\Nested;
17
18
class SassEngine implements AssetEngineInterface
19
{
20
    private $compiler;
21
    private $options = [];
22
23
    public function __construct()
24
    {
25
        $this->compiler = new Compiler();
26
    }
27
28
    public function getConfigurationNamespace()
29
    {
30
        return 'scss';
31
    }
32
33
    public function getDefaultConfiguration()
34
    {
35
        return [
36
            'style' => 'compressed',
37
            'sourcemap' => false,
38
        ];
39
    }
40
41
    public function getFolder()
42
    {
43
        return '_sass';
44
    }
45
46
    public function getExtensions()
47
    {
48
        return ['scss'];
49
    }
50
51
    public function parse($content)
52
    {
53
        return $this->compiler->compile($content);
54
    }
55
56
    public function setOptions(array $options)
57
    {
58
        $this->options = $options;
59
60
        $this->configureImportPath();
61
        $this->configureOutputStyle();
62
        $this->configureSourceMap();
63
    }
64
65
    private function configureImportPath()
66
    {
67
        $this->compiler->setImportPaths(Service::getWorkingDirectory() . '/_sass/');
68
    }
69
70
    private function configureOutputStyle()
71
    {
72
        $style = __::get($this->options, 'style', 'compressed');
73
74
        switch ($style)
75
        {
76
            case 'nested':
77
                $this->compiler->setFormatter(Nested::class);
78
                break;
79
80
            case 'expanded':
81
                $this->compiler->setFormatter(Expanded::class);
82
                break;
83
84
            case 'compact':
85
                $this->compiler->setFormatter(Compact::class);
86
                break;
87
88
            case 'compressed':
89
            default:
90
                $this->compiler->setFormatter(Crunched::class);
91
        }
92
    }
93
94
    private function configureSourceMap()
95
    {
96
        $sourcemap = __::get($this->options, 'sourcemap');
97
98
        if ($sourcemap === false || $sourcemap === null) {
99
            $this->compiler->setSourceMap(Compiler::SOURCE_MAP_NONE);
100
        }
101
        elseif ($sourcemap === 'inline') {
102
            $this->compiler->setSourceMap(Compiler::SOURCE_MAP_INLINE);
103
        }
104
        else {
105
            $this->compiler->setSourceMap(Compiler::SOURCE_MAP_FILE);
106
            $this->compiler->setSourceMapOptions([
107
                'sourceMapWriteTo' => $sourcemap
108
            ]);
109
        }
110
    }
111
}
112