TelegramServiceProvider::setupConfig()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 9
cp 0
rs 9.8666
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 20
1
<?php
2
3
namespace Telegram\Bot\Laravel;
4
5
use Telegram\Bot\Api;
6
use Telegram\Bot\BotsManager;
7
use Illuminate\Support\ServiceProvider;
8
use Illuminate\Contracts\Container\Container as Application;
9
use Laravel\Lumen\Application as LumenApplication;
10
use Illuminate\Foundation\Application as LaravelApplication;
11
12
/**
13
 * Class TelegramServiceProvider.
14
 */
15
class TelegramServiceProvider extends ServiceProvider
16
{
17
    /**
18
     * Indicates if loading of the provider is deferred.
19
     *
20
     * @var bool
21
     */
22
    protected $defer = true;
23
24
    /**
25
     * Boot the service provider.
26
     *
27
     * @return void
28
     */
29
    public function boot()
30
    {
31
        $this->setupConfig($this->app);
32
    }
33
34
    /**
35
     * Setup the config.
36
     *
37
     * @param \Illuminate\Contracts\Container\Container $app
38
     *
39
     * @return void
40
     */
41
    protected function setupConfig(Application $app)
42
    {
43
        $source = __DIR__.'/config/telegram.php';
44
45
        if ($app instanceof LaravelApplication && $app->runningInConsole()) {
0 ignored issues
show
Bug introduced by
The class Illuminate\Foundation\Application does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
46
            $this->publishes([$source => config_path('telegram.php')], 'config');
47
        } elseif ($app instanceof LumenApplication) {
0 ignored issues
show
Bug introduced by
The class Laravel\Lumen\Application does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
48
            $app->configure('telegram');
49
        }
50
51
        $this->mergeConfigFrom($source, 'telegram');
52
    }
53
54
    /**
55
     * Register the service provider.
56
     *
57
     * @return void
58
     */
59
    public function register()
60
    {
61
        $this->registerManager($this->app);
62
        $this->registerBindings($this->app);
63
    }
64
65
    /**
66
     * Register the manager class.
67
     *
68
     * @param \Illuminate\Contracts\Container\Container $app
69
     *
70
     * @return void
71
     */
72
    protected function registerManager(Application $app)
73
    {
74
        $app->singleton('telegram', function ($app) {
75
            $config = (array)$app['config']['telegram'];
76
77
            return (new BotsManager($config))->setContainer($app);
78
        });
79
80
        $app->alias('telegram', BotsManager::class);
81
    }
82
83
    /**
84
     * Register the bindings.
85
     *
86
     * @param \Illuminate\Contracts\Container\Container $app
87
     *
88
     * @return void
89
     */
90
    protected function registerBindings(Application $app)
91
    {
92
        $app->bind('telegram.bot', function ($app) {
93
            $manager = $app['telegram'];
94
95
            return $manager->bot();
96
        });
97
98
        $app->alias('telegram.bot', Api::class);
99
    }
100
101
    /**
102
     * Get the services provided by the provider.
103
     *
104
     * @return array
105
     */
106
    public function provides()
107
    {
108
        return ['telegram', 'telegram.bot', BotsManager::class, Api::class];
109
    }
110
}
111