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

Rollback::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 13
rs 9.4286
cc 1
eloc 7
nc 1
nop 0
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
}