Completed
Push — nln-php7 ( 1a6b54...2856d9 )
by Nicolas
02:57
created

Rollback   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 2
dl 0
loc 76
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0
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 bool
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
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(): void
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): int
41
    {
42 4
        parent::execute($input, $output);
43
44 4
        $this->processInputs($input);
45 3
        $this->launchRollback();
46
47 3
        return 0;
48
    }
49
50 4
    private function processInputs(InputInterface $input): void
51
    {
52 4
        $sourcePath = $input->getArgument('sourcePath');
53 4
        if($sourcePath === null)
54
        {
55 2
            $profile = $this->app['profile'];
56 2
            if($profile->hasSourcePath() !== true)
57
            {
58 1
                throw new \RuntimeException('Missing argument sourcePath');
59
            }
60
61 1
            $sourcePath = $profile->getSourcePath();
62
        }
63
64 3
        $this->output->writeln(sprintf(
65 3
            '<info>Rollback <comment>%s</comment></info>',
66
            $sourcePath
67
        ));
68
69 3
        $this->app['sources.path'] = $sourcePath;
70
71 3
        if($input->getOption('dry-run'))
72
        {
73 1
            $this->output->writeln("<fg=cyan>Run in dry-run mode</fg=cyan>");
74 1
            $this->dryRun = true;
75
        }
76
77 3
        $this->output->writeln('');
78 3
    }
79
80 3
    private function launchRollback(): void
81
    {
82 3
        $hydrator = $this->app['hydrator'];
83
84 3
        if($this->dryRun === true)
85
        {
86 1
            $hydrator->setDryRun();
87
        }
88
89 3
        $hydrator->rollback();
90 3
    }
91
}
92