Passed
Push — master ( 7af546...dfd36b )
by Arthur
04:54 queued 15s
created

BootstrapServiceProvider::overrideSeedCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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