Completed
Push — master ( 1c0bf9...10779d )
by juan
02:27
created

AddProjectsCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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