Completed
Pull Request — master (#42)
by Michael
06:48 queued 05:05
created

MigrationStatusCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Joomla! Statistics Server
4
 *
5
 * @copyright  Copyright (C) 2013 - 2017 Open Source Matters, Inc. All rights reserved.
6
 * @license    http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later
7
 */
8
9
namespace Joomla\StatsServer\Commands\Database;
10
11
use Joomla\Console\Command\AbstractCommand;
12
use Joomla\StatsServer\Database\Migrations;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Style\SymfonyStyle;
16
17
/**
18
 * CLI command for checking the database migration status
19
 */
20
class MigrationStatusCommand extends AbstractCommand
21
{
22
	/**
23
	 * The default command name
24
	 *
25
	 * @var  string|null
26
	 */
27
	protected static $defaultName = 'database:migrations:status';
28
29
	/**
30
	 * Database migrations helper
31
	 *
32
	 * @var  Migrations
33
	 */
34
	private $migrations;
35
36
	/**
37
	 * Constructor.
38
	 *
39
	 * @param   Migrations  $migrations  Database migrations helper
40
	 */
41 3
	public function __construct(Migrations $migrations)
42
	{
43 3
		$this->migrations = $migrations;
44
45 3
		parent::__construct();
46 3
	}
47
48
	/**
49
	 * Internal function to execute the command.
50
	 *
51
	 * @param   InputInterface   $input   The input to inject into the command.
52
	 * @param   OutputInterface  $output  The output to inject into the command.
53
	 *
54
	 * @return  integer  The command exit code
55
	 */
56 3
	protected function doExecute(InputInterface $input, OutputInterface $output): int
57
	{
58 3
		$symfonyStyle = new SymfonyStyle($input, $output);
59
60 3
		$symfonyStyle->title('Database Migrations: Check Status');
61
62 3
		$status = $this->migrations->checkStatus();
63
64 3
		if (!$status->tableExists)
65
		{
66 1
			$symfonyStyle->comment('The migrations table does not exist, run the "database:migrate" command to set up the database.');
67
		}
68 2
		elseif ($status->latest)
69
		{
70 1
			$symfonyStyle->success('Your database is up-to-date.');
71
		}
72
		else
73
		{
74 1
			$symfonyStyle->comment(sprintf('Your database is not up-to-date. You are missing %d migration(s).', $status->missingMigrations));
75
76 1
			$symfonyStyle->table(
77
				[
78 1
					'Current Version',
79
					'Latest Version',
80
				],
81
				[
82
					[
83 1
						$status->currentVersion,
84 1
						$status->latestVersion,
85
					],
86
				]
87
			);
88
89 1
			$symfonyStyle->comment('To update, run the "database:migrate" command.');
90
		}
91
92 3
		return 0;
93
	}
94
95
	/**
96
	 * Configures the current command.
97
	 *
98
	 * @return  void
99
	 */
100 3
	protected function configure(): void
101
	{
102 3
		$this->setDescription('Check the database migration status.');
103 3
	}
104
}
105