Passed
Push — nln-php7 ( 5c4f20...6ce259 )
by Nicolas
03:34
created

Rollback   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 3
dl 0
loc 76
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0

5 Methods

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