Completed
Pull Request — master (#12)
by Théo
11:36
created

DefinitionsParser::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
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\Parser\Yaml;
13
14
use Fidry\LaravelYaml\DependencyInjection\Builder\ContainerBuilder;
15
use Fidry\LaravelYaml\Exception\FileLoader\InvalidArgumentException;
16
use Fidry\LaravelYaml\FileLoader\Parser\Resolver\ResolverInterface;
17
use Fidry\LaravelYaml\FileLoader\Parser\Resolver\ServiceResolver;
18
use Fidry\LaravelYaml\FileLoader\Parser\Yaml\Util\DefinitionParser;
19
20
/**
21
 * @author Théo FIDRY <[email protected]>
22
 */
23
final class DefinitionsParser
24
{
25
    /**
26
     * @var DefinitionParser
27
     */
28
    private $definitionParser;
29
30
    public function __construct(ResolverInterface $serviceResolver = null)
31
    {
32
        $serviceResolver = (null === $serviceResolver) ? new ServiceResolver() : $serviceResolver;
33
        $this->definitionParser = new DefinitionParser($serviceResolver);
34
    }
35
36 69
    /**
37
     * Parses service definitions and register them to the container.
38 69
     *
39 69
     * @param ContainerBuilder $container
40
     * @param array            $content  YAML file content
41
     * @param string           $fileName file name
42
     *
43
     * @throws InvalidArgumentException
44
     */
45
    public function parse(ContainerBuilder $container, $content, $fileName)
46
    {
47
        if (!isset($content['services'])) {
48
            return;
49
        }
50 69
51
        if (!is_array($content['services'])) {
52 69
            throw new InvalidArgumentException(
53 12
                sprintf(
54
                    'The "services" key should contain an array in %s. Check your YAML syntax.',
55
                    $fileName
56 57
                )
57 6
            );
58 4
        }
59 6
60
        foreach ($content['services'] as $id => $service) {
61 4
            $this->definitionParser->parse($container, strtolower($id), $service, $fileName);
62 4
        }
63
    }
64
}
65