Failed Conditions
Branch feature/streamline-console-com... (d385c8)
by Bas
11:19
created

MigrateMakeCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Console\Migrations;
6
7
use Exception;
8
use Illuminate\Database\Console\Migrations\MigrateMakeCommand as IlluminateMigrateMakeCommand;
9
use Illuminate\Database\Console\Migrations\TableGuesser;
10
use Illuminate\Support\Composer;
11
use Illuminate\Support\Str;
12
use LaravelFreelancerNL\Aranguent\Migrations\MigrationCreator;
13
14
class MigrateMakeCommand extends IlluminateMigrateMakeCommand
15
{
16
    /**
17
     * The console command signature.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'make:migration {name : The name of the migration}
22
        {--create= : The table to be created}
23
        {--edge= : The edge collection to be created}
24
        {--table= : The table to alter}
25
        {--path= : The location where the migration file should be created}
26
        {--realpath : Indicate any provided migration file paths are pre-resolved absolute paths}
27
        {--fullpath : Output the full path of the migration (Deprecated)}';
28
29
    /**
30
     * Create a new migration install command instance.
31
     */
32
    public function __construct(MigrationCreator $creator, Composer $composer)
33
    {
34
        $this->creator = $creator;
35
        $this->composer = $composer;
0 ignored issues
show
Deprecated Code introduced by
The property Illuminate\Database\Cons...eMakeCommand::$composer has been deprecated: Will be removed in a future Laravel version. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

35
        /** @scrutinizer ignore-deprecated */ $this->composer = $composer;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
36
37
        parent::__construct($creator, $composer);
38
    }
39
40
    /**
41
     * Execute the console command.
42
     *
43
     * @return void
44
     *
45
     * @throws Exception
46
     */
47
    public function handle()
48
    {
49
        // It's possible for the developer to specify the tables to modify in this
50
        // schema operation. The developer may also specify if this table needs
51
        // to be freshly created so we can create the appropriate migrations.
52
        $name = Str::snake(trim((string) $this->input->getArgument('name')));
53
54
        $table = $this->input->getOption('table');
55
56
        $create = $this->input->getOption('create') ?: false;
57
58
        $edge = $this->input->getOption('edge') ?: 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 (!$table && is_string($create)) {
64
            $table = $create;
65
66
            $create = true;
67
        }
68
69
        if (!$table && is_string($edge)) {
70
            $table = $create;
71
72
            $edge = true;
73
        }
74
75
        // Next, we will attempt to guess the table name if this the migration has
76
        // "create" in the name. This will allow us to provide a convenient way
77
        // of creating migrations that create new tables for the application.
78
        if (!$table) {
79
            [$table, $create] = TableGuesser::guess($name);
80
        }
81
82
        // Now we are ready to write the migration out to disk. Once we've written
83
        // the migration out, we will dump-autoload for the entire framework to
84
        // make sure that the migrations are registered by the class loaders.
85
        $this->writeMigration($name, $table, $create, $edge);
86
87
        $this->composer->dumpAutoloads();
0 ignored issues
show
Deprecated Code introduced by
The property Illuminate\Database\Cons...eMakeCommand::$composer has been deprecated: Will be removed in a future Laravel version. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

87
        /** @scrutinizer ignore-deprecated */ $this->composer->dumpAutoloads();

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
88
    }
89
90
    /**
91
     * Write the migration file to disk.
92
     *
93
     * @param  string  $name
94
     * @param  string  $table
95
     * @param  bool  $create
96
     * @param  bool  $edge
97
     * @return void
98
     *
99
     * @throws Exception
100
     *
101
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
102
     */
103
    protected function writeMigration($name, $table, $create, $edge = false)
104
    {
105
        assert($this->creator instanceof MigrationCreator);
106
107
        $file = pathinfo(
108
            $this->creator->create(
109
                $name,
110
                $this->getMigrationPath(),
111
                $table,
112
                $create,
113
                $edge
114
            ),
115
            PATHINFO_FILENAME
116
        );
117
118
        $this->line("<info>Created Migration:</info> {$file}");
119
    }
120
}
121