Passed
Pull Request — master (#68)
by Anton
05:41 queued 02:10
created

LoveServiceProvider::configure()   A

Complexity

Conditions 2
Paths 2

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 2
nc 2
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\SetupReactable;
19
use Cog\Laravel\Love\Console\Commands\SetupReacterable;
20
use Cog\Laravel\Love\Console\Commands\UpgradeV5ToV6;
21
use Cog\Laravel\Love\Reactant\Listeners\DecrementAggregates;
22
use Cog\Laravel\Love\Reactant\Listeners\IncrementAggregates;
23
use Cog\Laravel\Love\Reaction\Events\ReactionHasBeenAdded;
24
use Cog\Laravel\Love\Reaction\Events\ReactionHasBeenRemoved;
25
use Cog\Laravel\Love\Reaction\Models\Reaction;
26
use Cog\Laravel\Love\Reaction\Observers\ReactionObserver;
27
use Illuminate\Contracts\Container\BindingResolutionException;
28
use Illuminate\Support\Facades\Event;
29
use Illuminate\Support\ServiceProvider;
30
31
final class LoveServiceProvider extends ServiceProvider
32
{
33
    /**
34
     * Register the service provider.
35
     *
36
     * @return void
37
     */
38
    public function register(): void
39
    {
40
    }
41
42
    /**
43
     * Bootstrap the application events.
44
     *
45
     * @return void
46
     */
47
    public function boot(): void
48
    {
49
        $this->configure();
50
        $this->registerConsoleCommands();
51
        $this->registerObservers();
52
        $this->registerPublishes();
53
        $this->registerMigrations();
54
        $this->registerListeners();
55
        $this->registerConstants();
56
    }
57
58
    /**
59
     * Register Love's models observers.
60
     *
61
     * @return void
62
     */
63
    private function registerObservers(): void
64
    {
65
        Reaction::observe(ReactionObserver::class);
66
    }
67
68
    /**
69
     * Register Love's console commands.
70
     *
71
     * @return void
72
     */
73
    private function registerConsoleCommands(): void
74
    {
75
        if ($this->app->runningInConsole()) {
76
            $this->commands([
77
                ReactionTypeAdd::class,
78
                Recount::class,
79
                SetupReactable::class,
80
                SetupReacterable::class,
81
                UpgradeV5ToV6::class,
82
            ]);
83
        }
84
    }
85
86
    /**
87
     * Setup the resource publishing groups for Love.
88
     *
89
     * @return void
90
     */
91
    private function registerPublishes(): void
92
    {
93
        if ($this->app->runningInConsole()) {
94
            $this->publishes([
95
                __DIR__ . '/../config/love.php' => config_path('love.php'),
96
            ], 'love-config');
97
98
            $this->publishes([
99
                __DIR__ . '/../database/migrations' => database_path('migrations'),
100
            ], 'love-migrations');
101
        }
102
    }
103
104
    /**
105
     * Register the Love migrations.
106
     *
107
     * @return void
108
     */
109
    private function registerMigrations(): void
110
    {
111
        if ($this->app->runningInConsole()) {
112
            $this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
113
        }
114
    }
115
116
    /**
117
     * Register the Love event listeners.
118
     *
119
     * @return void
120
     */
121
    private function registerListeners(): void
122
    {
123
        Event::listen(ReactionHasBeenAdded::class, IncrementAggregates::class);
124
        Event::listen(ReactionHasBeenRemoved::class, DecrementAggregates::class);
125
    }
126
127
    private function configure(): void
128
    {
129
        if (!$this->app->configurationIsCached()) {
130
            $this->mergeConfigFrom(__DIR__ . '/../config/love.php', 'love');
131
        }
132
    }
133
134
    /**
135
     * Register the Love constants.
136
     *
137
     * @return void
138
     */
139
    private function registerConstants(): void
140
    {
141
        if (!defined('COG_LOVE_DB_CONNECTION')) {
142
            try {
143
                $connection = $this->app->make('config')->get('love.database.connection');
144
            } catch (BindingResolutionException $e) {
145
                $connection = null;
146
            }
147
148
            define('COG_LOVE_DB_CONNECTION', $connection);
149
        }
150
    }
151
}
152