Completed
Push — master ( d93af6...bec704 )
by juan
04:18
created

AddProjectsCommand::execute()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 31
Code Lines 20

Duplication

Lines 6
Ratio 19.35 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 7
eloc 20
c 4
b 0
f 1
nc 12
nop 2
dl 6
loc 31
rs 6.7272
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
    const COMMAND_NAME = 'add-project';
16
    const COMMAND_DESC = 'Add Deploy Project.';
17
18
    const DIRECTORY = '.dep';
19
    const DB = 'db.json';
20
21
    private $databaseService;
22
23
    public function __construct(DatabaseService $databaseService)
24
    {
25
        parent::__construct();
26
27
        $this->databaseService = $databaseService;
28
    }
29
30
    protected function configure()
31
    {
32
        $this
33
            ->setName(self::COMMAND_NAME)
34
            ->setDescription(self::COMMAND_DESC)
35
            ->addArgument(
36
                'project',
37
                InputArgument::OPTIONAL,
38
                'What\'s the name of project?'
39
            );
40
    }
41
42
    protected function execute(InputInterface $input, OutputInterface $output)
43
    {
44
        $helper = $this->getHelper('question');
45
46
        $nameOfProject = $input->getArgument('project');
47 View Code Duplication
        if (!$nameOfProject) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
            $question = new Question('<question>What is the project key?</question>: ');
49
            do {
50
                $nameOfProject = trim($helper->ask($input, $output, $question));
51
            } while (empty($nameOfProject));
52
        }
53
54
        $question = new ConfirmationQuestion('Continue with this action? <info>Y/n</info> ', true);
55
        if (!$helper->ask($input, $output, $question)) {
56
            return;
57
        }
58
59
        $jsonDb = $this->databaseService->getProjects();
60
        if (array_key_exists($nameOfProject,$jsonDb)) {
61
            $question = new ConfirmationQuestion('<error>This project exist. Do you want override it?</error> <info>Y/n</info> ', false);
62
            if (!$helper->ask($input, $output, $question)) {
63
                return;
64
            }
65
        }
66
67
        if ($this->databaseService->addProject($nameOfProject, getcwd())){
68
            $output->writeln('<info>OK. Project added.</info>');
69
        } else {
70
            $output->writeln('<error>KO. Error</error>');
71
        }
72
    }
73
}