Completed
Push — master ( 4dc8e4...023a62 )
by Pablo
02:55
created

PreQualityToolProcessor::configMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace PhpGitHooks\Application\Composer;
4
5
use PhpGitHooks\Application\CodeSniffer\PhpCsConfigData;
6
use PhpGitHooks\Application\Message\MessageConfigData;
7
use PhpGitHooks\Application\PhpCsFixer\PhpCsFixerConfigData;
8
use PhpGitHooks\Application\PhpUnit\PhpUnitConfigData;
9
use PhpGitHooks\Infrastructure\Composer\ProcessorInterface;
10
11
class PreQualityToolProcessor extends Processor implements ProcessorInterface
12
{
13
    protected $simpleTools = ['composer', 'jsonlint', 'phplint', 'phpmd'];
14
15
    /**
16
     * @param array $configData
17
     *
18
     * @return array
19
     */
20 10
    public function execute(array $configData)
21
    {
22 10
        if (true === $this->enableHook($configData)) {
23 9
            $hookData = $this->configData[$this->hookName()];
24 9
            $this->configMessage($hookData);
25
26 9
            $execute = $hookData['execute'];
27 9
            $this->configSimpleTools($execute);
28 9
            $this->configComplexTools($execute);
29
        }
30
31 6
        return $this->configData;
32
    }
33
34
    /**
35
     * @param array $configData
36
     *
37
     * @return bool
38
     */
39 10 View Code Duplication
    protected function enableHook(array $configData)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41 10
        if (!isset($configData[$this->hookName()])) {
42 3
            $enable = $this->setQuestion(sprintf('Do you want enable %s hook?', $this->hookName()), '[Y/n]', 'Y');
43
44 3
            $enabled = 'Y' === strtoupper($enable) ? true : false;
45
46 3
            $this->configData[$this->hookName()] = [
47 3
                'enabled' => $enabled,
48
                'execute' => [],
49
            ];
50
51 3
            return $enabled;
52
        }
53
54 7
        $this->configData = $configData;
55
56 7
        return $configData[$this->hookName()]['enabled'];
57
    }
58
59
    /**
60
     * @param array $execute
61
     */
62 9
    protected function configSimpleTools(array $execute)
63
    {
64 9
        foreach ($this->simpleTools as $tool) {
65 9
            if (!isset($execute[$tool])) {
66 8
                $answer = $this->setQuestionTool($tool);
67 9
                $this->configData[$this->hookName()]['execute'][$tool] = 'Y' === strtoupper($answer) ? true : false;
68
            }
69
        }
70 9
    }
71
72
    /**
73
     * @param array $execute
74
     */
75 9
    protected function configComplexTools(array $execute)
76
    {
77 9
        $this->configPhpCs($execute);
78 9
        $this->configPhpCsFixer($execute);
79 7
        $this->configPhpUnit($execute);
80 5
    }
81
82
    /**
83
     * @param array $execute
84
     */
85 9
    protected function configPhpCs(array $execute)
86
    {
87 9
        $phpCsConfig = new PhpCsConfigData($this->io);
88 9
        $this->configData[$this->hookName()]['execute'][PhpCsConfigData::TOOL] = $phpCsConfig
89 9
            ->createConfigData($execute);
90 9
    }
91
92
    /**
93
     * @param array $execute
94
     */
95 9
    protected function configPhpCsFixer(array $execute)
96
    {
97 9
        $phpCsFixerConfig = new PhpCsFixerConfigData($this->io);
98 9
        $this->configData[$this->hookName()]['execute'][PhpCsFixerConfigData::TOOL] = $phpCsFixerConfig
99 9
            ->createConfigData($execute);
100 7
    }
101
102 7
    protected function configPhpUnit(array $execute)
103
    {
104 7
        $phpUnitConfig = new PhpUnitConfigData($this->io);
105 7
        $this->configData[$this->hookName()]['execute'][PhpUnitConfigData::TOOL] = $phpUnitConfig
106 7
            ->createConfigData($execute);
107 5
    }
108
109
    /**
110
     * @param string $tool
111
     *
112
     * @return string
113
     */
114 8
    protected function setQuestionTool($tool)
115
    {
116 8
        return $this->setQuestion(sprintf('Do you want enable %s tool?', strtoupper($tool)), '[Y/n]', 'Y');
117
    }
118
119
    /**
120
     * @param array $hookData
121
     */
122 9
    protected function configMessage(array $hookData)
123
    {
124 9
        $message = new MessageConfigData($this->io, $this->hookName());
125 9
        $this->configData[$this->hookName()][MessageConfigData::TOOL] = $message->config($hookData);
126 9
    }
127
128
    /**
129
     * @return string
130
     */
131
    public function hookName()
132
    {
133
    }
134
}
135