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

SymfonyContainerAdapter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 0
cbo 3
dl 0
loc 35
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 14 4
A has() 0 4 2
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