Completed
Push — master ( b34843...6180fe )
by Morris
41:44 queued 25:33
created

StatusCommand::completeArgumentValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017, ownCloud GmbH
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 *
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OC\Core\Command\Db\Migrations;
24
25
use OC\DB\MigrationService;
26
use OC\Migration\ConsoleOutput;
27
use OCP\IDBConnection;
28
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
29
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
30
use Symfony\Component\Console\Command\Command;
31
use Symfony\Component\Console\Input\InputArgument;
32
use Symfony\Component\Console\Input\InputInterface;
33
use Symfony\Component\Console\Output\OutputInterface;
34
35
class StatusCommand extends Command implements CompletionAwareInterface {
36
37
	/** @var IDBConnection */
38
	private $connection;
39
40
	/**
41
	 * @param IDBConnection $connection
42
	 */
43
	public function __construct(IDBConnection $connection) {
44
		$this->connection = $connection;
45
		parent::__construct();
46
	}
47
48
	protected function configure() {
49
		$this
50
			->setName('migrations:status')
51
			->setDescription('View the status of a set of migrations.')
52
			->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on');
53
	}
54
55
	public function execute(InputInterface $input, OutputInterface $output) {
56
		$appName = $input->getArgument('app');
57
		$ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output));
58
59
		$infos = $this->getMigrationsInfos($ms);
60
		foreach ($infos as $key => $value) {
61
			$output->writeln("    <comment>>></comment> $key: " . str_repeat(' ', 50 - strlen($key)) . $value);
62
		}
63
	}
64
65
	/**
66
	 * @param string $optionName
67
	 * @param CompletionContext $context
68
	 * @return string[]
69
	 */
70
	public function completeOptionValues($optionName, CompletionContext $context) {
71
		return [];
72
	}
73
74
	/**
75
	 * @param string $argumentName
76
	 * @param CompletionContext $context
77
	 * @return string[]
78
	 */
79 View Code Duplication
	public function completeArgumentValues($argumentName, CompletionContext $context) {
80
		if ($argumentName === 'app') {
81
			$allApps = \OC_App::getAllApps();
82
			return array_diff($allApps, \OC_App::getEnabledApps(true, true));
83
		}
84
		return [];
85
	}
86
87
	/**
88
	 * @param MigrationService $ms
89
	 * @return array associative array of human readable info name as key and the actual information as value
90
	 */
91
	public function getMigrationsInfos(MigrationService $ms) {
92
93
		$executedMigrations = $ms->getMigratedVersions();
94
		$availableMigrations = $ms->getAvailableVersions();
95
		$executedUnavailableMigrations = array_diff($executedMigrations, array_keys($availableMigrations));
96
97
		$numExecutedUnavailableMigrations = count($executedUnavailableMigrations);
98
		$numNewMigrations = count(array_diff(array_keys($availableMigrations), $executedMigrations));
99
100
		$infos = [
101
			'App'								=> $ms->getApp(),
102
			'Version Table Name'				=> $ms->getMigrationsTableName(),
103
			'Migrations Namespace'				=> $ms->getMigrationsNamespace(),
104
			'Migrations Directory'				=> $ms->getMigrationsDirectory(),
105
			'Previous Version'					=> $this->getFormattedVersionAlias($ms, 'prev'),
106
			'Current Version'					=> $this->getFormattedVersionAlias($ms, 'current'),
107
			'Next Version'						=> $this->getFormattedVersionAlias($ms, 'next'),
108
			'Latest Version'					=> $this->getFormattedVersionAlias($ms, 'latest'),
109
			'Executed Migrations'				=> count($executedMigrations),
110
			'Executed Unavailable Migrations'	=> $numExecutedUnavailableMigrations,
111
			'Available Migrations'				=> count($availableMigrations),
112
			'New Migrations'					=> $numNewMigrations,
113
		];
114
115
		return $infos;
116
	}
117
118
	/**
119
	 * @param MigrationService $migrationService
120
	 * @param string $alias
121
	 * @return mixed|null|string
122
	 */
123
	private function getFormattedVersionAlias(MigrationService $migrationService, $alias) {
124
		$migration = $migrationService->getMigration($alias);
125
		//No version found
126
		if ($migration === null) {
127
			if ($alias === 'next') {
128
				return 'Already at latest migration step';
129
			}
130
131
			if ($alias === 'prev') {
132
				return 'Already at first migration step';
133
			}
134
		}
135
136
		return $migration;
137
	}
138
139
140
}
141