BanServiceProvider   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 26
c 1
b 0
f 0
dl 0
loc 118
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A registerObservers() 0 3 1
A registerConsoleCommands() 0 7 2
A registerMigrations() 0 4 3
A register() 0 4 1
A registerContracts() 0 4 1
A registerPublishes() 0 13 2
A configure() 0 4 2
A shouldLoadDefaultMigrations() 0 3 1
A boot() 0 5 1
1
<?php
2
3
/*
4
 * This file is part of Laravel Ban.
5
 *
6
 * (c) Anton Komarev <[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 Cog\Laravel\Ban\Providers;
15
16
use Cog\Contracts\Ban\Ban as BanContract;
17
use Cog\Contracts\Ban\BanService as BanServiceContract;
18
use Cog\Laravel\Ban\Console\Commands\DeleteExpiredBans;
19
use Cog\Laravel\Ban\Models\Ban;
20
use Cog\Laravel\Ban\Observers\BanObserver;
21
use Cog\Laravel\Ban\Services\BanService;
22
use Illuminate\Support\Facades\Config;
23
use Illuminate\Support\ServiceProvider;
24
25
class BanServiceProvider extends ServiceProvider
26
{
27
    /**
28
     * Register bindings in the container.
29
     *
30
     * @return void
31
     */
32
    public function register(): void
33
    {
34
        $this->registerContracts();
35
        $this->registerConsoleCommands();
36
    }
37
38
    /**
39
     * Perform post-registration booting of services.
40
     *
41
     * @return void
42
     *
43
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
44
     */
45
    public function boot(): void
46
    {
47
        $this->configure();
48
        $this->registerPublishes();
49
        $this->registerObservers();
50
    }
51
52
    /**
53
     * Register Ban's console commands.
54
     *
55
     * @return void
56
     */
57
    protected function registerConsoleCommands(): void
58
    {
59
        if ($this->app->runningInConsole()) {
60
            $this->app->bind('command.ban:delete-expired', DeleteExpiredBans::class);
61
62
            $this->commands([
63
                'command.ban:delete-expired',
64
            ]);
65
        }
66
    }
67
68
    /**
69
     * Register Ban's classes in the container.
70
     *
71
     * @return void
72
     */
73
    protected function registerContracts(): void
74
    {
75
        $this->app->bind(BanContract::class, Ban::class);
76
        $this->app->singleton(BanServiceContract::class, BanService::class);
77
    }
78
79
    /**
80
     * Register Ban's models observers.
81
     *
82
     * @return void
83
     *
84
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
85
     */
86
    protected function registerObservers(): void
87
    {
88
        $this->app->make(BanContract::class)->observe(new BanObserver);
0 ignored issues
show
Bug introduced by
The method observe() does not exist on Cog\Contracts\Ban\Ban. Since it exists in all sub-types, consider adding an abstract or default implementation to Cog\Contracts\Ban\Ban. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
        $this->app->make(BanContract::class)->/** @scrutinizer ignore-call */ observe(new BanObserver);
Loading history...
89
    }
90
91
    /**
92
     * Setup the resource publishing groups for Ban.
93
     *
94
     * @return void
95
     */
96
    protected function registerPublishes(): void
97
    {
98
        if ($this->app->runningInConsole()) {
99
            $this->publishes([
100
                __DIR__ . '/../../config/ban.php' => config_path('ban.php'),
101
            ], 'ban-config');
102
103
            $this->publishes([
104
                __DIR__ . '/../../database/migrations' => database_path('migrations'),
105
            ], 'migrations');
106
        }
107
108
        $this->registerMigrations();
109
    }
110
111
    /**
112
     * Register the Ban migrations.
113
     *
114
     * @return void
115
     */
116
    private function registerMigrations(): void
117
    {
118
        if ($this->app->runningInConsole() && $this->shouldLoadDefaultMigrations()) {
119
            $this->loadMigrationsFrom(__DIR__ . '/../../database/migrations');
120
        }
121
    }
122
123
    /**
124
     * Merge Ban configuration with the application configuration.
125
     *
126
     * @return void
127
     */
128
    private function configure(): void
129
    {
130
        if (!$this->app->configurationIsCached()) {
131
            $this->mergeConfigFrom(__DIR__ . '/../../config/ban.php', 'ban');
132
        }
133
    }
134
135
    /**
136
     * Determine if we should register default migrations.
137
     *
138
     * @return bool
139
     */
140
    private function shouldLoadDefaultMigrations(): bool
141
    {
142
        return Config::get('ban.load_default_migrations', true);
143
    }
144
}
145