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

ExecuteCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 19.15 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 18
loc 94
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 9 1
A execute() 0 19 3
A completeOptionValues() 0 3 1
A completeArgumentValues() 18 18 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
4
 * @copyright Copyright (c) 2017, ownCloud GmbH
5
 *
6
 * @author Joas Schilling <[email protected]>
7
 *
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OC\Core\Command\Db\Migrations;
25
26
27
use OC\DB\MigrationService;
28
use OC\Migration\ConsoleOutput;
29
use OCP\App\IAppManager;
30
use OCP\IConfig;
31
use OCP\IDBConnection;
32
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
33
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
34
use Symfony\Component\Console\Command\Command;
35
use Symfony\Component\Console\Input\InputArgument;
36
use Symfony\Component\Console\Input\InputInterface;
37
use Symfony\Component\Console\Output\OutputInterface;
38
39
class ExecuteCommand extends Command implements CompletionAwareInterface {
40
41
	/** @var IDBConnection */
42
	private $connection;
43
44
	/** @var IConfig */
45
	private $config;
46
47
	/** @var IAppManager */
48
	protected $appManager;
49
50
	/**
51
	 * ExecuteCommand constructor.
52
	 *
53
	 * @param IDBConnection $connection
54
	 * @param IConfig $config
55
	 * @param IAppManager $appManager
56
	 */
57
	public function __construct(IDBConnection $connection, IAppManager $appManager, IConfig $config) {
58
		$this->connection = $connection;
59
		$this->config = $config;
60
61
		parent::__construct();
62
	}
63
64
	protected function configure() {
65
		$this
66
			->setName('migrations:execute')
67
			->setDescription('Execute a single migration version manually.')
68
			->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on')
69
			->addArgument('version', InputArgument::REQUIRED, 'The version to execute.', null);
70
71
		parent::configure();
72
	}
73
74
	/**
75
	 * @param InputInterface $input
76
	 * @param OutputInterface $output
77
	 * @return int
78
	 */
79
	public function execute(InputInterface $input, OutputInterface $output) {
80
		$appName = $input->getArgument('app');
81
		$ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output));
82
		$version = $input->getArgument('version');
83
84
		if ($this->config->getSystemValue('debug', false) === false) {
85
			$olderVersions = $ms->getMigratedVersions();
86
			$olderVersions[] = '0';
87
			$olderVersions[] = 'prev';
88
			if (in_array($version,  $olderVersions, true)) {
89
				$output->writeln('<error>Can not go back to previous migration without debug enabled</error>');
90
				return 1;
91
			}
92
		}
93
94
95
		$ms->executeStep($version);
96
		return 0;
97
	}
98
99
	/**
100
	 * @param string $optionName
101
	 * @param CompletionContext $context
102
	 * @return string[]
103
	 */
104
	public function completeOptionValues($optionName, CompletionContext $context) {
105
		return [];
106
	}
107
108
	/**
109
	 * @param string $argumentName
110
	 * @param CompletionContext $context
111
	 * @return string[]
112
	 */
113 View Code Duplication
	public function completeArgumentValues($argumentName, CompletionContext $context) {
114
		if ($argumentName === 'app') {
115
			$allApps = \OC_App::getAllApps();
116
			return array_diff($allApps, \OC_App::getEnabledApps(true, true));
117
		}
118
119
		if ($argumentName === 'version') {
120
			$appName = $context->getWordAtIndex($context->getWordIndex() - 1);
121
122
			$ms = new MigrationService($appName, $this->connection);
123
			$migrations = $ms->getAvailableVersions();
124
125
			array_unshift($migrations, 'next', 'latest');
126
			return $migrations;
127
		}
128
129
		return [];
130
	}
131
132
}
133