Completed
Push — master ( 38f1ea...fd6c7c )
by Matze
04:05
created

HandleExistingFile::handleQuestion()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
rs 8.8571
cc 5
eloc 14
nc 5
nop 4
1
<?php
2
3
namespace BrainExe\Core\Console\TestGenerator;
4
5
use Exception;
6
use Symfony\Component\Console\Helper\QuestionHelper;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Question\ChoiceQuestion;
10
11
/**
12
 * @codeCoverageIgnore
13
 */
14
class HandleExistingFile
15
{
16
17
    /**
18
     * @param string $originalTest
19
     * @param string $newTest
20
     * @throws Exception
21
     * @return string
22
     */
23
    private function replaceHeaderOnly($originalTest, $newTest)
24
    {
25
        if (!preg_match('/^.*?}/s', $newTest, $matches)) {
26
            throw new Exception('No header found in new test');
27
        }
28
29
        $header = $matches[0];
30
31
        return preg_replace('/^.*?}/s', $header, $originalTest);
32
    }
33
34
35
    /**
36
     * @param string $originalTest
37
     * @param string $newTest
38
     * @param OutputInterface $output
39
     * @todo finish
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
40
     */
41
    private function displayPatch($originalTest, $newTest, OutputInterface $output)
0 ignored issues
show
Unused Code introduced by
The parameter $originalTest is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $newTest is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
        $output->writeln('<info>Diff: Not implemented yet</info>');
44
    }
45
46
    /**
47
     * @param InputInterface $input
48
     * @param OutputInterface $output
49
     * @param QuestionHelper $helper
50
     * @param string $serviceId
51
     * @param string $testFileName
52
     * @param string $template
53
     * @return string
54
     * @throws Exception
55
     */
56
    public function handleExistingFile(
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
57
        InputInterface $input,
58
        OutputInterface $output,
59
        QuestionHelper $helper,
60
        $serviceId,
61
        $testFileName,
62
        $template
63
    ) {
64
        if ($input->getOption('no-interaction')) {
65
            $output->writeln(sprintf("Test for '<info>%s</info>' already exist", $serviceId));
66
            return false;
67
        }
68
69
        $choices = [
70
            'stop' => 'Stop',
71
            'replace' => 'Replace full test file',
72
            'diff' => 'Display the diff',
73
            'header' => 'full setup only',
74
        ];
75
        $question = new ChoiceQuestion(
76
            '<error>The test file already exist. What should i do now?</error>',
77
            $choices
78
        );
79
80
        $answer       = $helper->ask($input, $output, $question);
81
        $originalTest = file_get_contents($testFileName);
82
83
        $answerId = array_flip($choices)[$answer];
84
85
        return $this->handleQuestion($output, $template, $answerId, $originalTest);
86
    }
87
88
    /**
89
     * @param OutputInterface $output
90
     * @param $template
91
     * @param $answerId
92
     * @param $originalTest
93
     * @return bool|string
94
     * @throws Exception
95
     */
96
    private function handleQuestion(OutputInterface $output, string $template, string $answerId, $originalTest)
97
    {
98
        switch ($answerId) {
99
            case 'replace':
100
                break;
101
            case 'header':
102
                $template = $this->replaceHeaderOnly($originalTest, $template);
103
                break;
104
            case 'diff':
105
                $this->displayPatch($originalTest, $template, $output);
106
                return $template;
107
            case 'stop':
108
            default:
109
                return false;
110
        }
111
112
        return $template;
113
    }
114
}
115