Completed
Push — drop-container-aware-command ( 55eeca )
by Christian
02:20
created

CloudCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\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\CloudManager;
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
    public function __construct(CloudManager $cloudManager)
36
    {
37
        $this->cloudManager = $cloudManager;
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    protected function configure()
44
    {
45
        $this->setName(static::$defaultName); // BC with Symfony Console 3.3 and older not handling the property automatically
46
        $this->addOption(
47
            'cloud',
48
            '-c',
49
            InputOption::VALUE_REQUIRED,
50
            'Cloud on which the command is executed.'
51
        );
52
    }
53
54
    /**
55
     * @return \Xabbuh\PandaClient\Api\CloudManager
56
     */
57
    protected function getCloudManager()
58
    {
59
        return $this->cloudManager->get('xabbuh_panda.cloud_manager');
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Xabbuh\PandaClient\Api\CloudManager>.

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