Passed
Push — master ( c9415f...63dfa1 )
by Vladimir
02:04
created

FondBotServiceProvider::registerRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot;
6
7
use FondBot\Events\MessageReceived;
8
use FondBot\Channels\ChannelManager;
9
use Illuminate\Support\Facades\Route;
10
use Illuminate\Support\ServiceProvider;
11
use Illuminate\Contracts\Events\Dispatcher;
12
use FondBot\Conversation\ConversationManager;
13
use Illuminate\Cache\Repository as CacheRepository;
14
use FondBot\Foundation\Listeners\HandleConversation;
15
use FondBot\Foundation\Http\Middleware\InitializeKernel;
16
use FondBot\Contracts\Channels\Manager as ChannelManagerContract;
17
use FondBot\Contracts\Conversation\Manager as ConversationManagerContract;
18
19
class FondBotServiceProvider extends ServiceProvider
20
{
21
    /**
22
     * Bootstrap any package services.
23
     *
24
     * @return void
25
     */
26
    public function boot(): void
27
    {
28
        $this->registerConversationManager();
29
        $this->registerChannelManager();
30
        $this->registerEventListeners();
31
        $this->registerRoutes();
32
    }
33
34
    /**
35
     * Register conversation manager.
36
     */
37
    protected function registerConversationManager(): void
38
    {
39
        $this->app->singleton(ConversationManagerContract::class, function () {
40
            return new ConversationManager($this->app, $this->app[CacheRepository::class]);
41
        });
42
43
        $this->app->alias(ConversationManagerContract::class, ConversationManager::class);
44
    }
45
46
    /**
47
     * Register channel manager.
48
     */
49
    protected function registerChannelManager(): void
50
    {
51
        $this->app->singleton(ChannelManagerContract::class, function () {
52
            $channels = collect(config('fondbot.channels'))->mapWithKeys(function (array $parameters, string $name) {
53
                return [$name => $parameters];
54
            });
55
56
            return new ChannelManager($this->app, $channels);
57
        });
58
59
        $this->app->alias(ChannelManagerContract::class, ChannelManager::class);
60
    }
61
62
    /**
63
     * Register default event listeners.
64
     */
65
    protected function registerEventListeners(): void
66
    {
67
        /** @var Dispatcher $events */
68
        $events = $this->app['events'];
69
70
        $events->listen(MessageReceived::class, HandleConversation::class);
71
    }
72
73
    /**
74
     * Register routes.
75
     */
76
    protected function registerRoutes(): void
77
    {
78
        Route::middleware(InitializeKernel::class)
79
            ->namespace('FondBot\Foundation\Http\Controllers')
80
            ->as('fondbot.')
81
            ->prefix('fondbot')
82
            ->group(function () {
83
                $this->loadRoutesFrom(__DIR__.'/../routes/webhooks.php');
84
            });
85
    }
86
87
    /**
88
     * Register any application services.
89
     *
90
     * @return void
91
     */
92
    public function register(): void
93
    {
94
        if ($this->app->runningInConsole()) {
95
            $this->commands([
96
                Console\MakeIntentCommand::class,
97
                Console\MakeInteractionCommand::class,
98
                Console\MakeActivatorCommand::class,
99
                Console\ListDriversCommand::class,
100
                Console\InstallDriverCommand::class,
101
                Console\ListChannelsCommand::class,
102
                Console\ListIntentsCommand::class,
103
            ]);
104
105
            $this->publishes([
106
                __DIR__.'/../resources/stubs/FondBotServiceProvider.stub' => app_path('Providers/FondBotServiceProvider.php'),
107
            ], 'fondbot-provider');
108
109
            $this->publishes([
110
                __DIR__.'/../config/fondbot.php' => config_path('fondbot.php'),
111
            ], 'fondbot-config');
112
        }
113
    }
114
}
115