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

MessagePublisherFactory::factory()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 2
dl 0
loc 19
ccs 0
cts 15
cp 0
crap 12
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Darkilliant\MqProcessBundle\Message\Publisher;
4
5
use PhpAmqpLib\Connection\AMQPStreamConnection;
6
use Psr\Container\ContainerInterface;
7
8
class MessagePublisherFactory
9
{
10
    /** @var array */
11
    private $messagePublisher = [];
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->messagePublisher[$name])) {
24
            return $this->messagePublisher[$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->messagePublisher[$name] = new AmqpMessagePublisher(
32
                $connexion->channel(),
33
                $config['queue'],
34
                $config['exchange'],
35
                $config['persistant']
36
            );
37
        }
38
39
        return null;
40
    }
41
}
42