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

AddProjectsCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 7
Bugs 1 Features 2
Metric Value
c 7
b 1
f 2
dl 0
loc 58
rs 10
wmc 7
lcom 1
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 11 1
B execute() 0 26 5
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
}