Failed Conditions
Pull Request — master (#9)
by Yo
02:00
created

InitRepositoryProcessor::processOrNot()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 21
ccs 0
cts 20
cp 0
rs 8.7624
cc 5
eloc 14
nc 4
nop 1
crap 30
1
<?php
2
namespace Yoanm\DefaultPhpRepository\Processor;
3
4
use Symfony\Component\Console\Question\ConfirmationQuestion;
5
use Yoanm\DefaultPhpRepository\Helper\CommandHelper;
6
use Yoanm\DefaultPhpRepository\Model\FolderTemplate;
7
use Yoanm\DefaultPhpRepository\Model\Template;
8
9
/**
10
 * Class InitRepositoryProcessor
11
 */
12
class InitRepositoryProcessor
13
{
14
    /** @var CommandHelper */
15
    private $helper;
16
    /** @var bool */
17
    private $skipExisting;
18
    /** @var bool */
19
    private $forceOverride;
20
    /** @var array */
21
    private $idList = [];
22
23
    /**
24
     * @param CommandHelper $helper
25
     * @param bool          $skipExisting
26
     * @param bool          $forceOverride
27
     * @param array         $idList
28
     */
29
    public function __construct(
30
        CommandHelper $helper,
31
        $skipExisting = true,
32
        $forceOverride = false,
33
        $idList = []
34
    ) {
35
        $this->helper = $helper;
36
37
        $this->skipExisting = $skipExisting;
38
        $this->forceOverride = $forceOverride;
39
        $this->idList = $idList;
40
    }
41
42
    public function process()
43
    {
44
        $currentType = null;
45
        foreach ($this->helper->getTemplateList() as $template) {
46
            if (count($this->idList) && !in_array($template->getId(), $this->idList)) {
47
                continue;
48
            }
49
            $this->helper->displayHeader($template, $currentType);
50
51
            $currentType = $this->helper->resolveCurrentType($template);
52
53
            $this->displayTemplate($template);
54
55
            if ($template instanceof FolderTemplate) {
56
                $targetExist = $this->processFolder($template);
0 ignored issues
show
Unused Code introduced by
$targetExist is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
57
            } else {
58
                $targetExist = $this->processFile($template);
0 ignored issues
show
Unused Code introduced by
$targetExist is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
59
            }
60
        }
61
    }
62
63
    /**
64
     * @param FolderTemplate $template
65
     *
66
     * @return bool
67
     */
68
    protected function processFolder(FolderTemplate $template)
69
    {
70
        $targetExist = false;
71
        $process = false;
72
73
        foreach ($template->getFileList() as $subTemplate) {
74
            $targetExist = $this->helper->targetExist($subTemplate);
75
            $process = $this->processOrNot($targetExist);
76
            if (false === $process) {
77
                break;
78
            }
79
        }
80
81
        if (true === $process) {
82
            foreach ($template->getFileList() as $subTemplate) {
83
                $this->helper->dump($subTemplate);
84
            }
85
        }
86
87
        if (false === $targetExist) {
88
            $this->helper->display('<info>Done</info>');
89
        }
90
    }
91
92
    /**
93
     * @param Template $template
94
     * @return bool
95
     */
96
    protected function processFile(Template $template)
97
    {
98
        $targetExist = $this->helper->targetExist($template);
99
100
        if (true === $this->processOrNot($targetExist)) {
101
            $this->helper->dump($template);
102
        }
103
104
        if (false === $targetExist) {
105
            $this->helper->display('<info>Done</info>');
106
        }
107
    }
108
109
    /**
110
     * @param bool $targetExist
111
     *
112
     * @return bool
113
     */
114
    protected function processOrNot($targetExist)
115
    {
116
        $process = true;
117
        if ($targetExist) {
118
            if (false === $this->forceOverride && true === $this->skipExisting) {
119
                $this->helper->display('<comment>Skipped !</comment>');
120
                $process = false;
121
            } else {
122
                if (true === $this->forceOverride) {
123
                    $process = true;
124
                    $this->helper->display('<comment>Overriden !</comment>');
125
                } else {
126
                    $process = $this->helper->ask(
127
                        new ConfirmationQuestion('<question>Overwrite ? [n]</question>', false)
128
                    );
129
                }
130
            }
131
        }
132
133
        return $process;
134
    }
135
136
    /**
137
     * @param Template $template
138
     */
139
    protected function displayTemplate(Template $template)
140
    {
141
        $this->helper->display(sprintf('<comment>%s</comment>', $template->getId()), 2, false);
142
        $this->helper->display(sprintf(' - <info>./%s</info> : ', $template->getTarget()), 0, false);
143
    }
144
}
145