Failed Conditions
Push — master ( 28e23e...6697e2 )
by Yo
01:52
created

CommandTemplateHelper::dumpTemplate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 12
1
<?php
2
namespace Yoanm\DefaultPhpRepository\Command\Helper;
3
4
use Symfony\Component\Console\Helper\QuestionHelper;
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Console\Question\ConfirmationQuestion;
8
use Symfony\Component\Filesystem\Filesystem;
9
use Yoanm\DefaultPhpRepository\Helper\TemplateHelper;
10
11
/**
12
 * Class CommandTemplateHelper
13
 */
14
class CommandTemplateHelper extends TemplateHelper
15
{
16
    /** @var InputInterface */
17
    private $input;
18
    /** @var OutputInterface */
19
    private $output;
20
    /** @var Filesystem */
21
    private $fileSystem;
22
    /** @var bool */
23
    private $skipExisting;
24
    /** @var bool */
25
    private $forceOverride;
26
    /** @var QuestionHelper */
27
    private $questionHelper;
28
29
    /**
30
     * @param QuestionHelper  $questionHelper
31
     * @param InputInterface  $input
32
     * @param OutputInterface $output
33
     * @param array           $variableList
34
     * @param bool            $skipExisting
35
     * @param bool            $forceOverride
36
     * @param array           $extraTemplatePath
37
     */
38
    public function __construct(
39
        QuestionHelper $questionHelper,
40
        InputInterface $input,
41
        OutputInterface $output,
42
        array $variableList,
43
        $skipExisting = true,
44
        $forceOverride = false,
45
        array $extraTemplatePath = []
46
    ) {
47
        parent::__construct($variableList, $extraTemplatePath);
48
49
        $this->questionHelper = $questionHelper;
50
        $this->fileSystem = new Filesystem();
51
        $this->input = $input;
52
        $this->output = $output;
53
54
        $this->skipExisting = $skipExisting;
55
        $this->forceOverride = $forceOverride;
56
    }
57
58
    /**
59
     * @param string $templateFilePath
60
     * @param string $outputFilePath
61
     */
62
    public function dumpTemplate($templateFilePath, $outputFilePath)
63
    {
64
        $this->output->write("            <info>$outputFilePath</info> : ");
65
        list($fileExist, $process) = $this->validateDump($outputFilePath);
66
        if (true === $process) {
67
            parent::dumpTemplate($templateFilePath, $outputFilePath);
68
            if (false === $fileExist) {
69
                $this->output->writeln('<info>Done</info>');
70
            }
71
        }
72
    }
73
74
    /**
75
     * @param $outputFilePath
76
     * @return array
77
     */
78
    protected function validateDump($outputFilePath)
79
    {
80
        $fileExist = $this->fileSystem->exists($outputFilePath);
81
        $process = true;
82
        if ($fileExist) {
83
            if (false === $this->forceOverride && true === $this->skipExisting) {
84
                $this->output->writeln('<comment>Skipped !</comment>');
85
                $process = false;
86
                return array($fileExist, $process);
87
            } else {
88
                $process = false;
89
                if (true === $this->forceOverride) {
90
                    $process = true;
91
                    $this->output->writeln('<comment>Overriden !</comment>');
92
                    return array($fileExist, $process);
93
                } elseif ($this->doOverwrite()) {
94
                    $process = true;
95
                    return array($fileExist, $process);
96
                }
97
                return array($fileExist, $process);
98
            }
99
        }
100
        return array($fileExist, $process);
101
    }
102
103
    /**@return bool
104
     */
105
    protected function doOverwrite()
106
    {
107
        $question = new ConfirmationQuestion(
108
            '<question>Overwrite (y/n) ? [n]</question>',
109
            false
110
        );
111
112
        return $this->questionHelper->ask($this->input, $this->output, $question);
113
    }
114
}
115