Completed
Push — master ( 8f75b3...70c410 )
by Théo
10s
created

AliasParser::parse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 13
cts 13
cp 1
rs 9.4285
cc 3
eloc 12
nc 3
nop 3
crap 3
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\Definition\Alias;
15
use Fidry\LaravelYaml\Exception\FileLoader\InvalidArgumentException;
16
17
/**
18
 * @author Théo FIDRY <[email protected]>
19
 */
20
final class AliasParser
21
{
22
    /**
23
     * Parses a service definition alias and register it to the container.
24
     *
25
     * @param string           $id
26
     * @param array|string     $service
27
     * @param string           $fileName file name
28
     *
29
     * @throws InvalidArgumentException
30
     *
31
     * @return Alias|null
32
     */
33 28
    public function parse($id, array $service, $fileName)
34
    {
35 28
        if (false === isset($service['alias'])) {
36 22
            return null;
37
        }
38
39 8
        $aliasName = $service['alias'];
40 8
        if (false === is_string($aliasName)) {
41 2
            throw new InvalidArgumentException(
42 1
                sprintf(
43 2
                    'Parameter "alias" must be a plain name for service "%s", but found "%s" instead in %s. Check your YAML syntax.',
44 1
                    $id,
45 1
                    gettype($aliasName),
46
                    $fileName
47 1
                )
48 1
            );
49
        }
50
51 6
        return new Alias($aliasName, $id);
52
    }
53
}
54