Completed
Push — master ( 67ad92...902a2a )
by Victor
9s
created

CheckpointCommand::execute()   C

Complexity

Conditions 11
Paths 18

Size

Total Lines 42
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 42
ccs 0
cts 42
cp 0
rs 5.2653
cc 11
eloc 35
nc 18
nop 2
crap 132

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Victor Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2015, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace Owncloud\Updater\Command;
23
24
use Symfony\Component\Console\Input\InputArgument;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Input\InputOption;
27
use Symfony\Component\Console\Output\OutputInterface;
28
29
class CheckpointCommand extends Command {
30
31
	protected function configure(){
32
		$this
33
				->setName('upgrade:checkpoint')
34
				->setDescription('Create or restore owncloud core files')
35
				->addOption(
36
						'create', null, InputOption::VALUE_NONE, 'create a checkpoint'
37
				)
38
				->addOption(
39
						'restore', null, InputOption::VALUE_REQUIRED, 'revert files to a given checkpoint'
40
				)
41
				->addOption(
42
						'list', null, InputOption::VALUE_OPTIONAL, 'show all checkpoints'
43
				)
44
				->addOption(
45
						'remove', null, InputOption::VALUE_REQUIRED, 'remove a checkpoint'
46
				)
47
		;
48
	}
49
50
	protected function execute(InputInterface $input, OutputInterface $output){
51
		clearstatcache();
52
		$checkpoint = $this->container['utils.checkpoint'];
53
		if ($input->getOption('create')){
54
			try {
55
				$checkpointId = $checkpoint->create();
56
				$output->writeln('Created checkpoint ' . $checkpointId);
57
			} catch (\Exception $e){
58
				$output->writeln('Error while creating a checkpoint ' . $checkpointId);
59
			}
60
		} elseif ($input->getOption('remove')){
61
			$checkpointId = stripslashes($input->getOption('remove'));
62
			try {
63
				$checkpoint->remove($checkpointId);
64
				$output->writeln('Removed checkpoint ' . $checkpointId);
65
			} catch (\UnexpectedValueException $e){
66
				$output->writeln($e->getMessage());
67
			} catch (\Exception $e){
68
				$output->writeln('Error while removing a checkpoint ' . $checkpointId);
69
			}
70
		} elseif ($input->getOption('restore')) {
71
			$checkpointId = stripslashes($input->getOption('restore'));
72
			try {
73
				$checkpoint->restore($checkpointId);
74
				$checkpoint->remove($checkpointId);
75
				$output->writeln('Restored checkpoint ' . $checkpointId);
76
			} catch (\UnexpectedValueException $e){
77
				$output->writeln($e->getMessage());
78
			} catch (\Exception $e){
79
				$output->writeln('Error while restoring a checkpoint ' . $checkpointId);
80
			}
81
		} else {
82
			$checkpoints = $checkpoint->getAll();
83
			if (count($checkpoints)){
84
				foreach ($checkpoints as $checkpoint){
85
					$output->writeln(sprintf('%s  - %s', $checkpoint['title'], $checkpoint['date']));
86
				}
87
			} else {
88
				$output->writeln('No checkpoints found');
89
			}
90
		}
91
	}
92
}
93