Completed
Push — master ( f8221d...6a1ba8 )
by Vladimir
09:15
created

ServiceProvider::provides()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 11
ccs 0
cts 2
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\Console\MakeIntent;
8
use FondBot\Console\ListDrivers;
9
use FondBot\Console\ListChannels;
10
use FondBot\Console\InstallDriver;
11
use FondBot\Drivers\DriverManager;
12
use FondBot\Channels\ChannelManager;
13
use FondBot\Console\MakeInteraction;
14
use Illuminate\Contracts\Cache\Store;
15
use FondBot\Conversation\IntentManager;
16
use FondBot\Conversation\FallbackIntent;
17
use FondBot\Conversation\SessionManager;
18
use Illuminate\Contracts\Config\Repository;
19
use FondBot\Conversation\ConversationManager;
20
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
21
22
class ServiceProvider extends BaseServiceProvider
23
{
24
    /**
25
     * Bootstrap the application services.
26
     *
27
     * @return void
28
     */
29
    public function boot(): void
30
    {
31
        if ($this->app->runningInConsole()) {
0 ignored issues
show
Bug introduced by
The method runningInConsole() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
            $this->commands([
33
                MakeIntent::class,
34
                MakeInteraction::class,
35
                ListDrivers::class,
36
                InstallDriver::class,
37
                ListChannels::class,
38
            ]);
39
        }
40
    }
41
42
    /**
43
     * Register the service provider.
44
     *
45
     * @return void
46
     */
47
    public function register(): void
48
    {
49
        $this->registerConfiguration();
50
        $this->registerRoutes();
51
        $this->registerDriverManager();
52
        $this->registerChannelManager();
53
        $this->registerConversationManager();
54
        $this->registerSessionManager();
55
        $this->registerIntentManager();
56
        $this->registerKernel();
57
    }
58
59
    private function registerConfiguration()
60
    {
61
        $this->mergeConfigFrom(__DIR__.'/../../config/fondbot.php', 'fondbot');
62
    }
63
64
    private function registerRoutes(): void
65
    {
66
        if (get_class($this->app) === 'Laravel\Lumen\Application') {
67
            $router = $this->app;
68
69
            require __DIR__.'/../../resources/routes.php';
70
        } else {
71
            $this->loadRoutesFrom(__DIR__.'/../../resources/routes.php');
72
        }
73
    }
74
75
    private function registerDriverManager(): void
76
    {
77
        $this->app->singleton(DriverManager::class, function () {
78
            $manager = new DriverManager;
79
            $manager->register(array_get($this->config(), 'drivers', []));
80
81
            return $manager;
82
        });
83
    }
84
85
    private function registerChannelManager(): void
86
    {
87
        $this->app->singleton(ChannelManager::class, function () {
88
            /** @var array $channels */
89
            $channels = collect(array_get($this->config(), 'channels', []))
90
                ->mapWithKeys(function (array $parameters, string $name) {
91
                    return [$name => $parameters];
92
                })
93
                ->toArray();
94
95
            $manager = new ChannelManager;
96
            $manager->register($channels);
97
98
            return $manager;
99
        });
100
    }
101
102
    private function registerConversationManager(): void
103
    {
104
        $this->app->singleton(ConversationManager::class, function () {
105
            return new ConversationManager($this->app->make(Kernel::class));
0 ignored issues
show
Unused Code introduced by
The call to ConversationManager::__construct() has too many arguments starting with $this->app->make(\FondBo...undation\Kernel::class).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
106
        });
107
    }
108
109
    private function registerSessionManager(): void
110
    {
111
        $this->app->singleton(SessionManager::class, function () {
112
            return new SessionManager(
113
                $this->app,
114
                $this->app->make(Store::class)
115
            );
116
        });
117
    }
118
119
    private function registerIntentManager(): void
120
    {
121
        $this->app->singleton(IntentManager::class, function () {
122
            $intents = array_get($this->config(), 'intents', []);
123
            $fallbackIntent = array_get($this->config(), 'fallback_intent', FallbackIntent::class);
124
125
            $manager = new IntentManager;
126
            $manager->register($intents, $fallbackIntent);
127
128
            return $manager;
129
        });
130
    }
131
132
    private function registerKernel(): void
133
    {
134
        $this->app->singleton(Kernel::class, function () {
135
            return Kernel::createInstance($this->app);
136
        });
137
    }
138
139
    private function config(): array
140
    {
141
        /** @var Repository $config */
142
        $config = $this->app[Repository::class];
143
144
        return $config->get('fondbot');
145
    }
146
147
    /**
148
     * Get the services provided by the provider.
149
     *
150
     * @return array
151
     */
152
    public function provides(): array
153
    {
154
        return [
155
            DriverManager::class,
156
            ChannelManager::class,
157
            ConversationManager::class,
158
            SessionManager::class,
159
            IntentManager::class,
160
            Kernel::class,
161
        ];
162
    }
163
}
164