Completed
Push — master ( 10f95b...bf15d4 )
by Michael
09:02
created

MigrateCommand::execute()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 0
cts 14
cp 0
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 0
crap 6
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\Controller\AbstractController;
12
use Joomla\StatsServer\CommandInterface;
13
use Joomla\StatsServer\Database\Migrations;
14
use Psr\Log\{
15
	LoggerAwareInterface, LoggerAwareTrait
16
};
17
18
/**
19
 * CLI command for migrating the database
20
 *
21
 * @method         \Joomla\StatsServer\CliApplication  getApplication()  Get the application object.
22
 * @property-read  \Joomla\StatsServer\CliApplication  $app              Application object
23
 *
24
 * @since          1.0
25
 */
26
class MigrateCommand extends AbstractController implements CommandInterface, LoggerAwareInterface
27
{
28
	use LoggerAwareTrait;
29
30
	/**
31
	 * Database migrations helper
32
	 *
33
	 * @var    Migrations
34
	 * @since  1.0
35
	 */
36
	private $migrations;
37
38
	/**
39
	 * Constructor.
40
	 *
41
	 * @param   Migrations  $migrations  Database migrations helper
42
	 *
43
	 * @since   1.0
44
	 */
45
	public function __construct(Migrations $migrations)
46
	{
47
		$this->migrations = $migrations;
48
	}
49
50
	/**
51
	 * Execute the controller.
52
	 *
53
	 * @return  boolean
54
	 *
55
	 * @since   1.0
56
	 */
57
	public function execute()
58
	{
59
		$this->getApplication()->outputTitle('Database Migrations: Migrate');
60
61
		// If a version is given, we are only executing that migration
62
		$version = $this->getApplication()->input->getString('version', $this->getApplication()->input->getString('v', ''));
63
64
		try
65
		{
66
			$this->migrations->migrateDatabase($version);
67
		}
68
		catch (\Exception $exception)
69
		{
70
			$this->logger->critical(
71
				'Error migrating database',
72
				['exception' => $exception]
73
			);
74
75
			$message = sprintf('Error migrating database: %s', $exception->getMessage());
76
77
			$this->getApplication()->out("<error>$message</error>");
78
79
			return false;
80
		}
81
82
		$this->logger->info('Database migrated to latest version.');
83
84
		$this->getApplication()->out('<info>Database migrated to latest version.</info>');
85
86
		return true;
87
	}
88
89
	/**
90
	 * Get the command's description
91
	 *
92
	 * @return  string
93
	 *
94
	 * @since   1.0
95
	 */
96
	public function getDescription() : string
97
	{
98
		return 'Migrate the database schema to a newer version.';
99
	}
100
101
	/**
102
	 * Get the command's title
103
	 *
104
	 * @return  string
105
	 *
106
	 * @since   1.0
107
	 */
108
	public function getTitle() : string
109
	{
110
		return 'Database Migrations';
111
	}
112
}
113