Passed
Push — master ( c21d64...956340 )
by Arthur
05:06
created

BootstrapServiceProvider::loadFactories()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 3
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 5
    public function register()
23
    {
24 5
        $this->loadBootstrapService();
25 5
        $this->loadCommands();
26 5
        $this->loadPolicies();
27 5
        $this->loadRoutes();
28 5
        $this->loadConfigs();
29 5
        $this->loadFactories();
30 5
        $this->loadMigrations();
31
32 5
        $this->overrideSeedCommand();
33 5
    }
34
35 5
    public function loadBootstrapService()
36
    {
37 5
        $this->bootstrapService = new BootstrapRegistrarService();
38
39 5
        if (!$this->app->environment('production')) {
40 5
            $this->bootstrapService->recache();
41
        }
42 5
    }
43
44 5
    private function loadCommands()
45
    {
46 5
        $this->commands($this->bootstrapService->getCommands());
47 5
    }
48
49 5
    private function loadRoutes()
50
    {
51 5
        foreach ($this->bootstrapService->getRoutes() as $route) {
52 5
            $path = $route['path'];
53 5
            Route::group([
54 5
                'prefix' => 'v1/' . $route['module'],
55 5
                'namespace' => $route['controller'],
56 5
                'domain' => $route['domain'],
57
                'middleware' => ['api'],
58
            ], function (Router $router) use ($path) {
59 5
                require $path;
60 5
            });
61 5
            Route::model($route['module'], $route['model']);
62
        }
63 5
    }
64
65
    /**
66
     * Register config.
67
     *
68
     * @return void
69
     */
70 5
    protected function loadConfigs()
71
    {
72 5
        foreach ($this->bootstrapService->getConfigs() as $config) {
73 5
            $this->publishes([
74 5
                $config['path'] => config_path($config['module']),
75 5
            ], 'config');
76 5
            $this->mergeConfigFrom(
77 5
                $config['path'], basename($config['module'], '.php')
78
            );
79
        }
80 5
    }
81
82
    /**
83
     * Register additional directories of factories.
84
     *
85
     * @return void
86
     */
87 5
    public function loadFactories()
88
    {
89 5
        foreach ($this->bootstrapService->getFactories() as $factory) {
90 5
            if (!$this->app->environment('production')) {
91 5
                app(Factory::class)->load($factory['path']);
92
            }
93
        }
94 5
    }
95
96
    /**
97
     * Register additional directories of migrations.
98
     *
99
     * @return void
100
     */
101 5
    public function loadMigrations()
102
    {
103 5
        foreach ($this->bootstrapService->getMigrations() as $migration) {
104 5
            $this->loadMigrationsFrom($migration['path']);
105
        }
106 5
    }
107
108 5
    private function loadPolicies()
109
    {
110
        //TODO
111 5
    }
112
113 5
    private function overrideSeedCommand()
114
    {
115 5
        $app = $this->app;
116 5
        $service = $this->bootstrapService;
117
        $this->app->extend('command.seed', function () use ($app, $service) {
118
            return new SeedCommand($app['db'], $service);
119 5
        });
120 5
    }
121
}
122