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.
Completed
Push — master ( 00b768...e1065b )
by Shea
02:28
created

ModuleMigrateResetCommand::getArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Caffeinated\Modules\Console\Commands;
3
4
use Caffeinated\Modules\Modules;
5
use Illuminate\Console\ConfirmableTrait;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Database\Migrations\Migrator;
8
use Illuminate\Console\Command;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Input\InputArgument;
11
12
class ModuleMigrateResetCommand extends Command
13
{
14
    use ConfirmableTrait;
15
16
    /**
17
     * The console command name.
18
     *
19
     * @var string
20
     */
21
	protected $name = 'module:migrate:reset';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
	protected $description = 'Rollback all database migrations for a specific or all modules';
29
30
	/**
31
	 * @var Modules
32
	 */
33
	protected $module;
34
35
	/**
36
	 * @var Migrator
37
	 */
38
	protected $migrator;
39
40
	/**
41
	 * @var Filesystem
42
	 */
43
	protected $files;
44
45
	/**
46
	 * Create a new command instance.
47
	 *
48
	 * @param Modules  $module
49
	 * @param Filesystem  $files
50
	 * @param Migrator  $migrator
51
	 */
52
	public function __construct(Modules $module, Filesystem $files, Migrator $migrator)
53
	{
54
		parent::__construct();
55
56
		$this->module   = $module;
57
		$this->files    = $files;
58
		$this->migrator = $migrator;
59
	}
60
61
	/**
62
	 * Execute the console command.
63
	 *
64
	 * @return mixed
65
	 */
66
	public function fire()
67
	{
68
        if (! $this->confirmToProceed()) return null;
69
70
		$slug = $this->argument('slug');
71
72
		if (! empty($slug)) {
73
			if ($this->module->isEnabled($slug)) {
74
				return $this->reset($slug);
75
			} elseif ($this->option('force')) {
76
				return $this->reset($slug);
77
			}
78 View Code Duplication
		} else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
79
			if ($this->option('force')) {
80
				$modules = $this->module->all()->reverse();
81
			} else {
82
				$modules = $this->module->enabled()->reverse();
83
			}
84
85
			foreach ($modules as $module) {
86
				$this->reset($module['slug']);
87
			}
88
		}
89
	}
90
91
	/**
92
	 * Run the migration reset for the specified module.
93
	 *
94
	 * Migrations should be reset in the reverse order that they were
95
	 * migrated up as. This ensures the database is properly reversed
96
	 * without conflict.
97
	 *
98
	 * @param  string $slug
99
	 * @return mixed
100
	 */
101
	protected function reset($slug)
102
	{
103
		$this->migrator->setconnection($this->input->getOption('database'));
104
105
		$pretend       = $this->input->getOption('pretend');
106
		$migrationPath = $this->getMigrationPath($slug);
107
		$migrations    = array_reverse($this->migrator->getMigrationFiles($migrationPath));
108
109
		if (count($migrations) == 0) {
110
			return $this->error('Nothing to rollback.');
111
		}
112
113
		foreach ($migrations as $migration) {
114
			$this->info('Migration: '.$migration);
115
			$this->runDown($slug, $migration, $pretend);
116
		}
117
	}
118
119
	/**
120
	 * Run "down" a migration instance.
121
	 *
122
	 * @param  string $slug
123
	 * @param  object $migration
124
	 * @param  bool   $pretend
125
	 * @return void
126
	 */
127
	protected function runDown($slug, $migration, $pretend)
128
	{
129
		$migrationPath = $this->getMigrationPath($slug);
130
		$file          = (string) $migrationPath.'/'.$migration.'.php';
131
		$classFile     = implode('_', array_slice(explode('_', basename($file, '.php')), 4));
132
		$class         = studly_case($classFile);
133
		$table         = $this->laravel['config']['database.migrations'];
134
135
		include ($file);
136
137
		$instance = new $class;
138
		$instance->down();
139
140
		$this->laravel['db']->table($table)
141
			->where('migration', $migration)
142
			->delete();
143
	}
144
145
	/**
146
	 * Get the console command parameters.
147
	 *
148
	 * @param  string $slug
149
	 * @return array
150
	 */
151
	protected function getParameters($slug)
152
	{
153
		$params = [];
154
155
		$params['--path'] = $this->getMigrationPath($slug);
156
157
		if ($option = $this->option('database')) {
158
			$params['--database'] = $option;
159
		}
160
161
		if ($option = $this->option('pretend')) {
162
			$params['--pretend'] = $option;
163
		}
164
165
		if ($option = $this->option('seed')) {
166
			$params['--seed'] = $option;
167
		}
168
169
		return $params;
170
	}
171
172
	/**
173
	 * Get migrations path.
174
	 *
175
	 * @return string
176
	 */
177
	protected function getMigrationPath($slug)
178
	{
179
		$path = $this->module->getModulePath($slug).'Database/Migrations';
180
181
		return $path;
182
	}
183
184
	/**
185
	 * Get the console command arguments.
186
	 *
187
	 * @return array
188
	 */
189
	protected function getArguments()
190
	{
191
		return [['slug', InputArgument::OPTIONAL, 'Module slug.']];
192
	}
193
194
	/**
195
	 * Get the console command options.
196
	 *
197
	 * @return array
198
	 */
199 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...
200
	{
201
		return [
202
			['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
203
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run while in production.'],
204
            ['pretend', null, InputOption::VALUE_OPTIONAL, 'Dump the SQL queries that would be run.'],
205
			['seed', null, InputOption::VALUE_OPTIONAL, 'Indicates if the seed task should be re-run.']
206
		];
207
	}
208
}
209