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 ( a1e198...6844a0 )
by Shea
08:01
created

ModuleMigrateCommand::fire()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 26
Code Lines 17

Duplication

Lines 11
Ratio 42.31 %

Importance

Changes 5
Bugs 3 Features 1
Metric Value
c 5
b 3
f 1
dl 11
loc 26
rs 8.439
cc 6
eloc 17
nc 7
nop 0
1
<?php
2
namespace Caffeinated\Modules\Console\Commands;
3
4
use Caffeinated\Modules\Modules;
5
use Illuminate\Console\Command;
6
use Illuminate\Console\ConfirmableTrait;
7
use Illuminate\Database\Migrations\Migrator;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Str;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Input\InputArgument;
12
13
class ModuleMigrateCommand extends Command
14
{
15
    use ConfirmableTrait;
16
17
    /**
18
     * The console command name.
19
     *
20
     * @var string
21
     */
22
	protected $name = 'module:migrate';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
	protected $description = 'Run the 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
	 * Create a new command instance.
43
	 *
44
	 * @param Migrator  $migrator
45
	 * @param Modules  $module
46
	 */
47
	public function __construct(Migrator $migrator, Modules $module)
48
	{
49
		parent::__construct();
50
51
		$this->migrator = $migrator;
52
		$this->module   = $module;
53
	}
54
55
	/**
56
	 * Execute the console command.
57
	 *
58
	 * @return mixed
59
	 */
60
	public function fire()
61
	{
62
		$this->prepareDatabase();
63
64
		if (! empty($this->argument('slug'))) {
65
            $module = $this->module->getProperties($this->argument('slug'));
66
67
			if ($this->module->isEnabled($module['slug'])) {
68
				return $this->migrate($module['slug']);
69
			} elseif ($this->option('force')) {
70
				return $this->migrate($module['slug']);
71
			} else {
72
                return $this->error('Nothing to migrate.');
73
            }
74 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...
75
			if ($this->option('force')) {
76
				$modules = $this->module->all();
77
			} else {
78
				$modules = $this->module->enabled();
79
			}
80
81
			foreach ($modules as $module) {
82
				$this->migrate($module['slug']);
83
			}
84
		}
85
	}
86
87
	/**
88
	 * Run migrations for the specified module.
89
	 *
90
	 * @param  string $slug
91
	 * @return mixed
92
	 */
93
	protected function migrate($slug)
94
	{
95
		if ($this->module->exists($slug)) {
96
			$pretend = array(Arr::get($this->option(), 'pretend', false));
97
			$path    = $this->getMigrationPath($slug);
98
99
			$this->migrator->run($path, $pretend);
100
101
			// Once the migrator has run we will grab the note output and send it out to
102
			// the console screen, since the migrator itself functions without having
103
			// any instances of the OutputInterface contract passed into the class.
104
			foreach ($this->migrator->getNotes() as $note) {
105
                if (! $this->option('quiet')) {
106
                    $this->line($note);
107
                }
108
			}
109
110
			// Finally, if the "seed" option has been given, we will re-run the database
111
			// seed task to re-populate the database, which is convenient when adding
112
			// a migration and a seed at the same time, as it is only this command.
113
			if ($this->option('seed')) {
114
				$this->call('module:seed', ['module' => $slug, '--force' => true]);
115
			}
116
		} else {
117
			return $this->error("Module does not exist.");
118
		}
119
	}
120
121
	/**
122
	 * Get migration directory path.
123
	 *
124
	 * @param  string $slug
125
	 * @return string
126
	 */
127
	protected function getMigrationPath($slug)
128
	{
129
		$path = $this->module->getModulePath($slug);
130
131
		return $path.'Database/Migrations/';
132
	}
133
134
	/**
135
	 * Prepare the migration database for running.
136
	 *
137
	 * @return void
138
	 */
139
	protected function prepareDatabase()
140
	{
141
		$this->migrator->setConnection($this->option('database'));
142
143
		if (! $this->migrator->repositoryExists()) {
144
			$options = array('--database' => $this->option('database'));
145
146
			$this->call('migrate:install', $options);
147
		}
148
	}
149
150
	/**
151
	 * Get the console command arguments.
152
	 *
153
	 * @return array
154
	 */
155
	protected function getArguments()
156
	{
157
		return [['slug', InputArgument::OPTIONAL, 'Module slug.']];
158
	}
159
160
	/**
161
	 * Get the console command options.
162
	 *
163
	 * @return array
164
	 */
165 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...
166
	{
167
		return [
168
			['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
169
			['force', null, InputOption::VALUE_NONE, 'Force the operation to run while in production.'],
170
			['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'],
171
			['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'],
172
		];
173
	}
174
}
175