KernelBasedContainerAccessor::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the CrossContainerExtension package.
7
 *
8
 * (c) Kamil Kokot <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace FriendsOfBehat\CrossContainerExtension;
15
16
use Symfony\Component\DependencyInjection\Container;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
use Symfony\Component\HttpKernel\KernelInterface;
19
20
final class KernelBasedContainerAccessor implements ContainerAccessor
21
{
22
    /**
23
     * @var KernelInterface
24
     */
25
    private $kernel;
26
27
    /**
28
     * @param KernelInterface $kernel
29
     */
30
    public function __construct(KernelInterface $kernel)
31
    {
32
        $this->kernel = $kernel;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getService(string $id)
39
    {
40
        $container = $this->kernel->getContainer();
41
42
        if ($container->has('test.service_container')) {
43
            $container = $container->get('test.service_container');
44
        }
45
46
        if (!$container->has($id)) {
47
            throw new \DomainException('Could not get the services of kernel\'s container.');
48
        }
49
50
        return (new ContainerBasedContainerAccessor($container))->getService($id);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getParameters(): array
57
    {
58
        $container = $this->kernel->getContainer();
59
60
        if (!$container instanceof Container) {
61
            throw new \DomainException('Could not get the parameters of kernel\'s container.');
62
        }
63
64
        return (new ContainerBasedContainerAccessor($container))->getParameters();
65
    }
66
}
67