Passed
Branch master (512d52)
by Bas
08:47
created

MigrateMakeCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 21
c 1
b 0
f 0
dl 0
loc 87
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A writeMigration() 0 7 1
A handle() 0 36 6
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
     * @param MigrationCreator $creator
28
     * @param Composer $composer
29
     */
30
    public function __construct(MigrationCreator $creator, Composer $composer)
31
    {
32
        $this->creator = $creator;
33
        $this->composer = $composer;
34
35
        parent::__construct($creator, $composer);
36
    }
37
38
    /**
39
     * Execute the console command.
40
     *
41
     * @return void
42
     * @throws \Exception
43
     */
44
    public function handle()
45
    {
46
        // It's possible for the developer to specify the tables to modify in this
47
        // schema operation. The developer may also specify if this table needs
48
        // to be freshly created so we can create the appropriate migrations.
49
        $name = Str::snake(trim($this->input->getArgument('name')));
0 ignored issues
show
Bug introduced by
It seems like $this->input->getArgument('name') can also be of type string[]; however, parameter $str of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

49
        $name = Str::snake(trim(/** @scrutinizer ignore-type */ $this->input->getArgument('name')));
Loading history...
50
51
        $collection = $this->input->getOption('collection');
52
        if (! $collection) {
53
            $collection = $this->input->getOption('table');
54
        }
55
56
        $create = $this->input->getOption('create') ?: false;
57
58
        // If no table was given as an option but a create option is given then we
59
        // will use the "create" option as the table name. This allows the devs
60
        // to pass a table name into this option as a short-cut for creating.
61
        if (! $collection && is_string($create)) {
62
            $collection = $create;
63
64
            $create = true;
65
        }
66
67
        // Next, we will attempt to guess the table name if this the migration has
68
        // "create" in the name. This will allow us to provide a convenient way
69
        // of creating migrations that create new tables for the application.
70
        if (! $collection) {
71
            [$collection, $create] = TableGuesser::guess($name);
72
        }
73
74
        // Now we are ready to write the migration out to disk. Once we've written
75
        // the migration out, we will dump-autoload for the entire framework to
76
        // make sure that the migrations are registered by the class loaders.
77
        $this->writeMigration($name, $collection, $create);
78
79
        $this->composer->dumpAutoloads();
80
    }
81
82
    /**
83
     * Write the migration file to disk.
84
     *
85
     * @param string $name
86
     * @param string $collection
87
     * @param bool $create
88
     * @return void
89
     * @throws \Exception
90
     */
91
    protected function writeMigration($name, $collection, $create)
92
    {
93
        $file = pathinfo($this->creator->create(
94
            $name, $this->getMigrationPath(), $collection, $create
95
        ), PATHINFO_FILENAME);
96
97
        $this->line("<info>Created Migration:</info> {$file}");
98
    }
99
}
100