Completed
Push — master ( bd4c23...b341c0 )
by Filipe
10:29
created

AskForWebRoot::check()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 3
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Console\Command\Task;
11
12
use Slick\Mvc\Console\Command\TaskInterface;
13
use Slick\Mvc\Console\Exception\OperationAbortedException;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Helper\QuestionHelper;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Question\ConfirmationQuestion;
19
use Symfony\Component\Console\Question\Question;
20
21
/**
22
 * Ask For Web Root
23
 *
24
 * @package Slick\Mvc\Console\Command\Task
25
 * @author  Filipe Silva <[email protected]>
26
 */
27
class AskForWebRoot implements TaskInterface
28
{
29
    /**
30
     * @var Command
31
     */
32
    private $command;
33
34
    /**
35
     * Creates an Ask For Web Root task
36
     *
37
     * @param Command $command
38
     */
39
    public function __construct(Command $command)
40
    {
41
        $this->command = $command;
42
    }
43
44
45
    /**
46
     * Executes the current task.
47
     *
48
     * This method can return the task execution result. For example if this
49
     * task is asking for user input it should return its input.
50
     *
51
     * @param InputInterface $input An InputInterface instance
52
     * @param OutputInterface $output An OutputInterface instance
53
     *
54
     * @return mixed|false
55
     */
56
    public function execute(InputInterface $input, OutputInterface $output)
57
    {
58
        /** @var QuestionHelper $helper */
59
        $helper = $this->command->getHelper('question');
60
        $default = 'webroot';
61
        $question = "What's the application document root? ({$default}): ";
62
        $docRoot = $helper->ask(
63
            $input,
64
            $output,
65
            new Question($question, $default));
66
67
        return $this->check($docRoot, $input, $output) ? $docRoot : false;
68
    }
69
70
    /**
71
     * Checks if the document root has an index.php file. If so overwrite?
72
     *
73
     * @param string          $docRoot
74
     * @param InputInterface  $input
75
     * @param OutputInterface $output
76
     *
77
     * @return bool
78
     */
79
    public function check($docRoot, InputInterface $input, OutputInterface $output)
80
    {
81
        if (!file_exists(getcwd()."/$docRoot")) return true;
82
83
        /** @var QuestionHelper $helper */
84
        $helper = $this->command->getHelper('question');
85
86
        $question = "There is an 'index.php' file in this folder, overwrite it? (y/N): ";
87
        $default = false;
88
        $question = new ConfirmationQuestion($question, $default, '/(y|yes)/i');
89
90
        if (!$helper->ask($input, $output, $question)) {
91
            throw new OperationAbortedException(
92
                "Operation aborted! No file file will be overwritten."
93
            );
94
        }
95
96
        return true;
97
    }
98
}