BootstrapServiceProvider   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Test Coverage

Coverage 97.65%

Importance

Changes 0
Metric Value
eloc 61
dl 0
loc 169
ccs 83
cts 85
cp 0.9765
rs 9.76
c 0
b 0
f 0
wmc 33

14 Methods

Rating   Name   Duplication   Size   Complexity  
A loadServiceProviders() 0 4 2
A boot() 0 5 2
A loadConfigs() 0 6 2
A loadModelPolicies() 0 7 4
A register() 0 25 1
A loadCommands() 0 4 2
A overrideSeedCommand() 0 6 1
A loadFactories() 0 5 3
A loadRoutes() 0 16 3
A loadListeners() 0 5 3
A loadBootstrapService() 0 6 2
A loadModelObservers() 0 5 3
A loadCacheObservers() 0 5 3
A loadMigrations() 0 4 2
1
<?php
2
3
namespace Foundation\Providers;
4
5
use Foundation\Console\SeedCommand;
6
use Foundation\Observers\CacheObserver;
7
use Foundation\Policies\OwnershipPolicy;
8
use Foundation\Services\BootstrapRegistrarService;
9
use Illuminate\Database\Eloquent\Factory;
10
use Illuminate\Support\Facades\Event;
11
use Illuminate\Support\Facades\Gate;
12
use Illuminate\Support\ServiceProvider;
13
use Route;
14
15
/**
16
 * Class BootstrapServiceProvider.
17
 */
18
class BootstrapServiceProvider extends ServiceProvider
19
{
20
    /**
21
     * @var BootstrapRegistrarService
22
     */
23
    protected $bootstrapService;
24
25 43
    public function boot()
26
    {
27
        /* Load cache observers only when caching is enabled */
28 43
        if (config('model.caching')) {
29 43
            $this->loadCacheObservers();
30
        }
31 43
    }
32
33 43
    public function register()
34
    {
35
        /* Load BootstrapService here because of the dependencies needed in BootstrapRegistrarService */
36 43
        $this->loadBootstrapService();
37
38 43
        $this->loadCommands();
39 43
        $this->loadRoutes();
40 43
        $this->loadConfigs();
41 43
        $this->loadFactories();
42 43
        $this->loadMigrations();
43 43
        $this->loadListeners();
44
45
        /* Override the seed command with the larapi custom one */
46 43
        $this->overrideSeedCommand();
47
48
        /* Register Model Policies */
49 43
        $this->loadModelPolicies();
50
51
        /* Register Model Observers */
52 43
        $this->loadModelObservers();
53
54
        /* Register all Module Service providers.
55
        ** Always load at the end so the user has the ability to override certain functionality
56
         * */
57 43
        $this->loadServiceProviders();
58 43
    }
59
60 43
    private function loadBootstrapService()
61
    {
62 43
        $this->bootstrapService = new BootstrapRegistrarService();
63
64 43
        if (! ($this->app->environment('production'))) {
65 43
            $this->bootstrapService->recache();
66
        }
67 43
    }
68
69 43
    private function loadCommands()
70
    {
71 43
        foreach ($this->bootstrapService->getCommands() as $command) {
72 43
            $this->commands($command['class']);
73
        }
74 43
    }
75
76 43
    private function loadRoutes()
77
    {
78 43
        $moduleModel = null;
79 43
        foreach ($this->bootstrapService->getRoutes() as $route) {
80 43
            Route::group([
81 43
                'prefix' => $route['prefix'],
82 43
                'namespace' => $route['controller_namespace'],
83 43
                'domain' => $route['domain'],
84
                'middleware' => ['api'],
85
            ], function () use ($route) {
86 43
                require $route['path'];
87 43
            });
88 43
            if ($moduleModel !== $route['module_model']) {
89
                Route::model(strtolower(get_short_class_name($route['module_model'])), $route['module_model']);
90
            }
91 43
            $moduleModel = $route['module_model'];
92
        }
93 43
    }
94
95
    /**
96
     * Register config.
97
     *
98
     * @return void
99
     */
100 43
    protected function loadConfigs()
101
    {
102 43
        foreach ($this->bootstrapService->getConfigs() as $config) {
103 43
            $this->mergeConfigFrom(
104 43
                $config['path'],
105 43
                $config['name']
106
            );
107
        }
108 43
    }
109
110
    /**
111
     * Register additional directories of factories.
112
     *
113
     * @return void
114
     */
115 43
    public function loadFactories()
116
    {
117 43
        foreach ($this->bootstrapService->getFactories() as $factory) {
118 43
            if (! $this->app->environment('production')) {
119 43
                app(Factory::class)->load($factory['path']);
120
            }
121
        }
122 43
    }
123
124
    /**
125
     * Register additional directories of migrations.
126
     *
127
     * @return void
128
     */
129 43
    public function loadMigrations()
130
    {
131 43
        foreach ($this->bootstrapService->getMigrations() as $migration) {
132 43
            $this->loadMigrationsFrom($migration['path']);
133
        }
134 43
    }
135
136 43
    private function overrideSeedCommand()
137
    {
138 43
        $app = $this->app;
139 43
        $service = $this->bootstrapService;
140
        $this->app->extend('command.seed', function () use ($app, $service) {
141 26
            return new SeedCommand($app['db'], $service);
142 43
        });
143 43
    }
144
145 43
    private function loadCacheObservers()
146
    {
147 43
        foreach ($this->bootstrapService->getModels() as $model) {
148 43
            if ($model['cacheable']) {
149 43
                $model['class']::observe(CacheObserver::class);
150
            }
151
        }
152 43
    }
153
154 43
    private function loadModelPolicies()
155
    {
156 43
        foreach ($this->bootstrapService->getModels() as $model) {
157 43
            foreach ($model['policies'] as $policy) {
158 43
                Gate::policy($model['class'], $policy);
159 43
                if ($model['ownable']) {
160 43
                    Gate::define('access', OwnershipPolicy::class.'@access');
161
                }
162
            }
163
        }
164 43
    }
165
166 43
    private function loadModelObservers()
167
    {
168 43
        foreach ($this->bootstrapService->getModels() as $model) {
169 43
            foreach ($model['observers'] as $observer) {
170
                $model['class']::observe($observer);
171
            }
172
        }
173 43
    }
174
175 43
    private function loadServiceProviders()
176
    {
177 43
        foreach ($this->bootstrapService->getProviders() as $provider) {
178 43
            $this->app->register($provider['class']);
179
        }
180 43
    }
181
182 43
    private function loadListeners()
183
    {
184 43
        foreach ($this->bootstrapService->getEvents() as $event) {
185 43
            foreach ($event['listeners'] as $listener) {
186 43
                Event::listen($event['class'], $listener);
187
            }
188
        }
189 43
    }
190
}
191