Completed
Push — master ( debe90...9d4df6 )
by Avtandil
35:11 queued 20:14
created

ServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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