Completed
Push — master ( d82965...ddb300 )
by Christian
13s queued 11s
created

CloudCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 since 1.5
30
 */
31
abstract class CloudCommand extends Command
32
{
33
    private $cloudManager;
34
35 86
    public function __construct(CloudManagerInterface $cloudManager)
36
    {
37 86
        parent::__construct();
38
39 86
        $this->cloudManager = $cloudManager;
40 86
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45 86
    protected function configure()
46
    {
47 86
        $this->setName(static::$defaultName); // BC with Symfony Console 3.3 and older not handling the property automatically
48 86
        $this->addOption(
49 86
            'cloud',
50 86
            '-c',
51 86
            InputOption::VALUE_REQUIRED,
52 86
            'Cloud on which the command is executed.'
53
        );
54 86
    }
55
56
    /**
57
     * @return \Xabbuh\PandaClient\Api\CloudManager
58
     */
59 67
    protected function getCloudManager()
60
    {
61 67
        if (null !== $this->cloudManager) {
62 67
            return $this->cloudManager;
63
        }
64
65
        return $this->getContainer()->get('xabbuh_panda.cloud_manager');
0 ignored issues
show
Bug introduced by
The method getContainer() does not seem to exist on object<Xabbuh\PandaBundle\Command\CloudCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
    }
67
68
    /**
69
     * Get the cloud to work on.
70
     *
71
     * @param \Symfony\Component\Console\Input\InputInterface $input
72
     *
73
     * @return \Xabbuh\PandaClient\Api\CloudInterface
74
     */
75 67
    protected function getCloud(InputInterface $input)
76
    {
77 67
        if (null === $input->getOption('cloud')) {
78 67
            return $this->getCloudManager()->getDefaultCloud();
79
        }
80
81
        return $this->getCloudManager()->getCloud($input->getOption('cloud'));
82
    }
83
84
    /**
85
     * Executes the actual command (to be implemented by subclasses, will be called automatically).
86
     *
87
     * @param InputInterface  $input
88
     * @param OutputInterface $output
89
     */
90
    abstract protected function doExecuteCommand(InputInterface $input, OutputInterface $output);
91
92
    /**
93
     * {@inheritDoc}
94
     */
95 69
    protected function execute(InputInterface $input, OutputInterface $output)
96
    {
97
        try {
98 69
            $this->doExecuteCommand($input, $output);
99
100 40
            return 0;
101 29
        } catch (PandaException $e) {
102 29
            $output->writeln(sprintf('<error>An error occurred: %s</error>', $e->getMessage()));
103
104 29
            return 1;
105
        }
106
    }
107
}
108