Advanced   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 109
rs 10
ccs 40
cts 40
cp 1
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A configureHooks() 0 4 2
A configureHook() 0 16 3
A getActionOptions() 0 3 2
A getActionConfig() 0 6 1
A getPHPActionOption() 0 14 2
A getPHPActionOptions() 0 10 2
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[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 CaptainHook\App\Runner\Config\Setup;
13
14
use CaptainHook\App\Config;
15
use CaptainHook\App\Console\IOUtil;
16
use CaptainHook\App\Hook\Util as HookUtil;
17
use CaptainHook\App\Runner\Util as RunnerUtil;
18
use CaptainHook\App\Runner\Config\Setup;
19
20
/**
21
 * Class Advanced
22
 *
23
 * @package CaptainHook
24
 * @author  Sebastian Feldmann <[email protected]>
25
 * @link    https://github.com/captainhook-git/captainhook
26
 * @since   Class available since Release 2.2.0
27
 */
28
class Advanced extends Guided implements Setup
29
{
30
    /**
31
     * Setup hook configurations by asking some questions
32
     *
33
     * @param  \CaptainHook\App\Config $config
34
     * @return void
35
     * @throws \Exception
36
     */
37 4
    public function configureHooks(Config $config): void
38
    {
39 4
        foreach (HookUtil::getHooks() as $hook) {
40 4
            $this->configureHook($config->getHookConfig($hook), $hook);
41
        }
42
    }
43
44
    /**
45
     * Configure a hook by asking some questions
46
     *
47
     * @param  \CaptainHook\App\Config\Hook $config
48
     * @param  string                       $name
49
     * @return void
50
     * @throws \Exception
51
     */
52 4
    public function configureHook(Config\Hook $config, string $name): void
53
    {
54 4
        $answer = $this->io->ask('  <info>Enable \'' . $name . '\' hook?</info> <comment>[y,n]</comment> ', 'n');
55 4
        $enable = IOUtil::answerToBool($answer);
56
57 4
        $config->setEnabled($enable);
58
59 4
        if ($enable) {
60 4
            $addAction = $this->io->ask('  <info>Add a validation action?</info> <comment>[y,n]</comment> ', 'n');
61
62 4
            while (IOUtil::answerToBool($addAction)) {
63 4
                $config->addAction($this->getActionConfig());
64
                // add another action?
65 4
                $addAction = $this->io->ask(
66 4
                    '  <info>Add another validation action?</info> <comment>[y,n]</comment> ',
67 4
                    'n'
68 4
                );
69
            }
70
        }
71
    }
72
73
    /**
74
     * Setup a action config with user input
75
     *
76
     * @return \CaptainHook\App\Config\Action
77
     * @throws \Exception
78
     */
79 5
    public function getActionConfig(): Config\Action
80
    {
81 5
        $call    = $this->io->ask('  <info>PHP class or shell command to execute?</info> ');
82 5
        $options = $this->getActionOptions(RunnerUtil::getExecType($call));
83
84 5
        return new Config\Action($call, $options);
85
    }
86
87
    /**
88
     * Ask the user for any action options
89
     *
90
     * @param  string $type
91
     * @return array<string, string>
92
     * @throws \Exception
93
     */
94 5
    public function getActionOptions(string $type): array
95
    {
96 5
        return 'php' === $type ? $this->getPHPActionOptions() : [];
97
    }
98
99
    /**
100
     * Get the php action options
101
     *
102
     * @return array<string, string>
103
     * @throws \Exception
104
     */
105 4
    protected function getPHPActionOptions(): array
106
    {
107 4
        $options = [];
108 4
        $addOption = $this->io->ask('  <info>Add a validator option?</info> <comment>[y,n]</comment> ', 'n');
109 4
        while (IOUtil::answerToBool($addOption)) {
110 3
            $options[] = $this->getPHPActionOption();
111
            // add another action?
112 3
            $addOption = $this->io->ask('  <info>Add another validator option?</info> <comment>[y,n]</comment> ', 'n');
113
        }
114 4
        return array_merge(...$options);
115
    }
116
117
    /**
118
     * Ask the user for a php action option
119
     *
120
     * @return array<string, string>
121
     * @throws \Exception
122
     */
123 3
    protected function getPHPActionOption(): array
124
    {
125 3
        $result = [];
126 3
        $answer = $this->io->askAndValidate(
127 3
            '  <info>Specify options key and value</info> <comment>[key:value]</comment> ',
128 3
            ['\\CaptainHook\\App\\Runner\\Config\\Setup\\Guided', 'isPHPActionOptionValid'],
129 3
            3,
130 3
            null
131 3
        );
132 3
        if (null !== $answer) {
133 3
            list($key, $value) = explode(':', $answer);
134 3
            $result = [$key => $value];
135
        }
136 3
        return $result;
137
    }
138
}
139