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

Rollback::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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