RollbackCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 9
Bugs 4 Features 1
Metric Value
wmc 5
c 9
b 4
f 1
lcom 1
cbo 7
dl 0
loc 58
ccs 0
cts 45
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A execute() 0 17 2
B rollback() 0 29 2
1
<?php
2
3
namespace Onigoetz\Deployer\Command;
4
5
use Net_SFTP;
6
use Onigoetz\Deployer\Configuration\Environment;
7
use Onigoetz\Deployer\RemoteActionRunner;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class RollbackCommand extends BaseCommand
13
{
14
    protected function configure()
15
    {
16
        $this
17
            ->setName('server:rollback')
18
            ->setDescription('Rollback to the release before')
19
            ->addArgument('to', InputArgument::REQUIRED, 'The environment to rollback');
20
    }
21
22
    protected function execute(InputInterface $input, OutputInterface $output)
23
    {
24
        if (!defined('VERBOSE')) {
25
            define('VERBOSE', $input->getOption('verbose'));
26
        }
27
28
        $environment = $this->getEnvironment($input->getArgument('to'), $output);
29
30
        $this->allServers(
31
            $environment,
32
            $input,
33
            $output,
34
            function ($ssh) use ($environment, $output) {
35
                $this->rollback($output, $ssh, $environment);
36
            }
37
        );
38
    }
39
40
    protected function rollback(OutputInterface $output, Net_SFTP $ssh, Environment $environment)
41
    {
42
        $runner = new RemoteActionRunner($output, $ssh);
43
44
        $previous = trim($ssh->exec('cat ' . $environment->getDirectories()->getRoot() . '/previous'));
45
46
        if ($previous == '') {
47
            $output->writeln('<error>Cannot find previous file !!!</error>');
48
49
            return;
50
        }
51
52
        $actions = [
53
            'Removing the symlink of the release to rollback' => [
54
                'action' => 'rmfile',
55
                'file' => $environment->getDirectories()->getDeploy(),
56
            ],
57
            'Link it again to the snapshot ' . $previous => [
58
                'action' => 'symlink',
59
                'target' => $previous,
60
                'link_name' => $environment->getDirectories()->getDeploy(),
61
            ],
62
        ];
63
64
        $output->writeln('Previous snapshot : ' . $previous);
65
        $output->writeln("Reverting...\n", 'blue');
66
        $this->runActions($runner, $actions, $output, $environment->getSubstitutions($previous));
67
        $output->writeln("Done\n");
68
    }
69
}
70