Completed
Push — master ( fd6c7c...1f9fde )
by Matze
09:32
created

HandleExistingFile::handleExistingFile()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 31
rs 8.8571
cc 2
eloc 22
nc 2
nop 6
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
     * @param InputInterface $input
36
     * @param OutputInterface $output
37
     * @param QuestionHelper $helper
38
     * @param string $serviceId
39
     * @param string $testFileName
40
     * @param string $template
41
     * @return string
42
     * @throws Exception
43
     */
44
    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...
45
        InputInterface $input,
46
        OutputInterface $output,
47
        QuestionHelper $helper,
48
        $serviceId,
49
        $testFileName,
50
        $template
51
    ) {
52
        if ($input->getOption('no-interaction')) {
53
            $output->writeln(sprintf("Test for '<info>%s</info>' already exist", $serviceId));
54
            return false;
55
        }
56
57
        $choices = [
58
            'stop' => 'Stop',
59
            'replace' => 'Replace full test file',
60
            'diff' => 'Display the diff',
61
            'header' => 'full setup only',
62
        ];
63
        $question = new ChoiceQuestion(
64
            '<error>The test file already exist. What should i do now?</error>',
65
            $choices
66
        );
67
68
        $answer       = $helper->ask($input, $output, $question);
69
        $originalTest = file_get_contents($testFileName);
70
71
        $answerId = array_flip($choices)[$answer];
72
73
        return $this->handleQuestion($output, $template, $answerId, $originalTest);
74
    }
75
76
    /**
77
     * @param OutputInterface $output
78
     * @param $template
79
     * @param $answerId
80
     * @param $originalTest
81
     * @return bool|string
82
     * @throws Exception
83
     */
84
    private function handleQuestion(OutputInterface $output, string $template, string $answerId, $originalTest)
0 ignored issues
show
Unused Code introduced by
The parameter $output 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...
85
    {
86
        switch ($answerId) {
87
            case 'replace':
88
                break;
89
            case 'header':
90
                $template = $this->replaceHeaderOnly($originalTest, $template);
91
                break;
92
            case 'stop':
93
            default:
94
                return false;
95
        }
96
97
        return $template;
98
    }
99
}
100