Completed
Push — master ( 4288f6...92f818 )
by Alexander
28s queued 15s
created

DeployCommand::execute()   A

Complexity

Conditions 6
Paths 13

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 30
ccs 0
cts 18
cp 0
rs 9.0444
cc 6
nc 13
nop 2
crap 42
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 ConsoleHelpers\SVNBuddy\Config\AbstractConfigSetting;
16
use ConsoleHelpers\SVNBuddy\Config\ArrayConfigSetting;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
class DeployCommand extends AbstractCommand implements IConfigAwareCommand
23
{
24
25
	const SETTING_DEPLOY_REMOTE_COMMANDS = 'deploy.remote-commands';
26
27
	const SETTING_DEPLOY_LOCAL_COMMANDS = 'deploy.local-commands';
28
29
	/**
30
	 * {@inheritdoc}
31
	 */
32
	protected function configure()
33
	{
34
		$this
35
			->setName('deploy')
36
			->setDescription('Deploys changes to local/remote server')
37
			->addArgument(
38
				'path',
39
				InputArgument::OPTIONAL,
40
				'Working copy path',
41
				'.'
42
			)
43
			->addOption(
44
				'remote',
45
				'r',
46
				InputOption::VALUE_NONE,
47
				'Performs remote deployment'
48
			)
49
			->addOption(
50
				'local',
51
				'l',
52
				InputOption::VALUE_NONE,
53
				'Performs local deployment'
54
			);
55
56
		parent::configure();
57
	}
58
59
	/**
60
	 * {@inheritdoc}
61
	 *
62
	 * @throws CommandException When none of "--remote" and "--local" options aren't specified.
63
	 * @throws CommandException When deployment commands are not specified.
64
	 */
65
	protected function execute(InputInterface $input, OutputInterface $output)
66
	{
67
		if ( $this->io->getOption('remote') ) {
68
			$is_remote = true;
69
			$deploy_commands = $this->getSetting(self::SETTING_DEPLOY_REMOTE_COMMANDS);
70
		}
71
		elseif ( $this->io->getOption('local') ) {
72
			$is_remote = false;
73
			$deploy_commands = $this->getSetting(self::SETTING_DEPLOY_LOCAL_COMMANDS);
74
		}
75
		else {
76
			throw new CommandException('Please specify either "--remote" or "--local" option.');
77
		}
78
79
		$suffix = $is_remote ? 'remote' : 'local';
80
81
		if ( !$deploy_commands ) {
82
			throw new CommandException('The ' . $suffix . ' deployment commands are not specified.');
83
		}
84
85
		$this->io->writeln('<info>Performing a ' . $suffix . ' deploy...</info>');
86
87
		foreach ( $deploy_commands as $deploy_command ) {
88
			$this->io->writeln('<comment>Executing: ' . $deploy_command . '</comment>');
89
			passthru($deploy_command);
90
			$this->io->writeln('<comment>Done.</comment>');
91
			$this->io->writeln('');
92
		}
93
94
		$this->io->writeln('<info>Deployed.</info>');
95
	}
96
97
	/**
98
	 * Returns list of config settings.
99
	 *
100
	 * @return AbstractConfigSetting[]
101
	 */
102
	public function getConfigSettings()
103
	{
104
		return array(
105
			new ArrayConfigSetting(
106
				self::SETTING_DEPLOY_REMOTE_COMMANDS,
107
				array()
108
			),
109
			new ArrayConfigSetting(
110
				self::SETTING_DEPLOY_LOCAL_COMMANDS,
111
				array()
112
			),
113
		);
114
	}
115
116
}
117