InitRepositoryProcessor::processOrNot()   B
last analyzed

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 0
Metric Value
dl 0
loc 21
ccs 0
cts 20
cp 0
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 14
nc 4
nop 1
crap 30
1
<?php
2
namespace Yoanm\InitPhpRepository\Processor;
3
4
use Symfony\Component\Console\Question\ConfirmationQuestion;
5
use Yoanm\InitPhpRepository\Helper\CommandHelper;
6
use Yoanm\InitPhpRepository\Model\FolderTemplate;
7
use Yoanm\InitPhpRepository\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
            $this->displayTemplate($template);
51
52
            if ($template instanceof FolderTemplate) {
53
                $this->processFolder($template);
54
            } else {
55
                $this->processFile($template);
56
            }
57
58
            $currentType = $this->helper->resolveCurrentType($template);
59
        }
60
    }
61
62
    /**
63
     * @param FolderTemplate $template
64
     */
65
    protected function processFolder(FolderTemplate $template)
66
    {
67
        $targetExist = false;
68
        $process = false;
69
70
        foreach ($template->getFileList() as $subTemplate) {
71
            $targetExist = $this->helper->targetExist($subTemplate);
72
            $process = $this->processOrNot($targetExist);
73
            if (false === $process) {
74
                break;
75
            }
76
        }
77
78
        if (true === $process) {
79
            foreach ($template->getFileList() as $subTemplate) {
80
                $this->helper->dump($subTemplate);
81
            }
82
        }
83
84
        if (false === $targetExist) {
85
            $this->helper->display('<info>Done</info>');
86
        }
87
    }
88
89
    /**
90
     * @param Template $template
91
     */
92
    protected function processFile(Template $template)
93
    {
94
        $targetExist = $this->helper->targetExist($template);
95
96
        if (true === $this->processOrNot($targetExist)) {
97
            $this->helper->dump($template);
98
        }
99
100
        if (false === $targetExist) {
101
            $this->helper->display('<info>Done</info>');
102
        }
103
    }
104
105
    /**
106
     * @param bool $targetExist
107
     *
108
     * @return bool
109
     */
110
    protected function processOrNot($targetExist)
111
    {
112
        $process = true;
113
        if ($targetExist) {
114
            if (false === $this->forceOverride && true === $this->skipExisting) {
115
                $this->helper->display('<comment>Skipped !</comment>');
116
                $process = false;
117
            } else {
118
                if (true === $this->forceOverride) {
119
                    $process = true;
120
                    $this->helper->display('<comment>Overriden !</comment>');
121
                } else {
122
                    $process = $this->helper->ask(
123
                        new ConfirmationQuestion('<question>Overwrite ? [n]</question>', false)
124
                    );
125
                }
126
            }
127
        }
128
129
        return $process;
130
    }
131
132
    /**
133
     * @param Template $template
134
     */
135
    protected function displayTemplate(Template $template)
136
    {
137
        $this->helper->display(sprintf('<comment>%s</comment>', $template->getId()), 2, false);
138
        $this->helper->display(sprintf(' - <info>./%s</info> : ', $template->getTarget()), 0, false);
139
    }
140
}
141