Completed
Push — 57-formatter-per-extension ( 73b3c8 )
by Nicolas
40:17 queued 36:16
created

Rollback   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 13.16 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 7
Bugs 1 Features 2
Metric Value
wmc 9
c 7
b 1
f 2
lcom 1
cbo 4
dl 10
loc 76
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 13 1
A execute() 0 7 1
B processInputs() 10 29 4
A launchRollback() 0 11 2

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 Karma\Command;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Karma\Application;
10
use Karma\Command;
11
12
class Rollback extends Command
13
{
14
    private
15
        $dryRun;
16
    
17
    public function __construct(Application $app)
18
    {
19
        parent::__construct($app);
20
        
21
        $this->dryRun = false;
22
    }
23
    
24
    protected function configure()
25
    {
26
        parent::configure();
27
        
28
        $this
29
            ->setName('rollback')
30
            ->setDescription('Restore files from backup ones')
31
            
32
            ->addArgument('sourcePath', InputArgument::OPTIONAL, 'source path to hydrate')
33
            
34
            ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Simulation mode')
35
        ;
36
    }
37
    
38
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        parent::execute($input, $output);
41
        
42
        $this->processInputs($input);
43
        $this->launchRollback();
44
    }
45
    
46
    private function processInputs(InputInterface $input)
47
    {        
48
        $sourcePath = $input->getArgument('sourcePath');
49 View Code Duplication
        if($sourcePath === null)
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...
50
        {
51
            $profile = $this->app['profile'];
52
            if($profile->hasSourcePath() !== true)
53
            {
54
                throw new \RuntimeException('Missing argument sourcePath');
55
            }
56
        
57
            $sourcePath = $profile->getSourcePath();
58
        }
59
        
60
        $this->output->writeln(sprintf(
61
            '<info>Rollback <comment>%s</comment></info>',
62
            $sourcePath
63
        ));
64
        
65
        $this->app['sources.path'] = $sourcePath;
66
        
67
        if($input->getOption('dry-run'))
68
        {
69
            $this->output->writeln("<fg=cyan>Run in dry-run mode</fg=cyan>");
70
            $this->dryRun = true;
71
        }
72
        
73
        $this->output->writeln('');
74
    }
75
    
76
    private function launchRollback()
77
    {
78
        $hydrator = $this->app['hydrator'];
79
        
80
        if($this->dryRun === true)
81
        {
82
            $hydrator->setDryRun();
83
        }
84
        
85
        $hydrator->rollback();
86
    }
87
}