Completed
Pull Request — 0.4 (#35)
by jean
03:28
created

MessageProviderFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Darkilliant\MqProcessBundle\Message\Provider;
4
5
use PhpAmqpLib\Connection\AMQPStreamConnection;
6
use Psr\Container\ContainerInterface;
7
8
class MessageProviderFactory
9
{
10
    /** @var array */
11
    private $messageProvider = [];
12
13
    /** @var ContainerInterface */
14
    private $container;
15
16
    public function __construct(ContainerInterface $container)
17
    {
18
        $this->container = $container;
19
    }
20
21
    public function factory(string $name, array $config)
22
    {
23
        if (isset($this->messageProvider[$name])) {
24
            return $this->messageProvider[$name];
25
        }
26
27
        if ('amqp_lib' === $config['client']) {
28
            /** @var $connexion AMQPStreamConnection */
29
            $connexion = $this->container->get($config['connexion'] ?? 'darkilliant_mqprocess_connection');
30
31
            return $this->messageProvider[$name] = new AmqpMessageProvider(
32
                $connexion->channel(),
33
                $config['queue'],
34
                $config['exchange']
35
            );
36
        }
37
38
        return null;
39
    }
40
}
41