Completed
Push — master ( e32f5b...d93af6 )
by juan
02:37
created

AddProjectsCommand::execute()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 6
eloc 18
c 3
b 0
f 1
nc 6
nop 2
dl 0
loc 28
rs 8.439
1
<?php
2
3
namespace Juanber84\Console\Command;
4
5
use Juanber84\Services\DatabaseService;
6
use Symfony\Component\Console\Command\Command;
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;
11
12
class AddProjectsCommand extends Command
13
{
14
    const COMMAND_NAME = 'add-project';
15
    const COMMAND_DESC = 'Add Deploy Project.';
16
17
    const DIRECTORY = '.dep';
18
    const DB = 'db.json';
19
20
    private $databaseService;
21
22
    public function __construct(DatabaseService $databaseService)
23
    {
24
        parent::__construct();
25
26
        $this->databaseService = $databaseService;
27
    }
28
29
    protected function configure()
30
    {
31
        $this
32
            ->setName(self::COMMAND_NAME)
33
            ->setDescription(self::COMMAND_DESC);
34
    }
35
36
    protected function execute(InputInterface $input, OutputInterface $output)
37
    {
38
        $helper = $this->getHelper('question');
39
40
        $question = new Question('<question>What is the project key?</question>: ');
41
        do {
42
            $nameOfProject = trim($helper->ask($input, $output, $question));
43
        } while (empty($nameOfProject));
44
45
        $question = new ConfirmationQuestion('Continue with this action? <info>Y/n</info> ', true);
46
        if (!$helper->ask($input, $output, $question)) {
47
            return;
48
        }
49
50
        $jsonDb = $this->databaseService->getProjects();
51
        if (array_key_exists($nameOfProject,$jsonDb)) {
52
            $question = new ConfirmationQuestion('<error>This project exist. Do you want override it?</error> <info>Y/n</info> ', false);
53
            if (!$helper->ask($input, $output, $question)) {
54
                return;
55
            }
56
        }
57
58
        if ($this->databaseService->addProject($nameOfProject, getcwd())){
59
            $output->writeln('<info>OK. Project added.</info>');
60
        } else {
61
            $output->writeln('<error>KO. Error</error>');
62
        }
63
    }
64
}