registerConversationManager()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 23
rs 9.8666
c 0
b 0
f 0
cc 4
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot;
6
7
use SplFileInfo;
8
use ReflectionClass;
9
use Illuminate\Support\Str;
10
use FondBot\Conversation\Intent;
11
use FondBot\Channels\ChannelManager;
12
use Symfony\Component\Finder\Finder;
13
use Illuminate\Support\Facades\Route;
14
use FondBot\Console\MakeIntentCommand;
15
use FondBot\Console\ListDriversCommand;
16
use FondBot\Console\ListIntentsCommand;
17
use Illuminate\Support\ServiceProvider;
18
use FondBot\Console\ListChannelsCommand;
19
use FondBot\Console\InstallDriverCommand;
20
use FondBot\Console\MakeActivatorCommand;
21
use Illuminate\Cache\Repository as Cache;
22
use FondBot\Console\MakeInteractionCommand;
23
use FondBot\Conversation\ConversationManager;
24
25
class FondBotServiceProvider extends ServiceProvider
26
{
27
    public function register(): void
28
    {
29
        $this->app->singleton(FondBot::class, function () {
30
            return new FondBot();
31
        });
32
33
        $this->registerChannelManager();
34
        $this->registerConversationManager();
35
    }
36
37
    public function boot(): void
38
    {
39
        if ($this->app->runningInConsole()) {
40
            $this->mergeConfigFrom(
41
                __DIR__.'/../config/fondbot.php',
42
                'fondbot'
43
            );
44
45
            $this->publishes([
46
                __DIR__.'/../config/fondbot.php' => config_path('fondbot.php'),
47
            ], 'fondbot-config');
48
49
            $this->commands([
50
                MakeIntentCommand::class,
51
                MakeInteractionCommand::class,
52
                MakeActivatorCommand::class,
53
                ListDriversCommand::class,
54
                InstallDriverCommand::class,
55
                ListChannelsCommand::class,
56
                ListIntentsCommand::class,
57
            ]);
58
        }
59
60
        Route::prefix('fondbot')
61
            ->middleware('fondbot.webhook')
62
            ->group(function () {
63
                Route::get('/', 'FondBot\Foundation\Controller@index');
64
                Route::get('/webhook/{channel}/{secret?}', 'FondBot\Foundation\Controller@webhook')->name('fondbot.webhook');
65
                Route::post('/webhook/{channel}/{secret?}', 'FondBot\Foundation\Controller@webhook')->name('fondbot.webhook');
66
            });
67
    }
68
69
    private function registerChannelManager(): void
70
    {
71
        $this->app->singleton(ChannelManager::class, function () {
72
            $manager = new ChannelManager($this->app);
73
74
            $manager->register(
75
                collect(config('fondbot.channels'))
76
                    ->mapWithKeys(function (array $parameters, string $name) {
77
                        return [$name => $parameters];
78
                    })
79
                    ->toArray()
80
            );
81
82
            return $manager;
83
        });
84
    }
85
86
    private function registerConversationManager(): void
87
    {
88
        $this->app->singleton(ConversationManager::class, function () {
89
            $manager = new ConversationManager($this->app, $this->app[Cache::class]);
90
91
            $namespace = $this->app->getNamespace();
92
93
            /** @var SplFileInfo[] $files */
94
            $files = (new Finder())->in(config('fondbot.intents_path', []))->files();
95
96
            foreach ($files as $file) {
97
                $file = $namespace.str_replace(
98
                        ['/', '.php'],
99
                        ['\\', ''],
100
                        Str::after($file->getPathname(), app_path().DIRECTORY_SEPARATOR)
101
                    );
102
103
                if (is_subclass_of($file, Intent::class) && !(new ReflectionClass($file))->isAbstract()) {
104
                    $manager->registerIntent($file);
105
                }
106
            }
107
108
            return $manager;
109
        });
110
    }
111
}
112