Test Failed
Pull Request — master (#5)
by
unknown
01:29
created

ScssAssetConverter::getScssPhpFormatterClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
namespace lucidtaz\yii2scssphp;
4
5
use Leafo\ScssPhp\Compiler;
6
use lucidtaz\yii2scssphp\storage\FsStorage;
7
use lucidtaz\yii2scssphp\storage\Storage;
8
use RuntimeException;
9
use Yii;
10
use yii\base\Component;
11
use yii\web\AssetConverterInterface;
12
13
class ScssAssetConverter extends Component implements AssetConverterInterface
14
{
15
    /**
16
     * @var Storage
17
     */
18
    public $storage;
19
20
    /**
21
     * @var boolean whether the source asset file should be converted even if
22
     * its result already exists. You may want to set this to be `true` during
23
     * the development stage to make sure the converted assets are always up-to-
24
     * date. Do not set this to true on production servers as it will
25
     * significantly degrade the performance.
26
     */
27
    public $forceConvert = false;
28
    
29
    public $formatter = null;
30
    
31 11
    protected $scssPhpFormatterMap =
32
    [
33 11
        'compact'       => '\Leafo\ScssPhp\Formatter\Compact',
34 11
        'compressed'    => '\Leafo\ScssPhp\Formatter\Compressed',
35 1
        'crunched'      => '\Leafo\ScssPhp\Formatter\Crunched',
36
        'debug'         => '\Leafo\ScssPhp\Formatter\Debug',
37 11
        'expanded'      => '\Leafo\ScssPhp\Formatter\Expanded',
38 11
        'nested'        => '\Leafo\ScssPhp\Formatter\Nested',
39
    ];
40
41
    protected $defaultFormatter = 'nested';
42
43
    protected $compiler;
44
45
    public function init()
46 10
    {
47
        parent::init();
48 10
        if (!isset($this->storage)) {
49 10
            $this->storage = new FsStorage;
50 2
        }
51
        $this->compiler = Yii::createObject(Compiler::class);
52 8
        
53
        $this->compiler->setFormatter($this->getScssPhpFormatterClass($this->formatter));
54 8
    }
55 8
56
    /**
57 8
     * Converts a given SCSS asset file into a CSS file.
58
     * @param string $asset the asset file path, relative to $basePath
59 8
     * @param string $basePath the directory the $asset is relative to.
60 1
     * @return string the converted asset file path, relative to $basePath.
61 1
     */
62
    public function convert($asset, $basePath)
63
    {
64 7
        $extension = $this->getExtension($asset);
65
        if ($extension !== 'scss') {
66 7
            return $asset;
67
        }
68
        $cssAsset = $this->replaceExtension($asset, 'css');
69 10
70
        $inFile = "$basePath/$asset";
71 10
        $outFile = "$basePath/$cssAsset";
72
        
73
        $this->compiler->setImportPaths(dirname($inFile));
74 8
75
        if (!$this->storage->exists($inFile)) {
76 8
            Yii::error("Input file $inFile not found.", __METHOD__);
77 8
            return $asset;
78
        }
79
80 7
        $this->convertAndSaveIfNeeded($inFile, $outFile);
81
82 7
        return $cssAsset;
83 6
    }
84 6
85
    protected function getExtension(string $filename): string
86 7
    {
87
        return pathinfo($filename, PATHINFO_EXTENSION);
88 7
    }
89
90 7
    protected function replaceExtension(string $filename, string $newExtension): string
91 3
    {
92
        $extensionlessFilename = pathinfo($filename, PATHINFO_FILENAME);
93 4
        return "$extensionlessFilename.$newExtension";
94 1
    }
95
96
    protected function convertAndSaveIfNeeded(string $inFile, string $outFile)
97 3
    {
98 1
        if ($this->shouldConvert($inFile, $outFile)) {
99 1
            $css = $this->compiler->compile($this->storage->get($inFile), $inFile);
100 1
            $this->storage->put($outFile, $css);
101
        }
102
    }
103
104 3
    protected function shouldConvert(string $inFile, string $outFile): bool
105
    {
106 3
        if (!$this->storage->exists($outFile)) {
107
            return true;
108
        }
109
        if ($this->forceConvert) {
110
            return true;
111
        }
112
        try {
113
            return $this->isOlder($outFile, $inFile);
114
        } catch (RuntimeException $e) {
115
            Yii::warning('Encountered RuntimeException message "' . $e->getMessage() . '", going to convert.', __METHOD__);
116
            return true;
117
        }
118
    }
119
120
    protected function isOlder(string $fileA, string $fileB): bool
121
    {
122
        return $this->storage->getMtime($fileA) < $this->storage->getMtime($fileB);
123
    }
124
    
125
    protected function getScssPhpFormatterClass($formatter): string
126
    {
127
        if (!isset($this->scssPhpFormatterMap[$formatter]))
128
        {
129
            //use default formatter if not found
130
            $formatter = $this->defaultFormatter;
131
132
            //if (YII_DEBUG) Yii::error("Formatter $formatter not found.", __METHOD__);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
133
        }
134
135
        return $this->scssPhpFormatterMap[$formatter];
136
    }
137
}
138