Passed
Push — master ( 3a6da4...ca4c8c )
by Sebastian
03:44
created

Advanced::configureHooks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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