MigrateMakeCommand   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 137
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A handle() 0 34 5
A writeMigration() 0 12 2
A getMigrationPath() 0 10 3
A usingRealPath() 0 4 2
1
<?php
2
3
namespace JunaidQadirB\Cray\Console\Commands;
4
5
6
use Illuminate\Database\Console\Migrations\BaseCommand;
7
use Illuminate\Database\Migrations\MigrationCreator;
8
use Illuminate\Support\Composer;
9
use Illuminate\Support\Str;
10
11
class MigrateMakeCommand extends BaseCommand
12
{
13
    /**
14
     * The console command signature.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'cray:migration {name : The name of the migration.}
19
        {--create= : The table to be created.}
20
        {--table= : The table to migrate.}
21
        {--path= : The location where the migration file should be created.}
22
        {--realpath : Indicate any provided migration file paths are pre-resolved absolute paths.}
23
        {--fullpath : Output the full path of the migration}';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Create a new migration file';
31
32
    /**
33
     * The migration creator instance.
34
     *
35
     * @var \Illuminate\Database\Migrations\MigrationCreator
36
     */
37
    protected $creator;
38
39
    /**
40
     * The Composer instance.
41
     *
42
     * @var \Illuminate\Support\Composer
43
     */
44
    protected $composer;
45
46
    /**
47
     * Create a new migration install command instance.
48
     *
49
     * @param  \Illuminate\Database\Migrations\MigrationCreator  $creator
50
     * @param  \Illuminate\Support\Composer  $composer
51
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
52
     */
53
    public function __construct(MigrationCreator $creator, Composer $composer)
54
    {
55
        parent::__construct();
56
57
        $this->creator = $creator;
58
        $this->composer = $composer;
59
    }
60
61
    /**
62
     * Execute the console command.
63
     *
64
     * @return void
65
     */
66
    public function handle()
67
    {
68
        // It's possible for the developer to specify the tables to modify in this
69
        // schema operation. The developer may also specify if this table needs
70
        // to be freshly created so we can create the appropriate migrations.
71
        $name = Str::snake(trim($this->input->getArgument('name')));
72
73
        $table = $this->input->getOption('table');
74
75
        $create = $this->input->getOption('create') ?: false;
76
77
        // If no table was given as an option but a create option is given then we
78
        // will use the "create" option as the table name. This allows the devs
79
        // to pass a table name into this option as a short-cut for creating.
80
        if (! $table && is_string($create)) {
81
            $table = $create;
82
83
            $create = true;
84
        }
85
86
        // Next, we will attempt to guess the table name if this the migration has
87
        // "create" in the name. This will allow us to provide a convenient way
88
        // of creating migrations that create new tables for the application.
89
        if (! $table) {
90
            [$table, $create] = TableGuesser::guess($name);
91
        }
92
93
        // Now we are ready to write the migration out to disk. Once we've written
94
        // the migration out, we will dump-autoload for the entire framework to
95
        // make sure that the migrations are registered by the class loaders.
96
        $this->writeMigration($name, $table, $create);
97
98
        $this->composer->dumpAutoloads();
99
    }
100
101
    /**
102
     * Write the migration file to disk.
103
     *
104
     * @param  string  $name
105
     * @param  string  $table
106
     * @param  bool  $create
107
     * @return string
108
     */
109
    protected function writeMigration($name, $table, $create)
110
    {
111
        $file = $this->creator->create(
112
            $name, $this->getMigrationPath(), $table, $create
113
        );
114
115
        if (! $this->option('fullpath')) {
116
            $file = pathinfo($file, PATHINFO_FILENAME);
117
        }
118
119
        $this->line("<info>Created Migration:</info> {$file}");
120
    }
121
122
    /**
123
     * Get migration path (either specified by '--path' option or default location).
124
     *
125
     * @return string
126
     */
127
    protected function getMigrationPath()
128
    {
129
        if (! is_null($targetPath = $this->input->getOption('path'))) {
130
            return ! $this->usingRealPath()
131
                            ? $this->laravel->basePath().'/'.$targetPath
132
                            : $targetPath;
133
        }
134
135
        return parent::getMigrationPath();
136
    }
137
138
    /**
139
     * Determine if the given path(s) are pre-resolved "real" paths.
140
     *
141
     * @return bool
142
     */
143
    protected function usingRealPath()
144
    {
145
        return $this->input->hasOption('realpath') && $this->option('realpath');
146
    }
147
}
148