Completed
Push — master ( f2ddff...f08741 )
by Anton
02:57
created

LoveServiceProvider::registerListeners()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of Laravel Love.
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\Love;
15
16
use Cog\Laravel\Love\Console\Commands\ReactionTypeAdd;
17
use Cog\Laravel\Love\Console\Commands\Recount;
18
use Cog\Laravel\Love\Console\Commands\RegisterReactants;
19
use Cog\Laravel\Love\Console\Commands\RegisterReacters;
20
use Cog\Laravel\Love\Console\Commands\SetupReactable;
21
use Cog\Laravel\Love\Console\Commands\SetupReacterable;
22
use Cog\Laravel\Love\Console\Commands\UpgradeV5ToV6;
23
use Cog\Laravel\Love\Console\Commands\UpgradeV7ToV8;
24
use Cog\Laravel\Love\Reactant\ReactionCounter\Models\ReactionCounter;
25
use Cog\Laravel\Love\Reactant\ReactionCounter\Observers\ReactionCounterObserver;
26
use Cog\Laravel\Love\Reactant\ReactionTotal\Models\ReactionTotal;
27
use Cog\Laravel\Love\Reactant\ReactionTotal\Observers\ReactionTotalObserver;
28
use Cog\Laravel\Love\Reaction\Models\Reaction;
29
use Cog\Laravel\Love\Reaction\Observers\ReactionObserver;
30
use Illuminate\Support\Facades\Config;
31
use Illuminate\Support\ServiceProvider;
32
33
final class LoveServiceProvider extends ServiceProvider
34
{
35
    /**
36
     * Register the service provider.
37
     *
38
     * @return void
39
     */
40
    public function register(): void
41
    {
42
    }
43
44
    /**
45
     * Bootstrap the application events.
46
     *
47
     * @return void
48
     */
49
    public function boot(): void
50
    {
51
        $this->configure();
52
        $this->registerConsoleCommands();
53
        $this->registerObservers();
54
        $this->registerPublishes();
55
        $this->registerMigrations();
56
    }
57
58
    /**
59
     * Determine if we should register default migrations.
60
     *
61
     * @return bool
62
     */
63
    private function shouldLoadDefaultMigrations(): bool
64
    {
65
        return Config::get('love.load_default_migrations', true);
66
    }
67
68
    /**
69
     * Register Love's models observers.
70
     *
71
     * @return void
72
     */
73
    private function registerObservers(): void
74
    {
75
        Reaction::observe(ReactionObserver::class);
76
        ReactionCounter::observe(ReactionCounterObserver::class);
77
        ReactionTotal::observe(ReactionTotalObserver::class);
78
    }
79
80
    /**
81
     * Register Love's console commands.
82
     *
83
     * @return void
84
     */
85
    private function registerConsoleCommands(): void
86
    {
87
        if ($this->app->runningInConsole()) {
88
            $this->commands([
89
                ReactionTypeAdd::class,
90
                Recount::class,
91
                SetupReactable::class,
92
                SetupReacterable::class,
93
                RegisterReactants::class,
94
                RegisterReacters::class,
95
                UpgradeV5ToV6::class,
96
                UpgradeV7ToV8::class,
97
            ]);
98
        }
99
    }
100
101
    /**
102
     * Setup the resource publishing groups for Love.
103
     *
104
     * @return void
105
     */
106
    private function registerPublishes(): void
107
    {
108
        if ($this->app->runningInConsole()) {
109
            $this->publishes([
110
                __DIR__ . '/../config/love.php' => config_path('love.php'),
111
            ], 'love-config');
112
113
            $this->publishes([
114
                __DIR__ . '/../database/migrations' => database_path('migrations'),
115
            ], 'love-migrations');
116
        }
117
    }
118
119
    /**
120
     * Register the Love migrations.
121
     *
122
     * @return void
123
     */
124
    private function registerMigrations(): void
125
    {
126
        if ($this->app->runningInConsole() && $this->shouldLoadDefaultMigrations()) {
127
            $this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
128
        }
129
    }
130
131
    /**
132
     * Merge Love configuration with the application configuration.
133
     *
134
     * @return void
135
     */
136
    private function configure(): void
137
    {
138
        if (!$this->app->configurationIsCached()) {
139
            $this->mergeConfigFrom(__DIR__ . '/../config/love.php', 'love');
140
        }
141
    }
142
}
143