Failed Conditions
Push — master ( 07197c...02c092 )
by Yo
02:21
created

InitRepositoryProcessor   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 4
dl 0
loc 129
ccs 0
cts 83
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
B process() 0 19 5
B processFolder() 0 23 6
A processFile() 0 12 3
B processOrNot() 0 21 5
A displayTemplate() 0 5 1
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