Completed
Pull Request — 1.x (#56)
by Christian
11:34 queued 10:31
created

CloudCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
cbo 4
dl 0
loc 64
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
doExecuteCommand() 0 1 ?
A configure() 0 10 1
A getCloudManager() 0 4 1
A getCloud() 0 8 2
A execute() 0 12 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
 * @internal since 1.5
29
 */
30
abstract class CloudCommand extends ContainerAwareCommand
31
{
32
    /**
33
     * {@inheritDoc}
34
     */
35
    protected function configure()
36
    {
37
        $this->setName(static::$defaultName); // BC with Symfony Console 3.3 and older not handling the property automatically
38
        $this->addOption(
39
            'cloud',
40
            '-c',
41
            InputOption::VALUE_REQUIRED,
42
            'Cloud on which the command is executed.'
43
        );
44
    }
45
46
    /**
47
     * @return \Xabbuh\PandaClient\Api\CloudManager
48
     */
49
    protected function getCloudManager()
50
    {
51
        return $this->getContainer()->get('xabbuh_panda.cloud_manager');
52
    }
53
54
    /**
55
     * Get the cloud to work on.
56
     *
57
     * @param \Symfony\Component\Console\Input\InputInterface $input
58
     *
59
     * @return \Xabbuh\PandaClient\Api\CloudInterface
60
     */
61
    protected function getCloud(InputInterface $input)
62
    {
63
        if (null === $input->getOption('cloud')) {
64
            return $this->getCloudManager()->getDefaultCloud();
65
        }
66
67
        return $this->getCloudManager()->getCloud($input->getOption('cloud'));
68
    }
69
70
    /**
71
     * Executes the actual command (to be implemented by subclasses, will be called automatically).
72
     *
73
     * @param InputInterface  $input
74
     * @param OutputInterface $output
75
     */
76
    abstract protected function doExecuteCommand(InputInterface $input, OutputInterface $output);
77
78
    /**
79
     * {@inheritDoc}
80
     */
81
    protected function execute(InputInterface $input, OutputInterface $output)
82
    {
83
        try {
84
            $this->doExecuteCommand($input, $output);
85
86
            return 0;
87
        } catch (PandaException $e) {
88
            $output->writeln(sprintf('<error>An error occurred: %s</error>', $e->getMessage()));
89
90
            return 1;
91
        }
92
    }
93
}
94