Passed
Pull Request — 0.4 (#35)
by jean
03:13
created

MessagePublisherFactory::factory()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

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