Passed
Push — master ( 4b0d05...471523 )
by Arthur
103:49 queued 98:11
created

passedRegistrationCondition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Foundation\Providers;
4
5
use Foundation\Console\SeedCommand;
6
use Foundation\Contracts\ConditionalAutoRegistration;
7
use Foundation\Contracts\ModelPolicyContract;
8
use Foundation\Contracts\Ownable;
9
use Foundation\Observers\CacheObserver;
10
use Foundation\Policies\OwnershipPolicy;
11
use Foundation\Services\BootstrapRegistrarService;
12
use Foundation\Traits\Cacheable;
13
use Illuminate\Database\Eloquent\Factory;
14
use Illuminate\Support\Facades\Event;
15
use Illuminate\Support\Facades\Gate;
16
use Illuminate\Support\ServiceProvider;
17
use Route;
18
19
/**
20
 * Class BootstrapServiceProvider.
21
 */
22
class BootstrapServiceProvider extends ServiceProvider
23
{
24
    /**
25
     * @var BootstrapRegistrarService
26
     */
27
    protected $bootstrapService;
28
29 58
    public function boot()
30
    {
31
        /* Load cache observers only when caching is enabled */
32 58
        if (config('model.caching')) {
33 58
            $this->loadCacheObservers();
34
        }
35 58
    }
36
37 58
    public function register()
38
    {
39
        /* Load BootstrapService here because of the dependencies needed in BootstrapRegistrarService */
40 58
        $this->loadBootstrapService();
41
42 58
        $this->loadCommands();
43 58
        $this->loadRoutes();
44 58
        $this->loadConfigs();
45 58
        $this->loadFactories();
46 58
        $this->loadMigrations();
47 58
        $this->loadListeners();
48
49
        /* Override the seed command with the larapi custom one */
50 58
        $this->overrideSeedCommand();
51
52 58
        $this->loadOwnershipPolicies();
53
54
        /* Register Policies after ownership policies otherwise they would not get overriden */
55 58
        $this->loadPolicies();
56
57
        /* Register all Module Service providers.
58
        ** Always load at the end so the user has the ability to override certain functionality
59
         * */
60 58
        $this->loadServiceProviders();
61 58
    }
62
63 58
    private function loadBootstrapService()
64
    {
65 58
        $this->bootstrapService = new BootstrapRegistrarService();
66
67 58
        if (!($this->app->environment('production') || $this->app->environment('testing'))) {
68
            $this->bootstrapService->recache();
69
        }
70 58
    }
71
72 58
    private function loadCommands()
73
    {
74 58
        $this->commands($this->bootstrapService->getCommands());
75 58
    }
76
77 58
    private function loadRoutes()
78
    {
79 58
        foreach ($this->bootstrapService->getRoutes() as $route) {
80 58
            $path = $route['path'];
81 58
            $fileNameArray = explode('.', $route['filename']);
82 58
            $routePrefix = $fileNameArray[0];
83 58
            $version = $fileNameArray[1];
84
85 58
            if ($version === 'php') {
86
                $prefix = $routePrefix;
87
            } else {
88 58
                $prefix = $version.'/'.$routePrefix;
89
            }
90
91 58
            Route::group([
92 58
                'prefix'     => $prefix,
93 58
                'namespace'  => $route['controller'],
94 58
                'domain'     => $route['domain'],
95
                'middleware' => ['api'],
96
            ], function () use ($path) {
97 58
                require $path;
98 58
            });
99 58
            Route::model($route['module'], $route['model']);
100
        }
101 58
    }
102
103
    /**
104
     * Register config.
105
     *
106
     * @return void
107
     */
108 58
    protected function loadConfigs()
109
    {
110 58
        foreach ($this->bootstrapService->getConfigs() as $config) {
111 58
            if (isset($config['filename']) && is_string($config['filename'])) {
112 58
                $fileName = $config['filename'];
113 58
                $configName = strtolower(explode('.', $fileName)[0]);
114 58
                $this->mergeConfigFrom(
115 58
                    $config['path'], $configName
116
                );
117
            }
118
        }
119 58
    }
120
121
    /**
122
     * Register additional directories of factories.
123
     *
124
     * @return void
125
     */
126 58
    public function loadFactories()
127
    {
128 58
        foreach ($this->bootstrapService->getFactories() as $factory) {
129 58
            if (!$this->app->environment('production')) {
130 58
                app(Factory::class)->load($factory['path']);
131
            }
132
        }
133 58
    }
134
135
    /**
136
     * Register additional directories of migrations.
137
     *
138
     * @return void
139
     */
140 58
    public function loadMigrations()
141
    {
142 58
        foreach ($this->bootstrapService->getMigrations() as $migration) {
143 58
            $this->loadMigrationsFrom($migration['path']);
144
        }
145 58
    }
146
147 58
    private function loadPolicies()
148
    {
149 58
        foreach ($this->bootstrapService->getPolicies() as $policy) {
150 58
            if (class_implements_interface($policy['class'], ModelPolicyContract::class)) {
151 58
                Gate::policy($policy['model'], $policy['class']);
152
            }
153
        }
154 58
    }
155
156 58
    private function overrideSeedCommand()
157
    {
158 58
        $app = $this->app;
159 58
        $service = $this->bootstrapService;
160
        $this->app->extend('command.seed', function () use ($app, $service) {
161 58
            return new SeedCommand($app['db'], $service);
162 58
        });
163 58
    }
164
165 58
    private function loadCacheObservers()
166
    {
167 58
        foreach ($this->bootstrapService->getModels() as $model) {
168 58
            if (class_uses_trait($model, Cacheable::class)) {
169 58
                $model::observe(CacheObserver::class);
170
            }
171
        }
172 58
    }
173
174 58
    private function loadOwnershipPolicies()
175
    {
176 58
        foreach ($this->bootstrapService->getModels() as $model) {
177 58
            if (class_implements_interface($model, Ownable::class)) {
178 58
                Gate::policy($model, OwnershipPolicy::class);
179 58
                Gate::define('access', OwnershipPolicy::class.'@access');
180
            }
181
        }
182 58
    }
183
184 58
    private function loadServiceProviders()
185
    {
186 58
        foreach ($this->bootstrapService->getProviders() as $provider) {
187 58
            if ($this->passedRegistrationCondition($provider)) {
188 58
                $this->app->register($provider);
189
            }
190
        }
191 58
    }
192
193 58
    private function loadListeners()
194
    {
195 58
        foreach ($this->bootstrapService->getEvents() as $event) {
196 58
            foreach ($event['listeners'] as $listener) {
197 58
                Event::listen($event['class'], $listener);
198
            }
199
        }
200 58
    }
201
202 58
    private function passedRegistrationCondition($class)
203
    {
204 58
        if (!class_implements_interface($class, ConditionalAutoRegistration::class)) {
205 58
            return true;
206
        }
207
208 58
        return call_class_function($class, 'registrationCondition');
209
    }
210
}
211