RabbitMqProvider   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 1
c 2
b 0
f 0
lcom 1
cbo 7
dl 0
loc 52
ccs 0
cts 38
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 49 1
1
<?php
2
3
namespace Ccovey\LaravelRabbitMQ;
4
5
use Ccovey\RabbitMQ\Config\QueueConfig;
6
use Ccovey\RabbitMQ\Connection\Connection;
7
use Ccovey\RabbitMQ\Connection\ConnectionParameters;
8
use Ccovey\RabbitMQ\Consumer\Consumer;
9
use Ccovey\RabbitMQ\Producer\Producer;
10
use Ccovey\RabbitMQ\QueueDeclarer;
11
use Illuminate\Contracts\Foundation\Application;
12
use Illuminate\Queue\QueueManager;
13
use Illuminate\Support\ServiceProvider;
14
15
class RabbitMqProvider extends ServiceProvider
16
{
17
    public function register()
18
    {
19
        $rabbitMqConfig = $this->app['config']['queue.connections.rabbitmq'];
20
        $this->app->singleton(Connection::class, function(Application $app) use ($rabbitMqConfig) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
21
            $params = new ConnectionParameters(
22
                $rabbitMqConfig['host'],
23
                $rabbitMqConfig['port'] ?? ConnectionParameters::DEFAULT_PORT,
24
                $rabbitMqConfig['user'] ?? ConnectionParameters::DEFAULT_USER,
25
                $rabbitMqConfig['password'] ?? ConnectionParameters::DEFAULT_PASSWORD,
26
                $rabbitMqConfig['vhost'] ?? '/',
27
                $rabbitMqConfig['insist'] ?? false,
28
                $rabbitMqConfig['loginMethod'] ?? ConnectionParameters::LOGIN_METHOD,
29
                $rabbitMqConfig['loginResponse'] ?? null,
30
                $rabbitMqConfig['locale'] ?? ConnectionParameters::LOCALE,
31
                $rabbitMqConfig['connectionTimeout'] ?? ConnectionParameters::CONNECTION_TIMEOUT,
32
                $rabbitMqConfig['readWriteTimeout'] ?? ConnectionParameters::READ_WRITE_TIMEOUT,
33
                $rabbitMqConfig['context'] ?? null,
34
                $rabbitMqConfig['keepalive'] ?? false,
35
                $rabbitMqConfig['heartbeat'] ?? 0
36
            );
37
            return new Connection($params);
38
        });
39
40
        $this->app->singleton(QueueConfig::class, function (Application $app) use ($rabbitMqConfig) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
            return new QueueConfig($rabbitMqConfig['queue'] ?? []);
42
        });
43
44
        $this->app->singleton(QueueDeclarer::class, function (Application $app) use ($rabbitMqConfig) {
45
            return new QueueDeclarer($app[Connection::class], $app[QueueConfig::class]);
46
        });
47
48
        $this->app->singleton(Consumer::class, function(Application $app) {
49
            return new Consumer($app[Connection::class], $app[QueueDeclarer::class]);
50
        });
51
52
        $this->app->singleton(Producer::class, function(Application $app) {
53
            return new Producer($app[Connection::class], $app[QueueDeclarer::class]);
54
        });
55
56
        $this->app->booted(function () use ($rabbitMqConfig) {
57
            $queueManager = $this->app[QueueManager::class];
58
            $queueManager->addConnector('rabbitmq', function() use ($rabbitMqConfig) {
59
                return new RabbitMQConnector(
60
                    $this->app[Consumer::class],
61
                    $this->app[Producer::class],
62
                    $rabbitMqConfig);
63
            });
64
        });
65
    }
66
}
67