Completed
Pull Request — master (#47)
by Christian
24:29 queued 23:07
created

CloudCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
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\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Xabbuh\PandaClient\Exception\PandaException;
19
20
/**
21
 * Base class of all commands which act on panda clouds.
22
 *
23
 * The cloud name can be specified on the command-line. If no cloud is
24
 * given the configured default cloud is used.
25
 *
26
 * @author Christian Flothmann <[email protected]>
27
 */
28
abstract class CloudCommand extends ContainerAwareCommand
29
{
30
    /**
31
     * {@inheritDoc}
32
     */
33 85
    protected function configure()
34
    {
35 85
        $this->setName(static::$defaultName); // BC with Symfony Console 3.3 and older not handling the property automatically
36 85
        $this->addOption(
37 85
            'cloud',
38 85
            '-c',
39 85
            InputOption::VALUE_REQUIRED,
40 85
            'Cloud on which the command is executed.'
41
        );
42 85
    }
43
44
    /**
45
     * @return \Xabbuh\PandaClient\Api\CloudManager
46
     */
47 67
    protected function getCloudManager()
48
    {
49 67
        return $this->getContainer()->get('xabbuh_panda.cloud_manager');
50
    }
51
52
    /**
53
     * Get the cloud to work on.
54
     *
55
     * @param \Symfony\Component\Console\Input\InputInterface $input
56
     *
57
     * @return \Xabbuh\PandaClient\Api\CloudInterface
58
     */
59 67
    protected function getCloud(InputInterface $input)
60
    {
61 67
        if (null === $input->getOption('cloud')) {
62 67
            return $this->getCloudManager()->getDefaultCloud();
63
        }
64
65
        return $this->getCloudManager()->getCloud($input->getOption('cloud'));
66
    }
67
68
    /**
69
     * Executes the actual command (to be implemented by subclasses, will be called automatically).
70
     *
71
     * @param InputInterface  $input
72
     * @param OutputInterface $output
73
     */
74
    abstract protected function doExecuteCommand(InputInterface $input, OutputInterface $output);
75
76
    /**
77
     * {@inheritDoc}
78
     */
79 69
    protected function execute(InputInterface $input, OutputInterface $output)
80
    {
81
        try {
82 69
            $this->doExecuteCommand($input, $output);
83
84 40
            return 0;
85 29
        } catch (PandaException $e) {
86 29
            $output->writeln(sprintf('<error>An error occurred: %s</error>', $e->getMessage()));
87
88 29
            return 1;
89
        }
90
    }
91
}
92