Passed
Push — main ( 1c51e6...7cee2b )
by Michiel
07:42
created

SassCompiler::executeCommand()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 21
ccs 0
cts 11
cp 0
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the LGPL. For more information please see
18
 * <http://phing.info>.
19
 */
20
21
declare(strict_types=1);
22
23
namespace Phing\Task\Ext\Sass;
24
25
use Exception;
26
use Phing\Exception\BuildException;
27
use Phing\Project;
28
29
class SassCompiler implements SassTaskCompiler
30
{
31
    /**
32
     * @var string
33
     */
34
    private $executable;
35
36
    /**
37
     * @var string
38
     */
39
    private $flags;
40
41 2
    public function __construct(string $executable, string $flags)
42
    {
43 2
        $this->executable = $executable;
44 2
        $this->flags = $flags;
45
    }
46
47
    /**
48
     * @throws BuildException
49
     */
50
    public function compile(string $inputFilePath, string $outputFilePath, bool $failOnError): void
51
    {
52
        list($return, $output) = $this->executeCommand($inputFilePath, $outputFilePath);
53
        if ($failOnError && $return !== 0) {
54
            throw new BuildException(
55
                "Sass exited with return code {$return} and message '{$output[0]}'"
56
            );
57
        }
58
    }
59
60
    /**
61
     * Executes the command and returns return code and output.
62
     *
63
     * @param string $inputFile Input file
64
     * @param string $outputFile Output file
65
     *
66
     * @access protected
67
     * @throws BuildException
68
     * @return array array(return code, array with output)
69
     */
70
    private function executeCommand($inputFile, $outputFile)
71
    {
72
        // Prevent over-writing existing file.
73
        if ($inputFile == $outputFile) {
74
            throw new BuildException('Input file and output file are the same!');
75
        }
76
77
        $output = [];
78
        $return = null;
79
80
        $fullCommand = $this->executable;
81
82
        if (strlen($this->flags) > 0) {
83
            $fullCommand .= " {$this->flags}";
84
        }
85
86
        $fullCommand .= " {$inputFile} {$outputFile}";
87
88
        exec($fullCommand, $output, $return);
89
90
        return [$return, $output];
91
    }
92
}
93