Completed
Push — master ( b65bd1...2051f3 )
by Christian
03:20 queued 01:40
created

CloudCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the XabbuhPandaBundle package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Xabbuh\PandaBundle\Command;
13
14
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
15
use Symfony\Component\Console\Helper\Table;
16
use Symfony\Component\Console\Helper\TableHelper;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Xabbuh\PandaClient\Exception\PandaException;
21
22
/**
23
 * Base class of all commands which act on panda clouds.
24
 *
25
 * The cloud name can be specified on the command-line. If no cloud is
26
 * given the configured default cloud is used.
27
 *
28
 * @author Christian Flothmann <[email protected]>
29
 */
30
abstract class CloudCommand extends ContainerAwareCommand
31
{
32
    /**
33
     * {@inheritDoc}
34
     */
35 85
    protected function configure()
36
    {
37 85
        $this->addOption(
38 85
            'cloud',
39 85
            '-c',
40 85
            InputOption::VALUE_REQUIRED,
41 85
            'Cloud on which the command is executed.'
42
        );
43 85
    }
44
45
    /**
46
     * @return \Xabbuh\PandaClient\Api\CloudManager
47
     */
48 67
    protected function getCloudManager()
49
    {
50 67
        return $this->getContainer()->get('xabbuh_panda.cloud_manager');
51
    }
52
53
    /**
54
     * Get the cloud to work on.
55
     *
56
     * @param \Symfony\Component\Console\Input\InputInterface $input
57
     *
58
     * @return \Xabbuh\PandaClient\Api\Cloud
59
     */
60 67
    protected function getCloud(InputInterface $input)
61
    {
62 67
        if (null === $input->getOption('cloud')) {
63 67
            return $this->getCloudManager()->getDefaultCloud();
64
        }
65
66
        return $this->getCloudManager()->getCloud($input->getOption('cloud'));
67
    }
68
69
    /**
70
     * Executes the actual command (to be implemented by subclasses, will be called automatically).
71
     *
72
     * @param InputInterface  $input
73
     * @param OutputInterface $output
74
     */
75
    abstract protected function doExecuteCommand(InputInterface $input, OutputInterface $output);
76
77
    /**
78
     * {@inheritDoc}
79
     */
80 69
    protected function execute(InputInterface $input, OutputInterface $output)
81
    {
82
        try {
83 69
            $this->doExecuteCommand($input, $output);
84 29
        } catch (PandaException $e) {
85 29
            $output->writeln(sprintf('<error>An error occurred: %s</error>', $e->getMessage()));
86
        }
87 69
    }
88
}
89