Passed
Push — dependabot/github_actions/depe... ( d1016c )
by
unknown
11:16
created

MigrateMakeCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Console\Migrations;
4
5
use Illuminate\Database\Console\Migrations\MigrateMakeCommand as IlluminateMigrateMakeCommand;
6
use Illuminate\Database\Console\Migrations\TableGuesser;
7
use Illuminate\Support\Composer;
8
use Illuminate\Support\Str;
9
use LaravelFreelancerNL\Aranguent\Migrations\MigrationCreator;
10
11
class MigrateMakeCommand extends IlluminateMigrateMakeCommand
12
{
13
    /**
14
     * The console command signature.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'make:migration {name : The name of the migration}
19
        {--create= : The collection to be created}
20
        {--collection= : The collection to migrate}
21
        {--table= : (Alias for collection)}
22
        {--path= : The location where the migration file should be created}
23
        {--realpath : Indicate any provided migration file paths are pre-resolved absolute paths}';
24
25
    /**
26
     * Create a new migration install command instance.
27
     *
28
     * @param  MigrationCreator  $creator
29
     * @param  Composer  $composer
30
     */
31 217
    public function __construct(MigrationCreator $creator, Composer $composer)
32
    {
33 217
        $this->creator = $creator;
34 217
        $this->composer = $composer;
35
36 217
        parent::__construct($creator, $composer);
37
    }
38
39
    /**
40
     * Execute the console command.
41
     *
42
     * @return void
43
     *
44
     * @throws \Exception
45
     */
46
    public function handle()
47
    {
48
        // It's possible for the developer to specify the tables to modify in this
49
        // schema operation. The developer may also specify if this table needs
50
        // to be freshly created so we can create the appropriate migrations.
51
        $name = Str::snake(trim($this->input->getArgument('name')));
52
53
        $collection = $this->input->getOption('collection');
54
        if (! $collection) {
55
            $collection = $this->input->getOption('table');
56
        }
57
58
        $create = $this->input->getOption('create') ?: false;
59
60
        // If no table was given as an option but a create option is given then we
61
        // will use the "create" option as the table name. This allows the devs
62
        // to pass a table name into this option as a short-cut for creating.
63
        if (! $collection && is_string($create)) {
64
            $collection = $create;
65
66
            $create = true;
67
        }
68
69
        // Next, we will attempt to guess the table name if this the migration has
70
        // "create" in the name. This will allow us to provide a convenient way
71
        // of creating migrations that create new tables for the application.
72
        if (! $collection) {
73
            [$collection, $create] = TableGuesser::guess($name);
74
        }
75
76
        // Now we are ready to write the migration out to disk. Once we've written
77
        // the migration out, we will dump-autoload for the entire framework to
78
        // make sure that the migrations are registered by the class loaders.
79
        $this->writeMigration($name, $collection, $create);
80
81
        $this->composer->dumpAutoloads();
82
    }
83
84
    /**
85
     * Write the migration file to disk.
86
     *
87
     * @param  string  $name
88
     * @param  string  $collection
89
     * @param  bool  $create
90
     * @return void
91
     *
92
     * @throws \Exception
93
     */
94
    protected function writeMigration($name, $collection, $create)
95
    {
96
        $file = pathinfo($this->creator->create(
97
            $name,
98
            $this->getMigrationPath(),
99
            $collection,
100
            $create
101
        ), PATHINFO_FILENAME);
102
103
        $this->line("<info>Created Migration:</info> {$file}");
104
    }
105
}
106