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

InitCommandProcessor::process()   C

Complexity

Conditions 11
Paths 18

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 38
ccs 0
cts 35
cp 0
rs 5.2653
cc 11
eloc 24
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\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\Helper\TemplateHelper;
10
use Yoanm\DefaultPhpRepository\Model\FolderTemplate;
11
use Yoanm\DefaultPhpRepository\Model\Template;
12
13
/**
14
 * Class InitCommandProcessor
15
 */
16
class InitCommandProcessor extends CommandProcessor
17
{
18
    /** @var TemplateHelper */
19
    private $templateHelper;
20
    /** @var Filesystem */
21
    private $fileSystem;
22
    /** @var string */
23
    private $rootPath;
24
    /** @var bool */
25
    private $skipExisting;
26
    /** @var bool */
27
    private $forceOverride;
28
    /** @var array */
29
    private $idList = [];
30
31
    /**
32
     * @param QuestionHelper     $questionHelper
33
     * @param InputInterface     $input
34
     * @param OutputInterface    $output
35
     * @param TemplateHelper     $templateHelper
36
     * @param Template[]         $templateList
37
     * @param bool               $skipExisting
38
     * @param bool               $forceOverride
39
     * @param array              $idList
40
     */
41
    public function __construct(
42
        QuestionHelper $questionHelper,
43
        InputInterface $input,
44
        OutputInterface $output,
45
        TemplateHelper $templateHelper,
46
        array $templateList,
47
        $skipExisting = true,
48
        $forceOverride = false,
49
        $idList = [],
50
        $rootPath = '.'
51
    ) {
52
        parent::__construct($questionHelper, $input, $output, $templateList);
53
54
        $this->templateHelper = $templateHelper;
55
        $this->rootPath = realpath($rootPath);
56
        $this->fileSystem = new Filesystem();
57
58
        $this->skipExisting = $skipExisting;
59
        $this->forceOverride = $forceOverride;
60
        $this->idList = $idList;
61
    }
62
63
    public function process()
64
    {
65
        $currentType = null;
66
        foreach ($this->getTemplateList() as $template) {
67
            if (count($this->idList) && !in_array($template->getId(), $this->idList)) {
68
                continue;
69
            }
70
            $this->displayHeader($template, $currentType);
71
72
            $currentType = $this->resolveCurrentType($template);
73
            $this->displayTemplate($template);
74
            $fileExist = false;
75
            if ($template instanceof FolderTemplate) {
76
                $process = false;
77
                foreach ($template->getFileList() as $subTemplate) {
78
                    list($fileExist, $process) = $this->validateDump($subTemplate);
79
80
                    if (false === $process) {
81
                        break;
82
                    }
83
                }
84
                if (true === $process) {
85
                    foreach ($template->getFileList() as $subTemplate) {
86
                        $this->dumpTemplate($subTemplate);
87
                    }
88
                }
89
            } else {
90
                list($fileExist, $process) = $this->validateDump($template);
91
92
                if (true === $process) {
93
                    $this->dumpTemplate($template);
94
                }
95
            }
96
            if (false === $fileExist) {
97
                $this->display('<info>Done</info>');
98
            }
99
        }
100
    }
101
102
    /**
103
     * @param Template $template
104
     */
105
    protected function displayTemplate(Template $template)
106
    {
107
        $this->display(sprintf('<comment>%s</comment>', $template->getId()), 2, false);
108
        $targetRelativePath = sprintf('./%s', $template->getTarget());
109
        $this->display(sprintf(' - <info>%s</info> : ', $targetRelativePath), 0, false);
110
    }
111
112
    /**
113
     * @param Template $template
114
     * @param string           $path
0 ignored issues
show
Bug introduced by
There is no parameter named $path. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
115
     */
116
    protected function dumpTemplate(Template $template)
117
    {
118
        $templateId = $template->getSource();
119
        if (null !== $template->getNamespace()) {
120
            $templateId = sprintf('@%s/%s', $template->getNamespace(), $template->getSource());
121
        }
122
        $this->templateHelper->dumpTemplate($templateId, $this->resolveTargetPath($template));
123
    }
124
125
    /**
126
     * @param string $target
0 ignored issues
show
Bug introduced by
There is no parameter named $target. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
127
     * @return array
128
     */
129
    protected function validateDump(Template $template)
130
    {
131
        $fileExist = $this->fileSystem->exists($this->resolveTargetPath($template));
132
        $process = true;
133
        if ($fileExist) {
134
            if (false === $this->forceOverride && true === $this->skipExisting) {
135
                $this->display('<comment>Skipped !</comment>');
136
                $process = false;
137
            } else {
138
                $process = false;
139
                if (true === $this->forceOverride) {
140
                    $this->display('<comment>Overriden !</comment>');
141
                    $process = true;
142
                } elseif ($this->doOverwrite()) {
143
                    $process = true;
144
                }
145
            }
146
        }
147
148
        return [$fileExist, $process];
149
    }
150
151
    /**@return bool
152
     */
153
    protected function doOverwrite()
154
    {
155
        return $this->ask(new ConfirmationQuestion('<question>Overwrite ? [n]</question>', false));
156
    }
157
158
    /**
159
     * @param Template $template
160
     *
161
     * @return string
162
     */
163
    protected function resolveTargetPath(Template $template)
164
    {
165
        return sprintf('%s/%s', $this->rootPath, $template->getTarget());
166
    }
167
}
168