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

ServiceParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 8
nc 1
nop 1
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\Service;
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 ServiceParser
23
{
24
    /**
25
     * @var AliasParser
26
     */
27
    private $aliasParser;
28
29
    /**
30
     * @var AutowiringTypesParser
31
     */
32
    private $autowiringTypesParser;
33
34
    /**
35
     * @var ClassParser
36
     */
37
    private $classParser;
38
39
    /**
40
     * @var DecorationParser
41
     */
42
    private $decorationParser;
43
44
    /**
45
     * @var FactoryParser
46
     */
47
    private $factoryParser;
48
49
    /**
50
     * @var ResolverInterface
51
     */
52
    private $serviceResolver;
53
54
    /**
55
     * @var TagsParser
56
     */
57
    private $tagsParser;
58
59
    public function __construct(ResolverInterface $serviceResolver)
60
    {
61
        $this->serviceResolver = $serviceResolver;
62
63
        $this->aliasParser = new AliasParser();
64
        $this->classParser = new ClassParser();
65
        $this->tagsParser = new TagsParser();
66
        $this->autowiringTypesParser = new AutowiringTypesParser();
67
        $this->factoryParser = new FactoryParser($serviceResolver);
68
        $this->decorationParser = new DecorationParser();
69
    }
70
71
    /**
72
     * Parses a service definition and register it to the container.
73
     *
74
     * @param ContainerBuilder $container
75
     * @param string           $id
76
     * @param array|string     $service
77
     * @param string           $fileName file name
78
     *
79
     * @throws InvalidArgumentException
80
     */
81
    public function parse(ContainerBuilder $container, $id, array $service, $fileName)
82
    {
83
        $alias = $this->aliasParser->parse($id, $service, $fileName);
84
        if (null !== $alias) {
85
            $container->addAlias($alias);
86
        }
87
88
        $class = $this->classParser->parse($id, $service, $fileName);
89
        $arguments = (isset($service['arguments']))
90
            ? $this->serviceResolver->resolve($service['arguments'])
91
            : []
92
        ;
93
        $tags = $this->tagsParser->parse($id, $service, $fileName);
94
        $autowiringTypes = $this->autowiringTypesParser->parse($id, $service, $fileName);
95
96
        $serviceDefinition = $this->createService($id, $class, $arguments, $autowiringTypes, $tags, $fileName);
97
98
        $container->addService($serviceDefinition);
99
    }
100
    
101
    private function createService($id, $class, array $arguments, array $autowiringTypes, array $tags, $fileName)
102
    {
103
        $serviceDefinition = new Service($id, $class, $arguments, $autowiringTypes, $tags);
104
105
        if (isset($service['factory'])) {
0 ignored issues
show
Bug introduced by
The variable $service does not exist. Did you mean $serviceDefinition?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
106
            $serviceDefinition = $this->factoryParser->parse($serviceDefinition, $service['factory'], $fileName);
107
        } elseif (isset($service['decorates'])) {
0 ignored issues
show
Bug introduced by
The variable $service does not exist. Did you mean $serviceDefinition?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
108
            $serviceDefinition = $this->decorationParser->parse($serviceDefinition, $service, $fileName);
109
        }
110
        
111
        return $serviceDefinition;
112
    }
113
}
114