HomepageQuestion::buildQuestion()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 7
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
namespace Samurai\Project\Question;
3
4
use Samurai\Task\ITask;
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Console\Question\Question as SimpleQuestion;
8
9
/**
10
 * Class HomePageQuestion
11
 * @package Samurai\Project\Question
12
 * @author Raphaël Lefebvre <[email protected]>
13
 */
14 View Code Duplication
class HomepageQuestion extends Question
15
{
16
    /**
17
     * @param InputInterface $input
18
     * @param OutputInterface $output
19
     * @return int
20
     */
21 5
    public function execute(InputInterface $input, OutputInterface $output)
22
    {
23
        try {
24 5
            $this->getProject()->setHomepage($this->ask($input, $output, $this->buildQuestion()));
25 5
        }catch(\Exception $e){
26 1
            $this->getProject()->setHomepage('');
27
        }
28 5
        return ITask::NO_ERROR_CODE;
29
    }
30
31
    /**
32
     * @return SimpleQuestion
33
     */
34 5
    private function buildQuestion()
35
    {
36 5
        $question = new SimpleQuestion('<question>Enter your project homepage:</question>');
37 5
        $question->setValidator($this->buildValidator());
38 5
        $question->setMaxAttempts(3);
39 5
        return $question;
40
    }
41
42
    /**
43
     * @return callable
44
     */
45
    private function buildValidator()
46
    {
47 5
        return function ($answer) {
48 3
            if ($answer && !filter_var($answer, FILTER_VALIDATE_URL)) {
49 1
                throw new \RuntimeException('Error: format not valid');
50
            }
51 2
            return $answer;
52 5
        };
53
    }
54
55
}
56