Test Setup Failed
Push — master ( 4ad40e...16d0f9 )
by
unknown
02:31
created

ServiceProvider::provides()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Foundation;
6
7
use FondBot\Channels\ChannelManager;
8
use Illuminate\Contracts\Cache\Store;
9
use FondBot\Conversation\IntentManager;
10
use FondBot\Conversation\FallbackIntent;
11
use FondBot\Conversation\SessionManager;
12
use Illuminate\Contracts\Config\Repository;
13
use FondBot\Conversation\ConversationManager;
14
15
class ServiceProvider extends \Illuminate\Support\ServiceProvider
16
{
17
    /**
18
     * Bootstrap the application services.
19
     *
20
     * @return void
21
     */
22
    public function boot(): void
23
    {
24
        if ($this->app->runningInConsole()) {
25
            $this->commands([
26
                // TODO
27
//                MakeIntent::class,
28
//                MakeInteraction::class,
29
//                ListDrivers::class,
30
//                InstallDriver::class,
31
//                ListChannels::class,
32
            ]);
33
        }
34
    }
35
36
    /**
37
     * Register the service provider.
38
     *
39
     * @return void
40
     */
41
    public function register(): void
42
    {
43
        $this->registerChannelManager();
44
        $this->registerConversationManager();
45
        $this->registerSessionManager();
46
        $this->registerIntentManager();
47
        $this->registerKernel();
48
    }
49
50
    private function registerChannelManager(): void
51
    {
52
        $this->app->singleton(ChannelManager::class, function () {
53
            /** @var array $channels */
54
            $channels = collect(array_get($this->config(), 'channels', []))
55
                ->mapWithKeys(function (array $parameters, string $name) {
56
                    return [$name => $parameters];
57
                })
58
                ->toArray();
59
60
            $manager = new ChannelManager;
61
            $manager->register($channels);
62
63
            return $manager;
64
        });
65
    }
66
67
    private function registerConversationManager(): void
68
    {
69
        $this->app->singleton(ConversationManager::class, function () {
70
            return new ConversationManager;
71
        });
72
    }
73
74
    private function registerSessionManager(): void
75
    {
76
        $this->app->singleton(SessionManager::class, function () {
77
            return new SessionManager(
78
                $this->app,
79
                $this->app->make(Store::class)
80
            );
81
        });
82
    }
83
84
    private function registerIntentManager(): void
85
    {
86
        $this->app->singleton(IntentManager::class, function () {
87
            $intents = array_get($this->config(), 'intents', []);
88
            $fallbackIntent = array_get($this->config(), 'fallback_intent', FallbackIntent::class);
89
90
            $manager = new IntentManager;
91
            $manager->register($intents, $fallbackIntent);
92
93
            return $manager;
94
        });
95
    }
96
97
    private function registerKernel(): void
98
    {
99
        $this->app->singleton(Kernel::class, function () {
100
            return Kernel::createInstance($this->app);
101
        });
102
    }
103
104
    private function config(): array
105
    {
106
        /** @var Repository $config */
107
        $config = $this->app[Repository::class];
108
109
        return $config->get('fondbot');
110
    }
111
112
    /**
113
     * Get the services provided by the provider.
114
     *
115
     * @return array
116
     */
117
    public function provides(): array
118
    {
119
        return [
120
            ChannelManager::class,
121
            ConversationManager::class,
122
            SessionManager::class,
123
            IntentManager::class,
124
            Kernel::class,
125
        ];
126
    }
127
}
128