Completed
Push — master ( 066340...9f64e8 )
by juan
02:23
created

AddProjectsCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 11 1
A execute() 0 20 3
1
<?php
2
3
namespace Juanber84\Console\Command;
4
5
use Juanber84\Services\DatabaseService;
6
use Juanber84\Texts\AddProjectsCommandText;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Question\ConfirmationQuestion;
11
use Symfony\Component\Console\Question\Question;
12
use Symfony\Component\Console\Input\InputArgument;
13
14
class AddProjectsCommand extends Command
15
{
16
    use NameProjectTrait;
17
    use ConfirmationProccessTrait;
18
19
    const COMMAND_NAME = 'add-project';
20
    const COMMAND_DESC = 'Add Deploy Project.';
21
22
    const DIRECTORY = '.dep';
23
    const DB = 'db.json';
24
25
    private $databaseService;
26
27
    public function __construct(DatabaseService $databaseService)
28
    {
29
        parent::__construct();
30
31
        $this->databaseService = $databaseService;
32
    }
33
34
    protected function configure()
35
    {
36
        $this
37
            ->setName(self::COMMAND_NAME)
38
            ->setDescription(self::COMMAND_DESC)
39
            ->addArgument(
40
                'project',
41
                InputArgument::OPTIONAL,
42
                'What\'s the name of project?'
43
            );
44
    }
45
46
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48
        $helper = $this->getHelper('question');
49
        $nameOfProject = $input->getArgument('project');
50
51
        $nameOfProject = $this->getNameOfProject($input, $output, $helper, $nameOfProject);
52
        $this->confirmationProcess($input, $output, $helper, AddProjectsCommandText::KO_ABORTED);
53
54
        $jsonDb = $this->databaseService->getProjects();
55
56
        if (array_key_exists($nameOfProject,$jsonDb)) {
57
            $this->confirmationProcess($input, $output, $helper, AddProjectsCommandText::KO_ABORTED, '<error>This project exist. Do you want override it?</error> <info>Y/n</info> ');
58
        }
59
60
        if ($this->databaseService->addProject($nameOfProject, getcwd())){
61
            $output->writeln('<info>'.AddProjectsCommandText::OK_ADDED.'</info>');
62
        } else {
63
            $output->writeln('<error>'.AddProjectsCommandText::KO_ERROR.'</error>');
64
        }
65
    }
66
}