buildAdditionalNamespaceQuestion()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
namespace Samurai\Project\Question;
3
4
use ICanBoogie\Inflector;
5
use Samurai\Project\Package;
6
use Samurai\Task\ITask;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Question\ConfirmationQuestion;
10
use Symfony\Component\Console\Question\Question as SimpleQuestion;
11
12
/**
13
 * Class PackageQuestion
14
 * @package Samurai\Project\Question
15
 * @author Raphaël Lefebvre <[email protected]>
16
 */
17
class PackageQuestion extends Question
18
{
19
    /**
20
     * @param InputInterface $input
21
     * @param OutputInterface $output
22
     * @return int
23
     */
24 2
    public function execute(InputInterface $input, OutputInterface $output)
25
    {
26 2
        if($this->confirmFirstPackage($input, $output)){
27 1
            $this->setPackage($input, $output, true);
28 1
            while ($this->confirmAdditionalPackages($input, $output)) {
29
                $this->setPackage($input, $output, false);
30
            }
31 1
        }
32 2
        return ITask::NO_ERROR_CODE;
33
    }
34
35
    /**
36
     * @param InputInterface $input
37
     * @param OutputInterface $output
38
     * @return mixed
39
     */
40 2
    private function confirmFirstPackage(InputInterface $input, OutputInterface $output)
41
    {
42 2
        return $this->ask(
43 2
            $input,
44 2
            $output,
45 2
            new ConfirmationQuestion('<question>Do you want to add a namespace?[y]</question>')
46 2
        );
47
    }
48
49
    /**
50
     * @param InputInterface $input
51
     * @param OutputInterface $output
52
     * @return mixed
53
     */
54 1
    private function confirmAdditionalPackages(InputInterface $input, OutputInterface $output)
55
    {
56 1
        return $this->ask(
57 1
            $input,
58 1
            $output,
59 1
            new ConfirmationQuestion('<question>Do you want to add another namespace?[n]</question>', false)
60 1
        );
61
    }
62
63
    /**
64
     * @param InputInterface $input
65
     * @param OutputInterface $output
66
     * @param bool $isFirstNamespace
67
     */
68 1
    private function setPackage(InputInterface $input, OutputInterface $output, $isFirstNamespace)
69
    {
70 1
        $package = new Package();
71 1
        if($isFirstNamespace){
72 1
            $package->setNamespace($this->askForFirstNamespace($input, $output));
73 1
        }else{
74
            $package->setNamespace($this->askForAdditionalNamespace($input, $output));
75
        }
76 1
        $package->setPathList(explode(',', $this->askForPathList($input, $output)));
77 1
        $this->getProject()->addPackage($package);
78 1
    }
79
80
    /**
81
     * @param $input
82
     * @param $output
83
     * @return mixed
84
     */
85 1
    private function askForFirstNamespace($input, $output)
86
    {
87 1
        return $this->ask(
88 1
            $input,
89 1
            $output,
90 1
            $this->buildFirstNamespaceQuestion()
91 1
        );
92
    }
93
94
    /**
95
     * @param $input
96
     * @param $output
97
     * @return mixed
98
     */
99
    private function askForAdditionalNamespace($input, $output)
100
    {
101
        return $this->ask(
102
            $input,
103
            $output,
104
            $this->buildAdditionalNamespaceQuestion()
105
        );
106
    }
107
108
    /**
109
     * @return SimpleQuestion
110
     */
111 1
    private function buildFirstNamespaceQuestion()
112
    {
113 1
        $defaultNamespace = $this->retrieveDefaultNamespace();
114 1
        return new SimpleQuestion(
115 1
            '<question>Enter the namespace:['.$defaultNamespace.']</question>',
116
            $defaultNamespace
117 1
        );
118
    }
119
120
    /**
121
     * @return SimpleQuestion
122
     */
123
    private function buildAdditionalNamespaceQuestion()
124
    {
125
        return new SimpleQuestion('<question>Enter the namespace:</question>');
126
    }
127
128
    /**
129
     * @param $input
130
     * @param $output
131
     * @return mixed
132
     */
133 1
    private function askForPathList($input, $output)
134
    {
135 1
        return $this->ask(
136 1
            $input,
137 1
            $output,
138 1
            $this->buildPathListQuestion()
139 1
        );
140
    }
141
142
    /**
143
     * @return SimpleQuestion
144
     */
145 1
    private function buildPathListQuestion()
146
    {
147 1
        return new SimpleQuestion(
148 1
            '<question>Enter the path list (path separated by a comma):[src/,tests/]</question>',
149
            'src/,tests/'
150 1
        );
151
    }
152
153
    /**
154
     * @return string
155
     */
156 1
    private function retrieveDefaultNamespace()
157
    {
158 1
        $inflector = Inflector::get();//todo di?
159 1
        return $inflector->camelize(strtr(
160 1
            explode('/', $this->getProject()->getName())[1],
161
            [
162 1
                '-' => '\\',
163 1
                '_' => '\\',
164
            ]
165 1
        ));//todo test \
166
    }
167
}
168