Test Failed
Push — master ( 93ef3c...0c96ba )
by Arthur
09:20
created

BootstrapServiceProvider::loadFactories()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 0
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 12
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->loadV1Routes();
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 (!$this->app->environment('production')) {
40
            $this->bootstrapService->cache();
41
        }
42
    }
43
44
    private function loadCommands()
45
    {
46
        $this->commands($this->bootstrapService->getCommands());
47
    }
48
49
    private function loadV1Routes()
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
            $moduleNamespace = $route[1];
56
            $moduleName = explode('\\', $moduleNamespace)[1];
57
            $controllerNamespace = $moduleNamespace . '\\' . 'Http\\Controllers';
58
            $modelNameSpace = $moduleNamespace . '\\' . 'Entities\\' . $moduleName;
59
            $filepath = $route[0];
60
            Route::group([
61
                'prefix' => 'v1',
62
                'namespace' => $controllerNamespace,
63
                'domain' => $apiDomain,
64
                'middleware' => ['api'],
65
            ], function (Router $router) use ($filepath) {
66
                require $filepath;
67
            });
68
            Route::model(strtolower($moduleName), $modelNameSpace);
69
        }
70
    }
71
72
    /**
73
     * Register config.
74
     *
75
     * @return void
76
     */
77
    protected function loadConfigs()
78
    {
79
        foreach ($this->bootstrapService->getConfigs() as $route) {
80
            $this->publishes([
81
                $route[0] => config_path($route[1]),
82
            ], 'config');
83
            $this->mergeConfigFrom(
84
                $route[0], basename($route[1], '.php')
85
            );
86
        }
87
    }
88
89
    /**
90
     * Register additional directories of factories.
91
     *
92
     * @return void
93
     */
94
    public function loadFactories()
95
    {
96
        foreach ($this->bootstrapService->getFactories() as $factoryPath) {
97
            if (!$this->app->environment('production')) {
98
                app(Factory::class)->load($factoryPath);
99
            }
100
        }
101
    }
102
103
    /**
104
     * Register additional directories of migrations.
105
     *
106
     * @return void
107
     */
108
    public function loadMigrations()
109
    {
110
        foreach ($this->bootstrapService->getMigrations() as $migrationPath) {
111
            $this->loadMigrationsFrom($migrationPath);
112
        }
113
    }
114
115
    private function loadPolicies()
116
    {
117
        //TODO
118
    }
119
120
    private function overrideSeedCommand()
121
    {
122
        $app = $this->app;
123
        $service = $this->bootstrapService;
124
        $this->app->extend('command.seed', function () use ($app, $service) {
125
            return new SeedCommand($app['db'], $service);
126
        });
127
    }
128
}
129