Failed Conditions
Pull Request — master (#1)
by Yo
01:56
created

CommandTemplateHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 90
ccs 0
cts 51
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 1
C dumpTemplate() 0 26 8
A doOverwrite() 0 9 1
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\Exception\TargetFileExistsException;
10
use Yoanm\DefaultPhpRepository\Helper\TemplateHelper;
11
12
/**
13
 * Class CommandTemplateHelper
14
 */
15
class CommandTemplateHelper extends TemplateHelper
16
{
17
    /** @var InputInterface */
18
    private $input;
19
    /** @var OutputInterface */
20
    private $output;
21
    /** @var Filesystem */
22
    private $fileSystem;
23
    /** @var bool */
24
    private $skipExisting;
25
    /** @var bool */
26
    private $forceOverride;
27
    /** @var QuestionHelper */
28
    private $questionHelper;
29
30
    /**
31
     * @param QuestionHelper  $questionHelper
32
     * @param InputInterface  $input
33
     * @param OutputInterface $output
34
     * @param array           $variableList
35
     * @param bool            $skipExisting
36
     * @param bool            $forceOverride
37
     * @param array           $extraTemplatePath
38
     */
39
    public function __construct(
40
        QuestionHelper $questionHelper,
41
        InputInterface $input,
42
        OutputInterface $output,
43
        array $variableList,
44
        $skipExisting = true,
45
        $forceOverride = false,
46
        array $extraTemplatePath = []
47
    ) {
48
        parent::__construct($variableList, $extraTemplatePath);
49
50
        $this->questionHelper = $questionHelper;
51
        $this->fileSystem = new Filesystem();
52
        $this->input = $input;
53
        $this->output = $output;
54
55
        $this->skipExisting = $skipExisting;
56
        $this->forceOverride = $forceOverride;
57
    }
58
59
    /**
60
     * @param string $templateFilePath
61
     * @param string $outputFilePath
62
     * @param bool   $overwrite overwrite even if present
63
     *
64
     * @throws TargetFileExistsException
65
     */
66
    public function dumpTemplate($templateFilePath, $outputFilePath, $overwrite = false)
0 ignored issues
show
Unused Code introduced by
The parameter $overwrite 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...
67
    {
68
        $this->output->write("            <info>$outputFilePath</info> : ");
69
        $fileExist = $this->fileSystem->exists($outputFilePath);
70
        $process = true;
71
        if ($fileExist) {
72
            if (false === $this->forceOverride && true === $this->skipExisting) {
73
                $this->output->writeln('<comment>Skipped !</comment>');
74
                $process = false;
75
            } else {
76
                $process = false;
77
                if (true === $this->forceOverride) {
78
                    $process = true;
79
                    $this->output->writeln('<comment>Overriden !</comment>');
80
                } elseif ($this->doOverwrite()) {
81
                    $process = true;
82
                }
83
            }
84
        }
85
        if (true === $process) {
86
            parent::dumpTemplate($templateFilePath, $outputFilePath);
87
            if (false === $fileExist) {
88
                $this->output->writeln('<info>Done</info>');
89
            }
90
        }
91
    }
92
93
    /**@return bool
94
     */
95
    protected function doOverwrite()
96
    {
97
        $question = new ConfirmationQuestion(
98
            '<question>Overwrite (y/n) ? [n]</question>',
99
            false
100
        );
101
102
        return $this->questionHelper->ask($this->input, $this->output, $question);
103
    }
104
}
105