GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ModuleMigrateResetCommand::getArguments()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Caffeinated\Modules\Console\Commands;
4
5
use Caffeinated\Modules\Modules;
6
use Illuminate\Console\ConfirmableTrait;
7
use Illuminate\Filesystem\Filesystem;
8
use Illuminate\Database\Migrations\Migrator;
9
use Illuminate\Console\Command;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Input\InputArgument;
12
13
class ModuleMigrateResetCommand extends Command
14
{
15
    use ConfirmableTrait;
16
17
    /**
18
     * The console command name.
19
     *
20
     * @var string
21
     */
22
    protected $name = 'module:migrate:reset';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Rollback all database migrations for a specific or all modules';
30
31
    /**
32
     * @var Modules
33
     */
34
    protected $module;
35
36
    /**
37
     * @var Migrator
38
     */
39
    protected $migrator;
40
41
    /**
42
     * @var Filesystem
43
     */
44
    protected $files;
45
46
    /**
47
     * Create a new command instance.
48
     *
49
     * @param Modules    $module
50
     * @param Filesystem $files
51
     * @param Migrator   $migrator
52
     */
53
    public function __construct(Modules $module, Filesystem $files, Migrator $migrator)
54
    {
55
        parent::__construct();
56
57
        $this->module   = $module;
58
        $this->files    = $files;
59
        $this->migrator = $migrator;
60
    }
61
62
    /**
63
     * Execute the console command.
64
     *
65
     * @return mixed
66
     */
67 View Code Duplication
    public function fire()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        if (!$this->confirmToProceed()) {
70
            return;
71
        }
72
73
        $slug = $this->argument('slug');
74
75
        if (!empty($slug)) {
76
            if ($this->module->isEnabled($slug)) {
77
                return $this->reset($slug);
78
            } elseif ($this->option('force')) {
79
                return $this->reset($slug);
80
            }
81
        } else {
82
            if ($this->option('force')) {
83
                $modules = $this->module->all()->reverse();
84
            } else {
85
                $modules = $this->module->enabled()->reverse();
86
            }
87
88
            foreach ($modules as $module) {
89
                $this->reset($module['slug']);
90
            }
91
        }
92
    }
93
94
    /**
95
     * Run the migration reset for the specified module.
96
     *
97
     * Migrations should be reset in the reverse order that they were
98
     * migrated up as. This ensures the database is properly reversed
99
     * without conflict.
100
     *
101
     * @param string $slug
102
     *
103
     * @return mixed
104
     */
105
    protected function reset($slug)
106
    {
107
        $this->migrator->setconnection($this->input->getOption('database'));
108
109
        $pretend       = $this->input->getOption('pretend');
110
        $migrationPath = $this->getMigrationPath($slug);
111
        $migrations    = array_reverse($this->migrator->getMigrationFiles($migrationPath));
112
113
        if (count($migrations) == 0) {
114
            return $this->error('Nothing to rollback.');
115
        }
116
117
        foreach ($migrations as $migration) {
118
            $this->info('Migration: '.$migration);
119
            $this->runDown($slug, $migration, $pretend);
120
        }
121
    }
122
123
    /**
124
     * Run "down" a migration instance.
125
     *
126
     * @param string $slug
127
     * @param object $migration
128
     * @param bool   $pretend
129
     */
130
    protected function runDown($slug, $migration, $pretend)
131
    {
132
        $migrationPath = $this->getMigrationPath($slug);
133
        $file          = (string) $migrationPath.'/'.$migration.'.php';
134
        $classFile     = implode('_', array_slice(explode('_', basename($file, '.php')), 4));
135
        $class         = studly_case($classFile);
136
        $table         = $this->laravel['config']['database.migrations'];
137
138
        include $file;
139
140
        $instance = new $class();
141
        $instance->down();
142
143
        $this->laravel['db']->table($table)
144
            ->where('migration', $migration)
145
            ->delete();
146
    }
147
148
    /**
149
     * Get the console command parameters.
150
     *
151
     * @param string $slug
152
     *
153
     * @return array
154
     */
155
    protected function getParameters($slug)
156
    {
157
        $params = [];
158
159
        $params['--path'] = $this->getMigrationPath($slug);
160
161
        if ($option = $this->option('database')) {
162
            $params['--database'] = $option;
163
        }
164
165
        if ($option = $this->option('pretend')) {
166
            $params['--pretend'] = $option;
167
        }
168
169
        if ($option = $this->option('seed')) {
170
            $params['--seed'] = $option;
171
        }
172
173
        return $params;
174
    }
175
176
    /**
177
     * Get migrations path.
178
     *
179
     * @return string
180
     */
181
    protected function getMigrationPath($slug)
182
    {
183
        $path = $this->module->getModulePath($slug).'Database/Migrations';
184
185
        return $path;
186
    }
187
188
    /**
189
     * Get the console command arguments.
190
     *
191
     * @return array
192
     */
193
    protected function getArguments()
194
    {
195
        return [['slug', InputArgument::OPTIONAL, 'Module slug.']];
196
    }
197
198
    /**
199
     * Get the console command options.
200
     *
201
     * @return array
202
     */
203 View Code Duplication
    protected function getOptions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
204
    {
205
        return [
206
            ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
207
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run while in production.'],
208
            ['pretend', null, InputOption::VALUE_OPTIONAL, 'Dump the SQL queries that would be run.'],
209
            ['seed', null, InputOption::VALUE_OPTIONAL, 'Indicates if the seed task should be re-run.'],
210
        ];
211
    }
212
}
213