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

ConnectionManager::getDefault()   A

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 0
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