Passed
Push — master ( 660c80...17f60a )
by Arthur
08:30
created

BootstrapServiceProvider::loadModulePolicies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 1
cp 0
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\Services\BootstrapRegistrarService;
7
use Illuminate\Database\Eloquent\Factory;
8
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
9
use Illuminate\Routing\Router;
10
use Route;
11
12
/**
13
 * Class BootstrapServiceProvider.
14
 */
15
class BootstrapServiceProvider extends ServiceProvider
16
{
17
    /**
18
     * @var BootstrapRegistrarService
19
     */
20
    protected $bootstrapService;
21
22
    public function register()
23
    {
24
        $this->loadBootstrapService();
25
        $this->loadCommands();
26
        $this->loadPolicies();
27
        $this->loadRoutes();
28
        $this->loadConfigs();
29
        $this->loadFactories();
30
        $this->loadMigrations();
31
32
        $this->overrideSeedCommand();
33
    }
34
35
    public function loadBootstrapService()
36
    {
37
        $this->bootstrapService = new BootstrapRegistrarService();
38
39
        if (env('APP_ENV') !== 'production') {
40
            $this->bootstrapService->cache();
41
        }
42
    }
43
44
    private function loadCommands()
45
    {
46
        $this->commands($this->bootstrapService->getCommands());
47
    }
48
49
    private function loadRoutes()
50
    {
51
        foreach ($this->bootstrapService->getRoutes() as $route) {
52
            $apiDomain = strtolower(env('API_URL'));
53
            $apiDomain = str_replace('http://', '', $apiDomain);
54
            $apiDomain = str_replace('https://', '', $apiDomain);
55
            $namespace = $route[1] . '\\' . 'Http\\Controllers';
56
            $filepath = $route[0];
57
            Route::group([
58
                'prefix' => 'v1',
59
                'namespace' => $namespace,
60
                'domain' => $apiDomain,
61
                'middleware' => []
62
            ], function (Router $router) use ($filepath) {
63
                require $filepath;
64
            });
65
        }
66
    }
67
68
    /**
69
     * Register config.
70
     *
71
     * @return void
72
     */
73
    protected function loadConfigs()
74
    {
75
        foreach ($this->bootstrapService->getConfigs() as $route) {
76
            $this->publishes([
77
                $route[0] => config_path($route[1]),
78
            ], 'config');
79
            $this->mergeConfigFrom(
80
                $route[0], basename($route[1], '.php')
81
            );
82
        }
83
    }
84
85
    /**
86
     * Register additional directories of factories.
87
     *
88
     * @return void
89
     */
90
    public function loadFactories()
91
    {
92
        foreach ($this->bootstrapService->getFactories() as $factoryPath) {
93
            if (!app()->environment('production')) {
0 ignored issues
show
introduced by
The method environment() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
            if (!app()->/** @scrutinizer ignore-call */ environment('production')) {
Loading history...
94
                app(Factory::class)->load($factoryPath);
95
            }
96
        }
97
98
    }
99
100
    /**
101
     * Register additional directories of migrations.
102
     *
103
     * @return void
104
     */
105
    public function loadMigrations()
106
    {
107
        foreach ($this->bootstrapService->getMigrations() as $migrationPath) {
108
            $this->loadMigrationsFrom($migrationPath);
109
        }
110
111
    }
112
113
    private function loadPolicies()
114
    {
115
        //TODO
116
    }
117
118
    private function overrideSeedCommand()
119
    {
120
        $app = $this->app;
121
        $service = $this->bootstrapService;
122
        $this->app->extend('command.seed', function () use ($app, $service) {
123
            return new SeedCommand($app['db'], $service);
124
        });
125
    }
126
}
127