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

AddProjectsCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 9.84 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 11 1
C execute() 6 31 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}