Completed
Push — master ( f73df0...286c6f )
by Markus
23s queued 12s
created

QueueRabbitMqServiceProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
eloc 30
c 1
b 0
f 1
dl 0
loc 66
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerQueueClient() 0 7 1
A createConnection() 0 38 1
A register() 0 3 1
1
<?php
2
3
namespace Jellyfish\QueueRabbitMq;
4
5
use Jellyfish\Config\ConfigInterface;
6
use Jellyfish\Queue\MessageMapperInterface;
7
use Jellyfish\Queue\QueueClientInterface;
8
use PhpAmqpLib\Connection\AbstractConnection;
9
use PhpAmqpLib\Connection\AMQPLazyConnection;
10
use Pimple\Container;
11
use Pimple\ServiceProviderInterface;
12
13
class QueueRabbitMqServiceProvider implements ServiceProviderInterface
14
{
15
    /**
16
     * @param \Pimple\Container $pimple
17
     *
18
     * @return void
19
     */
20
    public function register(Container $pimple): void
21
    {
22
        $this->registerQueueClient($pimple);
23
    }
24
25
    protected function registerQueueClient(Container $container): QueueRabbitMqServiceProvider
26
    {
27
        $self = $this;
28
        $container->offsetSet('queue_client', function (Container $container) use ($self) {
29
            return new QueueClient(
30
                $self->createConnection($container),
31
                $container->offsetGet('message_mapper')
32
            );
33
        });
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Jellyfish\QueueRabbitMq\...RabbitMqServiceProvider. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
34
    }
35
36
    /**
37
     * @param \Pimple\Container $container
38
     *
39
     * @return \PhpAmqpLib\Connection\AbstractConnection
40
     */
41
    protected function createConnection(Container $container): AbstractConnection
42
    {
43
        $config = $container->offsetGet('config');
44
45
        $rabbitMqHost = $config->get(
46
            QueueRabbitMqConstants::RABBIT_MQ_HOST,
47
            QueueRabbitMqConstants::DEFAULT_RABBIT_MQ_HOST
48
        );
49
50
        $rabbitMqPort = $config->get(
51
            QueueRabbitMqConstants::RABBIT_MQ_PORT,
52
            QueueRabbitMqConstants::DEFAULT_RABBIT_MQ_PORT
53
        );
54
55
        $rabbitMqUser = $config->get(
56
            QueueRabbitMqConstants::RABBIT_MQ_USER,
57
            QueueRabbitMqConstants::DEFAULT_RABBIT_MQ_USER
58
        );
59
60
        $rabbitMqPassword = $config->get(
61
            QueueRabbitMqConstants::RABBIT_MQ_PASSWORD,
62
            QueueRabbitMqConstants::DEFAULT_RABBIT_MQ_PASSWORD
63
        );
64
65
        $rabbitMqVhost = $config->get(
66
            QueueRabbitMqConstants::RABBIT_MQ_VHOST,
67
            QueueRabbitMqConstants::DEFAULT_RABBIT_MQ_VHOST
68
        );
69
70
        $connection = new AMQPLazyConnection(
71
            $rabbitMqHost,
72
            $rabbitMqPort,
73
            $rabbitMqUser,
74
            $rabbitMqPassword,
75
            $rabbitMqVhost
76
        );
77
78
        return $connection;
79
    }
80
}
81