Completed
Push — symfony-5 ( 5d6114...fa4e1a )
by Christian
03:47
created

CloudCommandTrait::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\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
20
use Symfony\Component\DependencyInjection\ContainerInterface;
21
use Xabbuh\PandaClient\Exception\PandaException;
22
23 1
if (class_exists(ContainerAwareCommand::class)) {
24
    /**
25
     * Base class of all commands which act on panda clouds.
26
     *
27
     * The cloud name can be specified on the command-line. If no cloud is
28
     * given the configured default cloud is used.
29
     *
30
     * @author Christian Flothmann <[email protected]>
31
     *
32
     * @internal since 1.5
33
     */
34
    abstract class CloudCommand extends ContainerAwareCommand
35
    {
36
        use CloudCommandTrait;
37
    }
38
} else {
39
    /**
40
     * Base class of all commands which act on panda clouds.
41
     *
42
     * The cloud name can be specified on the command-line. If no cloud is
43
     * given the configured default cloud is used.
44
     *
45
     * @author Christian Flothmann <[email protected]>
46
     *
47
     * @internal since 1.5
48
     */
49
    abstract class CloudCommand extends Command implements ContainerAwareInterface
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type Xabbuh\PandaBundle\Command\CloudCommand has been defined more than once; this definition is ignored, only the first definition in this file (L34-37) is considered.

This check looks for classes that have been defined more than once in the same file.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
50
    {
51
        use CloudCommandTrait;
52
53
        private $container;
54
55 85
        public function setContainer(ContainerInterface $container = null)
56
        {
57 85
            $this->container = $container;
58 85
        }
59
60 67
        public function getContainer()
61
        {
62 67
            return $this->container;
63
        }
64
    }
65
}
66
67
trait CloudCommandTrait
68
{
69
    /**
70
     * {@inheritDoc}
71
     */
72 85
    protected function configure()
73
    {
74 85
        $this->setName(static::$defaultName); // BC with Symfony Console 3.3 and older not handling the property automatically
0 ignored issues
show
Bug introduced by
It seems like setName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
75 85
        $this->addOption(
0 ignored issues
show
Bug introduced by
It seems like addOption() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
76 85
            'cloud',
77 85
            '-c',
78 85
            InputOption::VALUE_REQUIRED,
79 85
            'Cloud on which the command is executed.'
80
        );
81 85
    }
82
83
    /**
84
     * @return \Xabbuh\PandaClient\Api\CloudManager
85
     */
86 67
    protected function getCloudManager()
87
    {
88 67
        return $this->getContainer()->get('xabbuh_panda.cloud_manager');
0 ignored issues
show
Bug introduced by
It seems like getContainer() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
89
    }
90
91
    /**
92
     * Get the cloud to work on.
93
     *
94
     * @param \Symfony\Component\Console\Input\InputInterface $input
95
     *
96
     * @return \Xabbuh\PandaClient\Api\CloudInterface
97
     */
98 67
    protected function getCloud(InputInterface $input)
99
    {
100 67
        if (null === $input->getOption('cloud')) {
101 67
            return $this->getCloudManager()->getDefaultCloud();
102
        }
103
104
        return $this->getCloudManager()->getCloud($input->getOption('cloud'));
105
    }
106
107
    /**
108
     * Executes the actual command (to be implemented by subclasses, will be called automatically).
109
     *
110
     * @param InputInterface  $input
111
     * @param OutputInterface $output
112
     */
113
    abstract protected function doExecuteCommand(InputInterface $input, OutputInterface $output);
114
115
    /**
116
     * {@inheritDoc}
117
     */
118 69
    protected function execute(InputInterface $input, OutputInterface $output)
119
    {
120
        try {
121 69
            $this->doExecuteCommand($input, $output);
122
123 40
            return 0;
124 29
        } catch (PandaException $e) {
125 29
            $output->writeln(sprintf('<error>An error occurred: %s</error>', $e->getMessage()));
126
127 29
            return 1;
128
        }
129
    }
130
}
131