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

ScriptLoader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 43
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A loadScript() 0 14 3
A loadFileContents() 0 4 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