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

CommandTemplateHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 88
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 $fs;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
23
    /** @var bool */
24
    private $skipExisting;
25
    /** @var bool */
26
    private $forceOverride;
27
28
    /**
29
     * @param QuestionHelper  $questionHelper
30
     * @param InputInterface  $input
31
     * @param OutputInterface $output
32
     * @param array           $variableList
33
     * @param bool            $skipExisting
34
     * @param bool            $forceOverride
35
     * @param array           $extraTemplatePath
36
     */
37
    public function __construct(
38
        QuestionHelper $questionHelper,
39
        InputInterface $input,
40
        OutputInterface $output,
41
        array $variableList,
42
        $skipExisting = true,
43
        $forceOverride = false,
44
        array $extraTemplatePath = []
45
    ) {
46
        parent::__construct($variableList, $extraTemplatePath);
47
48
        $this->questionHelper = $questionHelper;
0 ignored issues
show
Bug introduced by
The property questionHelper does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49
        $this->fs = new Filesystem();
50
        $this->input = $input;
51
        $this->output = $output;
52
53
        $this->skipExisting = $skipExisting;
54
        $this->forceOverride = $forceOverride;
55
    }
56
57
    /**
58
     * @param string $templateFilePath
59
     * @param string $outputFilePath
60
     * @param bool   $overwrite overwrite even if present
61
     *
62
     * @throws TargetFileExistsException
63
     */
64
    public function dumpTemplate($templateFilePath, $outputFilePath, $overwrite = false)
65
    {
66
        $this->output->write("            <info>$outputFilePath</info> : ");
67
        $fileExist = $this->fs->exists($outputFilePath);
68
        $process = true;
69
        if ($fileExist) {
70
            if (false === $this->forceOverride && true === $this->skipExisting) {
71
                $this->output->writeln('<comment>Skipped !</comment>');
72
                $process = false;
73
            } else {
74
                $process = false;
75
                if (true === $this->forceOverride) {
76
                    $process = true;
77
                    $this->output->writeln('<comment>Overriden !</comment>');
78
                } elseif ($this->doOverwrite()) {
79
                    $process = true;
80
                }
81
            }
82
        }
83
        if (true === $process) {
84
            parent::dumpTemplate($templateFilePath, $outputFilePath);
85
            if (false === $fileExist) {
86
                $this->output->writeln('<info>Done</info>');
87
            }
88
        }
89
    }
90
91
    /**@return bool
92
     */
93
    protected function doOverwrite()
94
    {
95
        $question = new ConfirmationQuestion(
96
            '<question>Overwrite (y/n) ? [n]</question>',
97
            false
98
        );
99
100
        return $this->questionHelper->ask($this->input, $this->output, $question);
101
    }
102
}
103