Completed
Push — master ( 9def54...8aa014 )
by Daniel
02:56
created

ConnectionManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 54
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getConnection() 0 16 4
A setDefault() 0 4 1
A getDefault() 0 4 1
A getContainer() 0 4 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
    private $default;
14
15
    public function __construct(array $factories)
16
    {
17
        $this->default = 'default';
18
        $this->factories = $factories;
19
    }
20
21
    public function getConnection($name = null)
22
    {
23
        $factory = $this->getContainer()->get($this->factories[$this->default]);
24
25
        if (!is_null($name) && array_key_exists($name, $this->factories)) {
26
            $factory = $this->getContainer()->get(
27
                $this->factories[$name]
28
            );
29
        }
30
31
        if (!$factory instanceof ConnectionFactory) {
32
            throw new NotFoundAMQPConnectionFactoryException(sprintf('%s: connection not found.', $name));
33
        }
34
35
        return $factory->createConnection();
36
    }
37
38
    /**
39
     * @param $connName
40
     */
41
    public function setDefault($connName)
42
    {
43
        $this->default = $connName;
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function getDefault()
50
    {
51
        return $this->default;
52
    }
53
54
    /**
55
     * @return \Symfony\Component\DependencyInjection\ContainerInterface
56
     */
57
    public function getContainer()
58
    {
59
        return $this->container;
60
    }
61
}
62