ServiceProvider   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 7
Bugs 0 Features 1
Metric Value
eloc 44
dl 0
loc 119
ccs 0
cts 69
cp 0
rs 10
c 7
b 0
f 1
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A registerContainer() 0 16 2
A register() 0 5 1
A publishConfig() 0 6 1
A registerConsumers() 0 18 3
A registerCommands() 0 8 1
A registerPublishers() 0 11 2
A boot() 0 4 1
1
<?php
2
namespace NeedleProject\LaravelRabbitMq\Providers;
3
4
use Illuminate\Foundation\Application;
5
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
6
use NeedleProject\LaravelRabbitMq\Builder\ContainerBuilder;
7
use NeedleProject\LaravelRabbitMq\Command\BaseConsumerCommand;
8
use NeedleProject\LaravelRabbitMq\Command\BasePublisherCommand;
9
use NeedleProject\LaravelRabbitMq\Command\DeleteAllCommand;
10
use NeedleProject\LaravelRabbitMq\Command\SetupCommand;
11
use NeedleProject\LaravelRabbitMq\Command\ListEntitiesCommand;
12
use NeedleProject\LaravelRabbitMq\ConfigHelper;
13
use NeedleProject\LaravelRabbitMq\ConsumerInterface;
14
use NeedleProject\LaravelRabbitMq\Container;
15
use NeedleProject\LaravelRabbitMq\Exception\LaravelRabbitMqException;
16
use NeedleProject\LaravelRabbitMq\PublisherInterface;
17
use Psr\Log\LoggerAwareInterface;
18
use Psr\Log\LoggerInterface;
19
20
/**
21
 * Class ServiceProvider
22
 *
23
 * @package NeedleProject\LaravelRabbitMq\Providers
24
 * @author  Adrian Tilita <[email protected]>
25
 */
26
class ServiceProvider extends LaravelServiceProvider
27
{
28
    /**
29
     * Indicates if loading of the provider is deferred.
30
     *
31
     * @var bool
32
     */
33
    protected $defer = false;
34
35
    /**
36
     * Register services.
37
     *
38
     * @return void
39
     */
40
    public function register()
41
    {
42
        $this->registerContainer();
43
        $this->registerPublishers();
44
        $this->registerConsumers();
45
    }
46
47
    /**
48
     * Perform post-registration booting of services.
49
     *
50
     * @return void
51
     */
52
    public function boot()
53
    {
54
        $this->publishConfig();
55
        $this->registerCommands();
56
    }
57
58
    /**
59
     * Publish Config
60
     */
61
    private function publishConfig()
62
    {
63
        $this->publishes([
0 ignored issues
show
Bug introduced by
The method publishes() does not exist on NeedleProject\LaravelRab...oviders\ServiceProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
        $this->/** @scrutinizer ignore-call */ 
64
               publishes([

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
            realpath(
65
                dirname(__FILE__)
66
            ) . '/../../config/laravel_rabbitmq.php' => config_path('laravel_rabbitmq.php'),
67
        ]);
68
    }
69
70
    /**
71
     * Create container and register binding
72
     */
73
    private function registerContainer()
74
    {
75
        $config = config('laravel_rabbitmq', []);
76
        if (!is_array($config)) {
77
            throw new \RuntimeException(
78
                "Invalid configuration provided for LaravelRabbitMQ!"
79
            );
80
        }
81
        $configHelper = new ConfigHelper();
82
        $config = $configHelper->addDefaults($config);
83
84
        $this->app->singleton(
0 ignored issues
show
Bug introduced by
The method singleton() does not exist on Tests\Laravel\App. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        $this->app->/** @scrutinizer ignore-call */ 
85
                    singleton(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
            Container::class,
86
            function () use ($config) {
87
                $container = new ContainerBuilder();
88
                return $container->createContainer($config);
89
            }
90
        );
91
    }
92
93
    /**
94
     * Register publisher bindings
95
     */
96
    private function registerPublishers()
97
    {
98
        // Get "tagged" like Publisher
99
        $this->app->singleton(PublisherInterface::class, function ($application, $arguments) {
100
            /** @var Container $container */
101
            $container = $application->make(Container::class);
102
            if (empty($arguments)) {
103
                throw new \RuntimeException("Cannot make Publisher. No publisher identifier provided!");
104
            }
105
            $aliasName = $arguments[0];
106
            return $container->getPublisher($aliasName);
107
        });
108
    }
109
110
    /**
111
     * Register consumer bindings
112
     */
113
    private function registerConsumers()
114
    {
115
        // Get "tagged" like Consumers
116
        $this->app->singleton(ConsumerInterface::class, function ($application, $arguments) {
117
            /** @var Container $container */
118
            $container = $application->make(Container::class);
119
            if (empty($arguments)) {
120
                throw new \RuntimeException("Cannot make Consumer. No consumer identifier provided!");
121
            }
122
            $aliasName = $arguments[0];
123
124
            if (!$container->hasConsumer($aliasName)) {
125
                throw new \RuntimeException("Cannot make Consumer.\nNo consumer with alias name {$aliasName} found!");
126
            }
127
            /** @var LoggerAwareInterface $consumer */
128
            $consumer = $container->getConsumer($aliasName);
129
            $consumer->setLogger($application->make(LoggerInterface::class));
130
            return $consumer;
131
        });
132
    }
133
134
    /**
135
     * Register commands
136
     */
137
    private function registerCommands()
138
    {
139
        $this->commands([
0 ignored issues
show
Bug introduced by
The method commands() does not exist on NeedleProject\LaravelRab...oviders\ServiceProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

139
        $this->/** @scrutinizer ignore-call */ 
140
               commands([

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
140
            SetupCommand::class,
141
            ListEntitiesCommand::class,
142
            BaseConsumerCommand::class,
143
            DeleteAllCommand::class,
144
            BasePublisherCommand::class
145
        ]);
146
    }
147
}
148