ConnectionManager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Cmobi\RabbitmqBundle\Connection;
4
5
use Cmobi\RabbitmqBundle\Connection\Exception\NotFoundAMQPConnectionFactoryException;
6
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
7
8
class ConnectionManager
9
{
10
    use ContainerAwareTrait;
11
12
    private $factories;
13
14
    public function __construct(array $factories)
15
    {
16
        $this->factories = $factories;
17
    }
18
19
    public function getConnection($name = null)
20
    {
21
        $factory = $this->getContainer()->get($this->factories['default']);
22
23
        if (!is_null($name) && array_key_exists($name, $this->factories)) {
24
            $factory = $this->getContainer()->get(
25
                $this->factories[$name]
26
            );
27
        }
28
29
        if (!$factory instanceof ConnectionFactory) {
30
            throw new NotFoundAMQPConnectionFactoryException(sprintf('%s: connection not found.', $name));
31
        }
32
33
        return $factory->createConnection();
34
    }
35
36
    /**
37
     * @return \Symfony\Component\DependencyInjection\ContainerInterface
38
     */
39
    public function getContainer()
40
    {
41
        return $this->container;
42
    }
43
}
44