Passed
Push — next ( cade46...f034dd )
by Bas
05:34 queued 02:21
created

MigrationServiceProvider   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Test Coverage

Coverage 80.85%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
dl 0
loc 143
ccs 38
cts 47
cp 0.8085
rs 10
c 1
b 0
f 0
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 9 1
A registerCreator() 0 6 1
A boot() 0 6 2
A __construct() 0 6 2
A registerMigrationsConvertCommand() 0 4 1
A registerRepository() 0 6 1
A registerMigrateMakeCommand() 0 9 1
A provides() 0 3 1
A registerMigrator() 0 9 1
A registerMigrateInstallCommand() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Providers;
6
7
use Illuminate\Database\Migrations\Migrator;
8
use Illuminate\Database\MigrationServiceProvider as IlluminateMigrationServiceProvider;
9
use LaravelFreelancerNL\Aranguent\Console\Concerns\ArangoCommands;
10
use LaravelFreelancerNL\Aranguent\Console\Migrations\MigrationsConvertCommand;
11
use LaravelFreelancerNL\Aranguent\Console\Migrations\MigrateMakeCommand;
12
use LaravelFreelancerNL\Aranguent\Console\Migrations\MigrateInstallCommand;
13
use LaravelFreelancerNL\Aranguent\Migrations\DatabaseMigrationRepository;
14
use LaravelFreelancerNL\Aranguent\Migrations\MigrationCreator;
15
16
class MigrationServiceProvider extends IlluminateMigrationServiceProvider
17
{
18
    use ArangoCommands;
19
20
    protected bool $defer = true;
21
22
    /**
23
     * @var string[]
24
     */
25
    protected $aliases = [
26
        'Migrator' => 'migrator',
27
        'Creator' => 'migration.creator',
28
        'Repository' => 'migration.repository',
29
        'MigrateMake' => 'migrate.make',
30
        'MigrateInstall' => MigrateInstallCommand::class,
31
    ];
32
33
    /**
34
     * Create a new service provider instance.
35
     *
36
     * @param  \Illuminate\Contracts\Foundation\Application  $app
37
     * @return void
38
     */
39 343
    public function __construct($app)
40
    {
41 343
        parent::__construct($app);
42
43 343
        foreach($this->aliases as $key => $alias) {
44 343
            $this->aliases[$key] = $alias;
45
        }
46
    }
47
48 343
    public function boot(): void
49
    {
50 343
        if ($this->app->runningInConsole()) {
51 343
            $this->commands([
52 343
                MigrateMakeCommand::class,
53 343
                MigrationsConvertCommand::class
54 343
            ]);
55
        }
56
    }
57
58
    /**
59
     * Register the service provider.
60
     *
61
     * @return void
62
     */
63 343
    public function register()
64
    {
65 343
        $this->registerRepository();
66
67 343
        $this->registerMigrator();
68
69 343
        $this->registerCreator();
70
71 343
        $this->registerCommands($this->commands);
72
    }
73
74
    /**
75
     * Register the migration repository service.
76
     *
77
     * @return void
78
     */
79 343
    protected function registerRepository()
80
    {
81 343
        $this->app->singleton($this->aliases['Repository'], function ($app) {
82 343
            $table = $app['config']['database.migrations'];
83
84 343
            return new DatabaseMigrationRepository($app['db'], $table);
85 343
        });
86
    }
87
88
    /**
89
     * Register the migrator service.
90
     *
91
     * @return void
92
     */
93 343
    protected function registerMigrator()
94
    {
95
        // The migrator is responsible for actually running and rollback the migration
96
        // files in the application. We'll pass in our database connection resolver
97
        // so the migrator can resolve any of these connections when it needs to.
98 343
        $this->app->singleton($this->aliases['Migrator'], function ($app) {
99 343
            $repository = $app[$this->aliases['Repository']];
100
101 343
            return new Migrator($repository, $app['db'], $app['files'], $app['events']);
102 343
        });
103
    }
104
105 343
    protected function registerCreator()
106
    {
107 343
        $this->app->singleton($this->aliases['Creator'], function ($app) {
108 343
            $customStubPath = __DIR__ . '/../Migrations/stubs';
109
110 343
            return new MigrationCreator($app['files'], $customStubPath);
111 343
        });
112
    }
113
114
    /**
115
     * Register the command.
116
     *
117
     * @return void
118
     */
119 343
    protected function registerMigrateMakeCommand()
120
    {
121
122 343
        $this->app->singleton($this->commands['MigrateMake'], function ($app) {
123
            $creator = $app['migration.creator'];
124
125
            $composer = $app['composer'];
126
127
            return new MigrateMakeCommand($creator, $composer);
128 343
        });
129
    }
130
131
    /**
132
     * Register the command.
133
     *
134
     * @return void
135
     */
136 343
    protected function registerMigrateInstallCommand()
137
    {
138
139 343
        $this->app->singleton($this->commands['MigrateInstall'], function ($app) {
140 343
            $repository = $app[$this->aliases['Repository']];
141
142 343
            return new MigrateInstallCommand($repository);
143 343
        });
144
    }
145
146
    protected function registerMigrationsConvertCommand(): void
147
    {
148
        $this->app->singleton($this->commands['MigrationsConvert'], function ($app) {
149
            return new MigrationsConvertCommand($app['migrator']);
150
        });
151
    }
152
153
    /**
154
     * @return string[]
155
     */
156
    public function provides()
157
    {
158
        return array_values($this->aliases);
159
    }
160
}
161