Passed
Push — master ( 67253e...1a1f73 )
by Arthur
04:53 queued 11s
created

BootstrapServiceProvider::loadOwnershipPolicies()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 0
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Foundation\Providers;
4
5
6
use Foundation\Console\SeedCommand;
7
use Foundation\Contracts\Cacheable;
8
use Foundation\Contracts\ModelPolicyContract;
9
use Foundation\Contracts\Ownable;
10
use Foundation\Observers\CacheObserver;
11
use Foundation\Policies\OwnershipPolicy;
12
use Foundation\Services\BootstrapRegistrarService;
13
use Illuminate\Database\Eloquent\Factory;
14
use Illuminate\Support\Facades\Gate;
15
use Illuminate\Support\ServiceProvider;
16
use Route;
17
18
/**
19
 * Class BootstrapServiceProvider.
20
 */
21
class BootstrapServiceProvider extends ServiceProvider
22
{
23
    /**
24
     * @var BootstrapRegistrarService
25
     */
26
    protected $bootstrapService;
27
28
29 17
    public function boot()
30
    {
31 17
        $this->overrideSeedCommand();
32 17
        $this->loadCacheObservers();
33 17
        $this->loadOwnershipPolicies();
34
35
        /* Register Policies after ownership policies otherwise they would not get overriden */
36 17
        $this->loadPolicies();
37 17
    }
38
39
40 17
    public function register()
41
    {
42
        /* Load BootstrapService here because of the dependencies needed in BootstrapRegistrarService */
43 17
        $this->loadBootstrapService();
44
45 17
        $this->loadCommands();
46 17
        $this->loadRoutes();
47 17
        $this->loadConfigs();
48 17
        $this->loadFactories();
49 17
        $this->loadMigrations();
50 17
    }
51
52 17
    private function loadBootstrapService()
53
    {
54 17
        $this->bootstrapService = new BootstrapRegistrarService();
55
56 17
        if (!$this->app->environment('production')) {
57 17
            $this->bootstrapService->recache();
58
        }
59 17
    }
60
61 17
    private function loadCommands()
62
    {
63 17
        $this->commands($this->bootstrapService->getCommands());
64 17
    }
65
66 17
    private function loadRoutes()
67
    {
68 17
        foreach ($this->bootstrapService->getRoutes() as $route) {
69 17
            $path = $route['path'];
70 17
            Route::group([
71 17
                'prefix' => 'v1/' . $route['module'],
72 17
                'namespace' => $route['controller'],
73 17
                'domain' => $route['domain'],
74
                'middleware' => ['api'],
75
            ], function () use ($path) {
76 17
                require $path;
77 17
            });
78 17
            Route::model($route['module'], $route['model']);
79
        }
80 17
    }
81
82
    /**
83
     * Register config.
84
     *
85
     * @return void
86
     */
87 17
    protected function loadConfigs()
88
    {
89 17
        foreach ($this->bootstrapService->getConfigs() as $config) {
90 17
            $this->publishes([
91 17
                $config['path'] => config_path($config['module']),
92 17
            ], 'config');
93 17
            $this->mergeConfigFrom(
94 17
                $config['path'], basename($config['module'], '.php')
95
            );
96
        }
97 17
    }
98
99
    /**
100
     * Register additional directories of factories.
101
     *
102
     * @return void
103
     */
104 17
    public function loadFactories()
105
    {
106 17
        foreach ($this->bootstrapService->getFactories() as $factory) {
107 17
            if (!$this->app->environment('production')) {
108 17
                app(Factory::class)->load($factory['path']);
109
            }
110
        }
111 17
    }
112
113
    /**
114
     * Register additional directories of migrations.
115
     *
116
     * @return void
117
     */
118 17
    public function loadMigrations()
119
    {
120 17
        foreach ($this->bootstrapService->getMigrations() as $migration) {
121 17
            $this->loadMigrationsFrom($migration['path']);
122
        }
123 17
    }
124
125 17
    private function loadPolicies()
126
    {
127 17
        foreach ($this->bootstrapService->getPolicies() as $policy) {
128 17
            if (classImplementsInterface($policy['class'], ModelPolicyContract::class)) {
129 17
                Gate::policy($policy['model'], $policy['class']);
130
            }
131
        }
132 17
    }
133
134 17
    private function overrideSeedCommand()
135
    {
136 17
        $app = $this->app;
137 17
        $service = $this->bootstrapService;
138
        $this->app->extend('command.seed', function () use ($app, $service) {
139 17
            return new SeedCommand($app['db'], $service);
140 17
        });
141 17
    }
142
143 17
    private function loadCacheObservers()
144
    {
145 17
        foreach ($this->bootstrapService->getModels() as $model) {
146 17
            if (classImplementsInterface($model, Cacheable::class) && (bool)config('model.caching')) {
147 17
                $model::observe(CacheObserver::class);
148
            }
149
        }
150 17
    }
151
152 17
    private function loadOwnershipPolicies()
153
    {
154 17
        foreach ($this->bootstrapService->getModels() as $model) {
155 17
            if (classImplementsInterface($model, Ownable::class)) {
156 17
                Gate::policy($model, OwnershipPolicy::class);
157 17
                Gate::define('access', OwnershipPolicy::class . '@access');
158
            }
159
        }
160 17
    }
161
}
162