Completed
Push — master ( 21d4aa...eb4259 )
by Thijs
02:23
created

ScssAssetConverter::isOlder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
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
    private $compiler;
30
31 10
    public function init()
32
    {
33 10
        parent::init();
34 10
        if (!isset($this->storage)) {
35 1
            $this->storage = new FsStorage;
36
        }
37 10
        $this->compiler = Yii::createObject(Compiler::class);
38 10
    }
39
40
    /**
41
     * Converts a given SCSS asset file into a CSS file.
42
     * @param string $asset the asset file path, relative to $basePath
43
     * @param string $basePath the directory the $asset is relative to.
44
     * @return string the converted asset file path, relative to $basePath.
45
     */
46 9
    public function convert($asset, $basePath)
47
    {
48 9
        $extension = $this->getExtension($asset);
49 9
        if ($extension !== 'scss') {
50 2
            return $asset;
51
        }
52 7
        $cssAsset = $this->replaceExtension($asset, 'css');
53
54 7
        $inFile = "$basePath/$asset";
55 7
        $outFile = "$basePath/$cssAsset";
56
57 7
        if (!$this->storage->exists($inFile)) {
58 1
            Yii::error("Input file $inFile not found.", __METHOD__);
59 1
            return $asset;
60
        }
61
62 6
        if ($this->shouldConvert($inFile, $outFile)) {
63 5
            $css = $this->compiler->compile($this->storage->get($inFile), $inFile);
64 5
            $this->storage->put($outFile, $css);
65
        }
66
67 6
        return $cssAsset;
68
    }
69
70 9
    private function getExtension(string $filename): string
71
    {
72 9
        return pathinfo($filename, PATHINFO_EXTENSION);
73
    }
74
75 7
    private function replaceExtension(string $filename, string $newExtension): string
76
    {
77 7
        $extensionlessFilename = pathinfo($filename, PATHINFO_FILENAME);
78 7
        return "$extensionlessFilename.$newExtension";
79
    }
80
81 6
    private function shouldConvert(string $inFile, string $outFile): bool
82
    {
83 6
        if (!$this->storage->exists($outFile)) {
84 2
            return true;
85
        }
86 4
        if ($this->forceConvert) {
87 1
            return true;
88
        }
89
        try {
90 3
            return $this->isOlder($outFile, $inFile);
91 1
        } catch (RuntimeException $e) {
92 1
            Yii::warning('Encountered RuntimeException message "' . $e->getMessage() . '", going to convert.', __METHOD__);
93 1
            return true;
94
        }
95
    }
96
97 3
    private function isOlder(string $fileA, string $fileB): bool
98
    {
99 3
        return $this->storage->getMtime($fileA) < $this->storage->getMtime($fileB);
100
    }
101
}
102