ConfiguratorTrait::configure()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
/**
3
 * This file is part of the phpspec-console package.
4
 * (c) 2017 Timo Michna <timomichna/yahoo.de>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Tidal\PhpSpec\ConsoleExtension\Behavior\Command;
11
12
use Tidal\PhpSpec\ConsoleExtension\Exception\InvalidCommandException;
13
use Tidal\PhpSpec\ConsoleExtension\Contract\Command\CommandInterface;
14
use Tidal\PhpSpec\ConsoleExtension\Contract\Command\ConfigInterface as Config;
15
use Symfony\Component\Console\Input\InputArgument;
16
17
/**
18
 * trait Tidal\PhpSpec\ConsoleExtension\Behavior\Command\ConfiguratorTrait
19
 */
20
trait ConfiguratorTrait
21
{
22
    /**
23
     * @var array
24
     */
25
    private $config;
26
27
    /**
28
     * @param array $config
29
     */
30
    public function setConfig(array $config) : void
31
    {
32
        $this->config = $config;
33
    }
34
35
    /**
36
     * @return array
37
     */
38
    public function getConfig(): array
39
    {
40
        return $this->config;
41
    }
42
43
    /**
44
     * @param CommandInterface $command
45
     */
46
    public function configure(CommandInterface $command) : void
47
    {
48
        if (!$this->accept($command)) {
49
            throw new InvalidCommandException($command);
50
        }
51
52
        $this->doConfigure($command);
53
        $this->configureScalarProperties($command);
54
        $this->configureArguments($command);
55
        $this->configureOptions($command);
56
    }
57
58
    abstract protected function doConfigure(CommandInterface $command);
59
60
    public function accept(CommandInterface $command): bool
61
    {
62
        return $this->doAccept($command);
63
    }
64
65
    abstract protected function doAccept(CommandInterface $command): bool;
66
67
    /**
68
     * @param array $config
69
     * @return InputArgument[]
70
     */
71
    protected function createArguments(array $config)
72
    {
73
        $arguments = [];
74
        foreach ($config as $name => $argument) {
75
            $arguments[] = new InputArgument(
76
                $name,
77
                $argument[Config::MODE_KEY],
78
                $argument[Config::DESCRIPTION_KEY]
79
            );
80
        }
81
82
        return $arguments;
83
    }
84
85
    /**
86
     * @param CommandInterface $command
87
     */
88
    private function configureArguments(CommandInterface $command)
89
    {
90
        if (!isset($this->getConfig()[Config::ARGUMENTS_KEY])) {
91
            return;
92
        }
93
        $command->setDefinition(
94
            $this->createArguments(
95
                $this->getConfig()[Config::ARGUMENTS_KEY]
96
            )
97
        );
98
    }
99
100
    /**
101
     * @param CommandInterface $command
102
     */
103
    private function configureOptions(CommandInterface $command)
104
    {
105
        if (!isset($this->getConfig()[Config::OPTIONS_KEY])) {
106
            return;
107
        }
108
109
        foreach ($this->getConfig()[Config::OPTIONS_KEY] as $name => $option) {
110
            $command->addOption(
111
                $name,
112
                isset($option[Config::SHORTCUT_KEY]) ? $option[Config::SHORTCUT_KEY] : null,
113
                isset($option[Config::MODE_KEY]) ? $option[Config::MODE_KEY] : null,
114
                isset($option[Config::DESCRIPTION_KEY]) ? $option[Config::DESCRIPTION_KEY] : ''
115
            );
116
        }
117
    }
118
119
    private function getScalarConfig()
120
    {
121
        return array_intersect_key(
122
            $this->getConfig(),
123
            array_flip(
124
                Config::SCALAR_CONFIG
125
            )
126
        );
127
    }
128
129
    private function configureScalarProperties(CommandInterface $command)
130
    {
131
        foreach ($this->getScalarConfig() as $key => $value) {
132
            $command->{'set'.ucfirst($key)}($value);
133
        }
134
    }
135
}
136
137