Completed
Pull Request — master (#19)
by
unknown
13:13
created

ServiceProvider::provides()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
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
use PhpTelegramBot\Laravel\Commands\WebhookCommand;
17
18
class ServiceProvider extends LaravelServiceProvider
19
{
20
    /**
21
     * Indicates if loading of the provider is deferred.
22
     *
23
     * @var bool
24
     */
25
    protected $defer = true;
26
27
    /**
28
     * Bootstrap any application services.
29
     *
30
     * @return void
31
     */
32
    public function boot()
33
    {
34
        // Publish config files
35
        $this->publishes([
36
            __DIR__ . '/../config/config.php' => config_path('phptelegrambot.php'),
37
        ], 'config');
38
39
        // Append the default settings
40
        $this->mergeConfigFrom(
41
            __DIR__ . '/../config/config.php',
42
            'phptelegrambot'
43
        );
44
45
        $this->publishes([
46
            __DIR__ . '/../database/migrations/' => database_path('migrations'),
47
        ], 'migrations');
48
    }
49
50
    /**
51
     * Register any application services.
52
     *
53
     * @return void
54
     */
55
    public function register()
56
    {
57
        $this->app->bind(PhpTelegramBotContract::class, static function ($app) {
58
            $config = $app['config']->get('phptelegrambot');
59
60
            $bot = new PhpTelegramBot($config['bot']['api_key'], ! empty($config['bot']['name']) ? $config['bot']['name'] : '');
61
62
            // Add commands if paths are given
63
            if (! empty($config['commands']['paths'])) {
64
                $bot->addCommandsPaths($config['commands']['paths']);
65
            }
66
67
            // Set command related configs
68
            if (! empty($config['commands']['configs'])) {
69
                foreach ($config['commands']['configs'] as $command_name => $command_config) {
70
                    $bot->setCommandConfig($command_name, $command_config);
71
                }
72
            }
73
74
            // Set database connection
75
            if ($config['database']['enabled'] === true) {
76
                /** @var \Illuminate\Database\Connection $connection */
77
                $connection = $app['db']->connection($config['database']['connection']);
78
                /*
79
                    Ability to use custom table prefix
80
                */
81
                $tablePrefix = (!empty($config['database']['prefix']) ? $config['database']['prefix'] : '';
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ';'
Loading history...
82
                $bot->enableExternalMySql($connection->getPdo(), $tablePrefix);
83
            }
84
85
            // Enable admins if provided
86
            if (! empty($config['admins'])) {
87
                $bot->enableAdmins($config['admins']);
88
            }
89
90
            // Set paths
91
            if (! empty($config['download_path'])) {
92
                $bot->setDownloadPath($config['download_path']);
93
            }
94
            if (! empty($config['upload_path'])) {
95
                $bot->setUploadPath($config['upload_path']);
96
            }
97
98
            return $bot;
99
        });
100
101
        if ($this->app->runningInConsole()) {
102
            $this->commands([
103
                WebhookCommand::class,
104
            ]);
105
        }
106
    }
107
108
    /**
109
     * Get the services provided by the provider.
110
     *
111
     * @return array
112
     */
113
    public function provides()
114
    {
115
        return [PhpTelegramBotContract::class];
116
    }
117
}
118