Completed
Pull Request — master (#12)
by Théo
03:12 queued 46s
created

DefinitionParser::parse()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 13

Duplication

Lines 10
Ratio 45.45 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 22
ccs 15
cts 15
cp 1
rs 8.9197
cc 4
eloc 13
nc 3
nop 4
crap 4
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\Util;
13
14
use Fidry\LaravelYaml\DependencyInjection\Builder\ContainerBuilder;
15
use Fidry\LaravelYaml\DependencyInjection\Definition\Alias;
16
use Fidry\LaravelYaml\Exception\FileLoader\InvalidArgumentException;
17
use Fidry\LaravelYaml\FileLoader\Parser\Resolver\ResolverInterface;
18
19
/**
20
 * @author Théo FIDRY <[email protected]>
21
 */
22
final class DefinitionParser
23
{
24
    /**
25
     * @var ServiceParser
26
     */
27
    private $serviceParser;
28
29 44
    public function __construct(ResolverInterface $serviceResolver)
30
    {
31 44
        $this->serviceParser = new ServiceParser($serviceResolver);
32 44
    }
33
34
    /**
35
     * Parses a service definition and register it to the container.
36
     *
37
     * @param ContainerBuilder $container
38
     * @param string           $id
39
     * @param array|string     $service
40
     * @param string           $fileName file name
41
     *
42
     * @throws InvalidArgumentException
43
     */
44 32
    public function parse(ContainerBuilder $container, $id, $service, $fileName)
45
    {
46 32
        if (is_string($service) && 0 === strpos($service, '@')) {
47 4
            $alias = new Alias($id, substr($service, 1));
48 4
            $container->addAlias($alias);
49
50 4
            return;
51
        }
52
53 30 View Code Duplication
        if (false === is_array($service)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54 2
            throw new InvalidArgumentException(
55 1
                sprintf(
56 2
                    'A service definition must be an array or a string starting with "@" but %s found for service "%s" in %s. Check your YAML syntax.',
57 1
                    gettype($service),
58 1
                    $id,
59
                    $fileName
60 1
                )
61 1
            );
62
        }
63
64 29
        $this->serviceParser->parse($container, $id, $service, $fileName);
65 13
    }
66
}
67