Failed Conditions
Pull Request — master (#9)
by Yo
01:59
created

InitRepositoryProcessor::process()   C

Complexity

Conditions 11
Paths 18

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 41
ccs 0
cts 36
cp 0
rs 5.2653
cc 11
eloc 25
nc 18
nop 0
crap 132

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 = false;
57
                $process = false;
58
59
                foreach ($template->getFileList() as $subTemplate) {
60
                    $targetExist = $this->helper->targetExist($subTemplate);
61
                    $process = $this->processOrNot($targetExist);
62
                    if (false === $process) {
63
                        break;
64
                    }
65
                }
66
67
                if (true === $process) {
68
                    foreach ($template->getFileList() as $subTemplate) {
69
                        $this->helper->dump($subTemplate);
70
                    }
71
                }
72
            } else {
73
                $targetExist = $this->helper->targetExist($template);
74
                if (true === $this->processOrNot($targetExist)) {
75
                    $this->helper->dump($template);
76
                }
77
            }
78
            if (false === $targetExist) {
79
                $this->helper->display('<info>Done</info>');
80
            }
81
        }
82
    }
83
84
    /**
85
     * @param Template $template
86
     */
87
    protected function displayTemplate(Template $template)
88
    {
89
        $this->helper->display(sprintf('<comment>%s</comment>', $template->getId()), 2, false);
90
        $this->helper->display(sprintf(' - <info>./%s</info> : ', $template->getTarget()), 0, false);
91
    }
92
93
    /**
94
     * @param bool $targetExist
95
     *
96
     * @return bool
97
     */
98
    protected function processOrNot($targetExist)
99
    {
100
        $process = true;
101
        if ($targetExist) {
102
            if (false === $this->forceOverride && true === $this->skipExisting) {
103
                $this->helper->display('<comment>Skipped !</comment>');
104
                $process = false;
105
            } else {
106
                if (true === $this->forceOverride) {
107
                    $process = true;
108
                    $this->helper->display('<comment>Overriden !</comment>');
109
                } else {
110
                    $process = $this->helper->ask(
111
                        new ConfirmationQuestion('<question>Overwrite ? [n]</question>', false)
112
                    );
113
                }
114
            }
115
        }
116
117
        return $process;
118
    }
119
}
120