ServiceProvider::publishConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Author: Joker
4
 * Date: 2020-05-08 13:57
5
 */
6
7
namespace JokerProject\LaravelAliyunAmqp\Providers;
8
9
use Illuminate\Foundation\Application;
10
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
11
use JokerProject\LaravelAliyunAmqp\Builder\ContainerBuilder;
12
use JokerProject\LaravelAliyunAmqp\Command\BaseConsumerCommand;
13
use JokerProject\LaravelAliyunAmqp\Command\BasePublisherCommand;
14
use JokerProject\LaravelAliyunAmqp\Command\DeleteAllCommand;
15
use JokerProject\LaravelAliyunAmqp\Command\SetupCommand;
16
use JokerProject\LaravelAliyunAmqp\Command\ListEntitiesCommand;
17
use JokerProject\LaravelAliyunAmqp\ConfigHelper;
18
use JokerProject\LaravelAliyunAmqp\ConsumerInterface;
19
use JokerProject\LaravelAliyunAmqp\Container;
20
use JokerProject\LaravelAliyunAmqp\Exception\LaravelRabbitMqException;
21
use JokerProject\LaravelAliyunAmqp\PublisherInterface;
22
use Psr\Log\LoggerAwareInterface;
23
use Psr\Log\LoggerInterface;
24
25
/**
26
 * Class ServiceProvider
27
 *
28
 * @package JokerProject\LaravelAliyunAmqp\Providers
29
 * @author  Adrian Tilita <[email protected]>
30
 */
31
class ServiceProvider extends LaravelServiceProvider
32
{
33
    /**
34
     * Indicates if loading of the provider is deferred.
35
     *
36
     * @var bool
37
     */
38
    protected $defer = false;
39
40
    /**
41
     * Perform post-registration booting of services.
42
     *
43
     * @return void
44
     * @throws LaravelRabbitMqException
45
     */
46
    public function boot()
47
    {
48
        $this->publishConfig();
49
        $this->registerContainer();
50
        $this->registerPublishers();
51
        $this->registerConsumers();
52
        $this->registerCommands();
53
    }
54
55
    /**
56
     * Publish Config
57
     */
58
    private function publishConfig()
59
    {
60
        $this->publishes([
61
            realpath(
62
                dirname(__FILE__)
63
            ) . '/../../config/laravel_rabbitmq.php' => config_path('laravel_rabbitmq.php'),
64
        ]);
65
    }
66
67
    /**
68
     * Create container and register binding
69
     */
70
    private function registerContainer()
71
    {
72
        $config = config('laravel_rabbitmq', []);
73
        if (!is_array($config)) {
74
            throw new \RuntimeException(
75
                "Invalid configuration provided for LaravelRabbitMQ!"
76
            );
77
        }
78
        $configHelper = new ConfigHelper();
79
        $config = $configHelper->addDefaults($config);
80
81
        $this->app->singleton(
82
            Container::class,
83
            function () use ($config) {
84
                $container = new ContainerBuilder();
85
                return $container->createContainer($config);
86
            }
87
        );
88
    }
89
90
    /**
91
     * Register publisher bindings
92
     */
93
    private function registerPublishers()
94
    {
95
        // Get "tagged" like Publisher
96
        $this->app->singleton(PublisherInterface::class, function (Application $application, $arguments) {
97
            /** @var Container $container */
98
            $container = $application->make(Container::class);
99
            if (empty($arguments)) {
100
                throw new \RuntimeException("Cannot make Publisher. No publisher identifier provided!");
101
            }
102
            $aliasName = $arguments[0];
103
            return $container->getPublisher($aliasName);
104
        });
105
    }
106
107
    /**
108
     * Register consumer bindings
109
     */
110
    private function registerConsumers()
111
    {
112
        // Get "tagged" like Consumers
113
        $this->app->singleton(ConsumerInterface::class, function (Application $application, $arguments) {
114
            /** @var Container $container */
115
            $container = $application->make(Container::class);
116
            if (empty($arguments)) {
117
                throw new \RuntimeException("Cannot make Consumer. No consumer identifier provided!");
118
            }
119
            $aliasName = $arguments[0];
120
121
            if (!$container->hasConsumer($aliasName)) {
122
                throw new \RuntimeException("Cannot make Consumer.\nNo consumer with alias name {$aliasName} found!");
123
            }
124
            /** @var LoggerAwareInterface $consumer */
125
            $consumer = $container->getConsumer($aliasName);
126
            $consumer->setLogger($application->make(LoggerInterface::class));
127
            return $consumer;
128
        });
129
    }
130
131
    /**
132
     * Register commands
133
     */
134
    private function registerCommands()
135
    {
136
        $this->commands([
137
            SetupCommand::class,
138
            ListEntitiesCommand::class,
139
            BaseConsumerCommand::class,
140
            DeleteAllCommand::class,
141
            BasePublisherCommand::class
142
        ]);
143
    }
144
}
145