Completed
Push — master ( bc194a...d90c8b )
by Siad
17:01
created

ScssPhpCompiler   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 77.14%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 64
ccs 27
cts 35
cp 0.7714
rs 10
c 2
b 0
f 0
wmc 16

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 5
A checkInputFile() 0 13 4
B compile() 0 21 7
1
<?php
2
3
declare(strict_types=1);
4
5
use ScssPhp\ScssPhp\Compiler;
6
7
class ScssPhpCompiler implements SassTaskCompiler
8
{
9
10
    /**
11
     * @var Compiler
12
     */
13
    private $scssCompiler;
14
15 4
    public function __construct(string $style, string $encoding, bool $lineNumbers, string $loadPath)
16
    {
17 4
        $this->scssCompiler = new Compiler();
18 4
        if ($style) {
19 4
            $ucStyle = ucfirst(strtolower($style));
20 4
            $this->scssCompiler->setFormatter('ScssPhp\\ScssPhp\\Formatter\\' . $ucStyle);
21
        }
22 4
        if ($encoding) {
23 4
            $this->scssCompiler->setEncoding($encoding);
24
        }
25 4
        if ($lineNumbers) {
26
            $this->scssCompiler->setLineNumberStyle(1);
27
        }
28 4
        if ($loadPath !== '') {
29
            $this->scssCompiler->setImportPaths(explode(PATH_SEPARATOR, $loadPath));
30
        }
31 4
    }
32
33 4
    public function compile(string $inputFilePath, string $outputFilePath, bool $failOnError): void
34
    {
35 4
        if (!$this->checkInputFile($inputFilePath, $failOnError)) {
36 1
            return;
37
        }
38
39 2
        $input = file_get_contents($inputFilePath);
40
        try {
41 2
            $out = $this->scssCompiler->compile($input);
42 2
            if ($out !== '') {
43 2
                $success = file_put_contents($outputFilePath, $out);
44 2
                if (!$success && $failOnError) {
0 ignored issues
show
introduced by
The condition $failOnError is always false.
Loading history...
45
                    throw new BuildException(
46
                        "Cannot write to output file " . var_export($outputFilePath, true),
47
                        Project::MSG_INFO
48
                    );
49
                }
50
            }
51
        } catch (Exception $ex) {
52
            if ($failOnError) {
0 ignored issues
show
introduced by
The condition $failOnError is always false.
Loading history...
53
                throw new BuildException($ex->getMessage());
54
            }
55
        }
56 2
    }
57
58 4
    private function checkInputFile($inputFilePath, $failOnError): bool
59
    {
60 4
        if (file_exists($inputFilePath) && is_readable($inputFilePath)) {
61 2
            return true;
62
        }
63
64 2
        if ($failOnError) {
65 1
            throw new BuildException(
66 1
                "Cannot read from input file " . var_export($inputFilePath, true),
67 1
                Project::MSG_INFO
0 ignored issues
show
Bug introduced by
Project::MSG_INFO of type integer is incompatible with the type Exception|Location|null expected by parameter $p2 of BuildException::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
                /** @scrutinizer ignore-type */ Project::MSG_INFO
Loading history...
68
            );
69
        }
70 1
        return false;
71
    }
72
}
73