Completed
Push — master ( 01e9a3...b0156c )
by Kamil
18:54
created

ContainerConfiguration::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Behat\MultiContainerExtension;
13
14
use Symfony\Component\DependencyInjection\Definition;
15
use Symfony\Component\DependencyInjection\Reference;
16
17
/**
18
 * @author Kamil Kokot <[email protected]>
19
 */
20
final class ContainerConfiguration
21
{
22
    /**
23
     * @var string
24
     */
25
    private $defaultContainerName;
26
27
    /**
28
     * @var array
29
     */
30
    private $containers = [];
31
32
    /**
33
     * @param string $defaultContainerName
34
     */
35
    public function __construct($defaultContainerName)
36
    {
37
        $this->defaultContainerName = $defaultContainerName;
38
    }
39
40
    /**
41
     * @param string $containerName
42
     * @param string $containerId
43
     */
44
    public function addContainer($containerName, $containerId)
45
    {
46
        $this->containers[$containerName] = $containerId;
47
    }
48
49
    /**
50
     * @param string|null $containerName
51
     *
52
     * @return bool
53
     */
54
    public function isDefault($containerName = null)
55
    {
56
        return null === $containerName || $containerName === $this->defaultContainerName;
57
    }
58
59
    /**
60
     * @param string $containerName
61
     *
62
     * @return bool
63
     */
64
    public function isDefined($containerName)
65
    {
66
        return $this->isDefault($containerName) || isset($this->containers[$containerName]);
67
    }
68
69
    /**
70
     * @param string $serviceId
71
     * @param string $containerName
72
     *
73
     * @return Definition|Reference
74
     */
75
    public function createReferenceFor($serviceId, $containerName = null)
76
    {
77
        if ($this->isDefault($containerName)) {
78
            return new Reference($serviceId);
79
        }
80
81
        if (!$this->isDefined($containerName)) {
82
            throw new \InvalidArgumentException(sprintf(
83
                'Could not find container named "%s"',
84
                $containerName
85
            ));
86
        }
87
88
        return (new Definition(null, [$serviceId]))->setFactory([
89
            new Reference($this->containers[$containerName]),
90
            'get',
91
        ]);
92
    }
93
}
94