Passed
Push — master ( 9e0b2c...e0b23c )
by Pol
03:34
created

YamlCommands::bindInputOptionsToConfig()   B

Complexity

Conditions 10
Paths 9

Size

Total Lines 46
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 23
nc 9
nop 1
dl 0
loc 46
ccs 0
cts 32
cp 0
crap 110
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types = 1);
4
5
namespace PhpTaskman\Core\Robo\Plugin\Commands;
6
7
use Consolidation\AnnotatedCommand\AnnotatedCommand;
8
use PhpTaskman\Core\Robo\Task\CollectionFactory\LoadCollectionFactoryTasks;
9
use Robo\Collection\CollectionBuilder;
10
use Robo\Exception\TaskException;
11
use Symfony\Component\Console\Event\ConsoleCommandEvent;
12
13
/**
14
 * Class DynamicCommands.
15
 */
16
final class YamlCommands extends AbstractCommands
17
{
18
    use LoadCollectionFactoryTasks;
19
20
    /**
21
     * Bind input values of custom command options to config entries.
22
     *
23
     * @param \Symfony\Component\Console\Event\ConsoleCommandEvent $event
24
     *
25
     * @hook pre-command-event *
26
     */
27
    public function bindInputOptionsToConfig(ConsoleCommandEvent $event): void
28
    {
29
        $command = $event->getCommand();
30
31
        if (null === $command) {
32
            return;
33
        }
34
35
        if (AnnotatedCommand::class !== \get_class($command)) {
36
            return;
37
        }
38
39
        if (!\is_subclass_of($command, AnnotatedCommand::class)) {
40
            return;
41
        }
42
43
        /** @var \Consolidation\AnnotatedCommand\AnnotatedCommand $command */
44
        /** @var \Consolidation\AnnotatedCommand\AnnotationData $annotatedData */
45
        $annotatedData = $command->getAnnotationData();
0 ignored issues
show
Bug introduced by
The method getAnnotationData() does not exist on Symfony\Component\Console\Command\Command. It seems like you code against a sub-type of Symfony\Component\Console\Command\Command such as Consolidation\AnnotatedCommand\AnnotatedCommand. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
        /** @scrutinizer ignore-call */ 
46
        $annotatedData = $command->getAnnotationData();
Loading history...
46
        if (!$annotatedData->get('dynamic-command')) {
47
            return;
48
        }
49
50
        // Dynamic commands may define their own options bound to specific configuration. Dynamically set the
51
        // configuration from command options.
52
        $config = $this->getConfig();
53
        $commands = $config->get('commands');
54
55
        if (empty($commands[$command->getName()]['options'])) {
56
            return;
57
        }
58
59
        foreach ($commands[$command->getName()]['options'] as $optionName => $option) {
60
            if (empty($option['config']) && $event->getInput()->hasOption($optionName)) {
61
                continue;
62
            }
63
64
            $inputValue = $event->getInput()->getOption($optionName);
65
66
            if (null === $inputValue) {
67
                continue;
68
            }
69
70
            $config->set(
71
                $option['config'],
72
                $event->getInput()->getOption($optionName)
73
            );
74
        }
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getConfigurationFile(): string
81
    {
82
        return __DIR__ . '/../../../../config/commands/default.yml';
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function getDefaultConfigurationFile(): string
89
    {
90
        return __DIR__ . '/../../../../config/default.yml';
91
    }
92
93
    /**
94
     * Run a task.
95
     *
96
     * @dynamic-command true
97
     *
98
     * @throws \Robo\Exception\TaskException
99
     *
100
     * @return \Robo\Collection\CollectionBuilder
101
     */
102
    public function runTasks(): CollectionBuilder
103
    {
104
        $command = $this->input()->getArgument('command');
105
106
        if (!\is_string($command)) {
107
            throw new TaskException($this, 'The command must be a string.');
108
        }
109
110
        $tasks = $this->getConfig()->get('commands.' . $command);
111
112
        $inputOptions = [];
113
        foreach ($this->input()->getOptions() as $name => $value) {
114
            if ($this->input()->hasParameterOption('--' . $name)) {
115
                $inputOptions[$name] = $value;
116
            }
117
        }
118
119
        return $this->taskCollectionFactory($tasks, $inputOptions);
120
    }
121
}
122