YamlFileLoader::__construct()   B
last analyzed

Complexity

Conditions 6
Paths 32

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 8.8571
cc 6
eloc 16
nc 32
nop 7
crap 6
1
<?php
2
3
/*
4
 * This file is part of the LaravelYaml package.
5
 *
6
 * (c) Théo FIDRY <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Fidry\LaravelYaml\FileLoader\Yaml;
13
14
use Fidry\LaravelYaml\DependencyInjection\Builder\ContainerBuilder;
15
use Fidry\LaravelYaml\Exception\FileLoader\Exception;
16
use Fidry\LaravelYaml\Exception\FileLoader\InvalidArgumentException;
17
use Fidry\LaravelYaml\FileLoader\FileLoaderInterface;
18
use Fidry\LaravelYaml\FileLoader\Parser\ParserInterface;
19
use Fidry\LaravelYaml\FileLoader\Parser\Yaml\DefinitionsParser;
20
use Fidry\LaravelYaml\FileLoader\Parser\Yaml\ImportsParser;
21
use Fidry\LaravelYaml\FileLoader\Parser\Yaml\ParametersParser;
22
use Symfony\Component\Config\FileLocatorInterface;
23
use Symfony\Component\Yaml\Parser as YamlParser;
24
25
/**
26
 * This loader is able to load YAML files. Parsed values are interpreted and added to the {@see ContainerBuilder} to be
27
 * loaded to the Application later on.
28
 *
29
 * @author Théo FIDRY <[email protected]>
30
 */
31
final class YamlFileLoader implements FileLoaderInterface
32
{
33
    /**
34
     * @var ContainerBuilder
35
     */
36
    private $container;
37
38
    /**
39
     * @var ParserInterface
40
     */
41
    private $definitionsParser;
42
43
    /**
44
     * @var FileLocatorInterface
45
     */
46
    private $fileLocator;
47
48
    /**
49
     * @var ParserInterface
50
     */
51
    private $importsParser;
52
53
    /**
54
     * @var ParserInterface
55
     */
56
    private $parametersParser;
57
58
    /**
59
     * @var YamlSingleFileLoader
60
     */
61
    private $singleFileLoader;
62
63 10
    public function __construct(
64
        ContainerBuilder $container,
65
        FileLocatorInterface $fileLocator,
66
        ParserInterface $definitionsParser = null,
67
        ParserInterface $parametersParser = null,
68
        ParserInterface $importsParser = null,
69
        YamlParser $yamlParser = null,
70
        YamlValidator $yamlValidator = null
71
    ) {
72 10
        $this->container = $container;
73 10
        $this->fileLocator = $fileLocator;
74
75 10
        $this->definitionsParser = (null === $definitionsParser) ? new DefinitionsParser() : $definitionsParser;
76 10
        $this->parametersParser = (null === $parametersParser) ? new ParametersParser() : $parametersParser;
77 10
        $this->importsParser = (null === $importsParser) ? new ImportsParser() : $importsParser;
78
79 10
        $yamlParser = (null === $yamlParser) ? new YamlParser() : $yamlParser;
80 10
        $yamlValidator = (null === $yamlValidator) ? new YamlValidator() : $yamlValidator;
81 10
        $this->singleFileLoader = new YamlSingleFileLoader($yamlParser, $yamlValidator);
82 10
    }
83
84
    /**
85
     * {@inheritdoc}
86
     *
87
     * @param string $resource file name
88
     *
89
     * @example
90
     *  ::load('services.yml')
91
     *
92
     * @throws InvalidArgumentException
93
     * @throws Exception
94
     * @return $this
95
     */
96 8
    public function load($resource)
97
    {
98
        /* @var string|null $path */
99 8
        $path = $this->fileLocator->locate($resource, null, true);
100 8
        $content = $this->singleFileLoader->loadFile($path);
101
102 4
        $imports = $this->importsParser->parse($this->container, $content, $resource);
103 4
        foreach ($imports as $importedResource) {
104 2
            $this->load($importedResource);
105 2
        }
106 4
        $this->parametersParser->parse($this->container, $content, $resource);
107 4
        $this->definitionsParser->parse($this->container, $content, $resource);
108
109 4
        return $this;
110
    }
111
}
112