ServiceProvider   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 34
c 3
b 1
f 0
dl 0
loc 96
rs 10
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
B register() 0 47 10
A boot() 0 16 1
A provides() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the PhpTelegramBot/Laravel package.
5
 *
6
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace PhpTelegramBot\Laravel;
15
16
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
17
use PhpTelegramBot\Laravel\Commands\WebhookCommand;
18
19
class ServiceProvider extends LaravelServiceProvider
20
{
21
    /**
22
     * Indicates if loading of the provider is deferred.
23
     *
24
     * @var bool
25
     */
26
    protected $defer = true;
27
28
    /**
29
     * Bootstrap any application services.
30
     *
31
     * @return void
32
     */
33
    public function boot(): void
34
    {
35
        // Publish config files
36
        $this->publishes([
37
            __DIR__ . '/../config/config.php' => config_path('phptelegrambot.php'),
38
        ], 'config');
39
40
        // Append the default settings
41
        $this->mergeConfigFrom(
42
            __DIR__ . '/../config/config.php',
43
            'phptelegrambot',
44
        );
45
46
        $this->publishes([
47
            __DIR__ . '/../database/migrations/' => database_path('migrations'),
48
        ], 'migrations');
49
    }
50
51
    /**
52
     * Register any application services.
53
     *
54
     * @return void
55
     */
56
    public function register(): void
57
    {
58
        $this->app->bind(PhpTelegramBotContract::class, static function ($app) {
59
            $config = $app['config']->get('phptelegrambot');
60
61
            $bot = new PhpTelegramBot($config['bot']['api_key'], ! empty($config['bot']['name']) ? $config['bot']['name'] : '');
62
63
            // Add commands if paths are given
64
            if (! empty($config['commands']['paths'])) {
65
                $bot->addCommandsPaths($config['commands']['paths']);
66
            }
67
68
            // Set command related configs
69
            if (! empty($config['commands']['configs'])) {
70
                foreach ($config['commands']['configs'] as $commandName => $commandConfig) {
71
                    $bot->setCommandConfig($commandName, $commandConfig);
72
                }
73
            }
74
75
            // Set database connection
76
            if ($config['database']['enabled'] === true) {
77
                /** @var \Illuminate\Database\Connection $connection */
78
                $connection = $app['db']->connection($config['database']['connection']);
79
                // Ability to use custom table prefix
80
                $tablePrefix = $config['database']['prefix'] ?? '';
81
                $bot->enableExternalMySql($connection->getPdo(), $tablePrefix);
82
            }
83
84
            // Enable admins if provided
85
            if (! empty($config['admins'])) {
86
                $bot->enableAdmins($config['admins']);
87
            }
88
89
            // Set paths
90
            if (! empty($config['download_path'])) {
91
                $bot->setDownloadPath($config['download_path']);
92
            }
93
            if (! empty($config['upload_path'])) {
94
                $bot->setUploadPath($config['upload_path']);
95
            }
96
97
            return $bot;
98
        });
99
100
        if ($this->app->runningInConsole()) {
101
            $this->commands([
102
                WebhookCommand::class,
103
            ]);
104
        }
105
    }
106
107
    /**
108
     * Get the services provided by the provider.
109
     *
110
     * @return array
111
     */
112
    public function provides(): array
113
    {
114
        return [PhpTelegramBotContract::class];
115
    }
116
}
117