Completed
Pull Request — master (#86)
by Jan Philipp
01:53
created

ScriptLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
4
namespace Shopware\Psh\ScriptRuntime\ScriptLoader;
5
6
use Shopware\Psh\Listing\Script;
7
use Shopware\Psh\ScriptRuntime\Command;
8
9
/**
10
 * Load scripts and parse it into commands
11
 */
12
class ScriptLoader
13
{
14
    /**
15
     * @var ScriptParser[]
16
     */
17
    private $parsers;
18
19
    /**
20
     * @param ScriptParser ...$parsers
21
     */
22
    public function __construct(ScriptParser ... $parsers)
23
    {
24
        $this->parsers = $parsers;
25
    }
26
27
    /**
28
     * @param Script $script
29
     * @return Command[]
30
     */
31
    public function loadScript(Script $script): array
32
    {
33
        $content = $this->loadFileContents($script->getPath());
34
35
        foreach ($this->parsers as $parser) {
36
            try {
37
                return $parser->parseContent($content, $script, $this);
38
            } catch (ScriptNotSupportedByParser $e) {
39
                //nth
40
            }
41
        }
42
43
        throw new ScriptNotSupportedByParser('Can not find a parser able to parse ' . $script->getPath());
44
    }
45
46
    /**
47
     * @param string $file
48
     * @return string
49
     */
50
    protected function loadFileContents(string $file): string
51
    {
52
        return file_get_contents($file);
53
    }
54
}
55