Completed
Pull Request — master (#37)
by Christian
03:46
created

CloudCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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