Completed
Push — master ( 06f997...84d5d5 )
by Vladimir
03:43
created

FondBotServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 29
dl 0
loc 69
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A registerChannelManager() 0 11 1
A boot() 0 5 1
A registerEventListeners() 0 6 1
A registerConversationManager() 0 7 1
A register() 0 20 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot;
6
7
use Illuminate\Filesystem\Cache;
8
use FondBot\Events\MessageReceived;
9
use FondBot\Channels\ChannelManager;
10
use Illuminate\Support\ServiceProvider;
11
use Illuminate\Contracts\Events\Dispatcher;
12
use FondBot\Conversation\ConversationManager;
13
use FondBot\Foundation\Listeners\HandleConversation;
14
use FondBot\Contracts\Channels\Manager as ChannelManagerContract;
15
use FondBot\Contracts\Conversation\Manager as ConversationManagerContract;
16
17
class FondBotServiceProvider extends ServiceProvider
18
{
19
    /**
20
     * Bootstrap any package services.
21
     *
22
     * @return void
23
     */
24
    public function boot(): void
25
    {
26
        $this->registerConversationManager();
27
        $this->registerChannelManager();
28
        $this->registerEventListeners();
29
    }
30
31
    protected function registerConversationManager(): void
32
    {
33
        $this->app->singleton(ConversationManagerContract::class, function () {
34
            return new ConversationManager($this->app, $this->app[Cache::class]);
35
        });
36
37
        $this->app->alias(ConversationManagerContract::class, ConversationManager::class);
38
    }
39
40
    protected function registerChannelManager(): void
41
    {
42
        $this->app->singleton(ChannelManagerContract::class, function () {
43
            $channels = collect(config('fondbot.channels'))->mapWithKeys(function (array $parameters, string $name) {
44
                return [$name => $parameters];
45
            });
46
47
            return new ChannelManager($this->app, $channels);
48
        });
49
50
        $this->app->alias(ChannelManagerContract::class, ChannelManager::class);
51
    }
52
53
    protected function registerEventListeners(): void
54
    {
55
        /** @var Dispatcher $events */
56
        $events = $this->app['events'];
57
58
        $events->listen(MessageReceived::class, HandleConversation::class);
59
    }
60
61
    /**
62
     * Register any application services.
63
     *
64
     * @return void
65
     */
66
    public function register(): void
67
    {
68
        if ($this->app->runningInConsole()) {
69
            $this->commands([
70
                Console\MakeIntentCommand::class,
71
                Console\MakeInteractionCommand::class,
72
                Console\MakeActivatorCommand::class,
73
                Console\ListDriversCommand::class,
74
                Console\InstallDriverCommand::class,
75
                Console\ListChannelsCommand::class,
76
                Console\ListIntentsCommand::class,
77
            ]);
78
79
            $this->publishes([
80
                __DIR__.'/../resources/stubs/FondBotServiceProvider.stub' => app_path('Providers/FondBotServiceProvider.php'),
81
            ], 'fondbot-provider');
82
83
            $this->publishes([
84
                __DIR__.'/../config/fondbot.php' => config_path('fondbot.php'),
85
            ], 'fondbot-config');
86
        }
87
    }
88
}
89