Completed
Pull Request — master (#28)
by ARCANEDEV
09:22
created

SettingsServiceProvider::boot()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
c 0
b 0
f 0
cc 3
nc 4
nop 0
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelSettings;
6
7
use Arcanedev\LaravelSettings\Contracts\Manager as ManagerContract;
8
use Arcanedev\LaravelSettings\Contracts\Store as StoreContract;
9
use Arcanedev\Support\Providers\PackageServiceProvider;
10
use Illuminate\Contracts\Support\DeferrableProvider;
11
12
/**
13
 * Class     SettingsServiceProvider
14
 *
15
 * @author   ARCANEDEV <[email protected]>
16
 */
17
class SettingsServiceProvider extends PackageServiceProvider implements DeferrableProvider
18
{
19
    /* -----------------------------------------------------------------
20
     |  Properties
21
     | -----------------------------------------------------------------
22
     */
23
24
    /**
25
     * Package name.
26
     *
27
     * @var string
28
     */
29
    protected $package = 'settings';
30
31
    /* -----------------------------------------------------------------
32
     |  Main Methods
33
     | -----------------------------------------------------------------
34
     */
35
36
    /**
37
     * Register the service provider.
38
     */
39 192
    public function register(): void
40
    {
41 192
        parent::register();
42
43 192
        $this->registerConfig();
44
45 192
        $this->registerSettingsManager();
46 192
    }
47
48
    /**
49
     * Boot the service provider.
50
     */
51 192
    public function boot(): void
52
    {
53 192
        SettingsManager::$runsMigrations ? $this->loadMigrations() : $this->publishMigrations();
54
55 192
        if ($this->app->runningInConsole()) {
56 192
            $this->publishConfig();
57
        }
58 192
    }
59
60
    /**
61
     * Get the services provided by the provider.
62
     *
63
     * @return array
64
     */
65 4
    public function provides(): array
66
    {
67
        return [
68 4
            ManagerContract::class,
69
            StoreContract::class,
70
        ];
71
    }
72
73
    /* -----------------------------------------------------------------
74
     |  Other Methods
75
     | -----------------------------------------------------------------
76
     */
77
78
    /**
79
     * Register the Settings Manager & Store drivers.
80
     */
81 192
    private function registerSettingsManager(): void
82
    {
83 96
        $this->singleton(ManagerContract::class, SettingsManager::class);
84
85 96
        $this->app->extend(ManagerContract::class, function (ManagerContract $manager, $app) {
86 180
            foreach ($app['config']->get('settings.drivers', []) as $driver => $params) {
87 180
                $manager->registerStore($driver, $params);
88
            }
89
90 180
            return $manager;
91 192
        });
92
93 96
        $this->singleton(StoreContract::class, function ($app): StoreContract {
94 10
            return $app[ManagerContract::class]->driver();
95 192
        });
96 192
    }
97
}
98