Completed
Push — 1.0 ( d216e1...58fc60 )
by David
02:54
created

SymfonyContainerAdapter::__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
namespace TheCodingMachine\Interop\ServiceProviderBridgeBundle;
4
5
use TheCodingMachine\Interop\ServiceProviderBridgeBundle\Exception\ContainerException as BridgeContainerException;
6
use TheCodingMachine\Interop\ServiceProviderBridgeBundle\Exception\NotFoundException as BridgeNotFoundException;
7
use Interop\Container\ContainerInterface;
8
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
9
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException as SymfonyInvalidArgumentException;
10
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException as SymfonyNotFoundException;
11
12
/**
13
 * An adapter from a Symfony Container to the standardized ContainerInterface
14
 * Heavily adapter from Acclimate's SymfonyContainerAdapter
15
 */
16
class SymfonyContainerAdapter implements ContainerInterface
17
{
18
    /**
19
     * @var SymfonyContainerInterface A Symfony Container
20
     */
21
    private $container;
22
23
    /**
24
     * @param SymfonyContainerInterface $container A Symfony Container
25
     */
26
    public function __construct(SymfonyContainerInterface $container)
27
    {
28
        $this->container = $container;
29
    }
30
31
    public function get($id)
32
    {
33
        // First, let's test if there is a parameter (parameters and services are the same thing in container/interop)
34
        if ($this->container->hasParameter($id)) {
35
            return $this->container->getParameter($id);
36
        }
37
        try {
38
            return $this->container->get($id);
39
        } catch (SymfonyNotFoundException $prev) {
40
            throw BridgeNotFoundException::fromPrevious($id, $prev);
41
        } catch (\Exception $prev) {
42
            throw BridgeContainerException::fromPrevious($id, $prev);
43
        }
44
    }
45
46
    public function has($id)
47
    {
48
        return $this->container->has($id) || $this->container->hasParameter($id);
49
    }
50
}
51