ParametersParser::parse()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 5
Ratio 31.25 %

Code Coverage

Tests 11
CRAP Score 4

Importance

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