Failed Conditions
Push — master ( 8c07b1...95a77b )
by Alexander
02:40
created

RevertCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 11
cp 0
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * This file is part of the SVN-Buddy library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/svn-buddy
9
 */
10
11
namespace ConsoleHelpers\SVNBuddy\Command;
12
13
14
use ConsoleHelpers\ConsoleKit\Exception\CommandException;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class RevertCommand extends AbstractCommand implements IAggregatorAwareCommand
20
{
21
22
	/**
23
	 * {@inheritdoc}
0 ignored issues
show
introduced by
Doc comment short description must start with a capital letter
Loading history...
24
	 */
1 ignored issue
show
introduced by
Missing @return tag in function comment
Loading history...
25
	protected function configure()
26
	{
27
		$this
28
			->setName('revert')
29
			->setDescription('Restore pristine working copy file (undo most local edits)')
30
			->addArgument(
31
				'path',
32
				InputArgument::OPTIONAL,
33
				'Working copy path',
34
				'.'
35
			);
36
37
		parent::configure();
38
	}
39
40
	/**
0 ignored issues
show
introduced by
Doc comment for parameter "$input" missing
Loading history...
introduced by
Doc comment for parameter "$output" missing
Loading history...
41
	 * {@inheritdoc}
0 ignored issues
show
introduced by
Doc comment short description must start with a capital letter
Loading history...
42
	 */
1 ignored issue
show
introduced by
Missing @return tag in function comment
Loading history...
43
	protected function execute(InputInterface $input, OutputInterface $output)
44
	{
45
		$wc_path = $this->getWorkingCopyPath();
46
47
		// Collect added path before "svn revert" because then they won't be shown as added by "svn status".
48
		$added_paths = $this->getAddedPaths();
49
50
		$this->io->writeln('Reverting local changes in working copy ... ');
51
		$command = $this->repositoryConnector->getCommand(
52
			'revert',
53
			'--depth infinity {' . $wc_path . '}'
54
		);
55
		$command->runLive(array(
56
			$wc_path => '.',
57
		));
58
59
		$this->deletePaths($added_paths);
60
61
		$this->setSetting(MergeCommand::SETTING_MERGE_RECENT_CONFLICTS, null, 'merge');
62
		$this->io->writeln('<info>Done</info>');
63
	}
64
65
	/**
66
	 * Returns added paths.
67
	 *
68
	 * @return array
69
	 */
70
	protected function getAddedPaths()
71
	{
72
		$wc_path = $this->getWorkingCopyPath();
73
74
		$added_paths = array();
75
		$status = $this->repositoryConnector->getWorkingCopyStatus($wc_path);
76
77
		foreach ( $status as $status_path => $status_path_data ) {
78
			if ( $status_path_data['item'] === 'added' ) {
79
				$added_paths[] = $status_path;
80
			}
81
		}
82
83
		return $added_paths;
84
	}
85
86
	/**
87
	 * Deletes given paths in the working copy.
88
	 *
89
	 * @param array $paths Paths.
90
	 *
91
	 * @return void
92
	 * @throws CommandException When one of the paths can't be deleted.
93
	 */
94
	protected function deletePaths(array $paths)
95
	{
96
		if ( !$paths ) {
97
			return;
98
		}
99
100
		// When folder with files is added delete files first and then folder.
101
		rsort($paths);
102
		$wc_path = $this->getWorkingCopyPath();
103
104
		foreach ( $paths as $path ) {
105
			$absolute_path = $wc_path . '/' . $path;
106
			$deleted = is_dir($absolute_path) ? rmdir($absolute_path) : unlink($absolute_path);
107
108
			if ( $deleted ) {
109
				$this->io->writeln('Deleted \'' . str_replace($wc_path, '.', $absolute_path) . '\'');
110
			}
111
			else {
112
				throw new CommandException('Unable to delete "' . $path . '".');
113
			}
114
		}
115
	}
116
117
}
118