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

MessagePublisherFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 32
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A factory() 0 19 3
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