RollbackCommand::initialize()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 9
nc 4
nop 2
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Bencagri\Artisan\Deployer\Command;
4
5
use Bencagri\Artisan\Deployer\Context;
6
use Bencagri\Artisan\Deployer\Helper\ConfigPathGuesser;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class RollbackCommand extends Command
14
{
15
    private $projectDir;
16
    private $logDir;
17
    private $configFilePath;
18
19
    public function __construct()
20
    {
21
        $this->projectDir = base_path();
22
        $this->logDir = base_path('storage/logs');
23
24
        parent::__construct();
25
    }
26
27
    protected function configure()
28
    {
29
        $this
30
            ->setName('rollback')
31
            ->setDescription('Deploys a Laravel application to one or more remote servers.')
32
            ->setHelp('...')
33
            ->addArgument('stage', InputArgument::OPTIONAL, 'The stage to roll back ("production", "staging", etc.)', 'prod')
34
            ->addOption('configuration', 'c', InputOption::VALUE_REQUIRED, 'Load configuration from the given file path')
35
            ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Shows the commands to perform the roll back without actually executing them')
36
        ;
37
    }
38
39
    protected function initialize(InputInterface $input, OutputInterface $output)
40
    {
41
        $customConfigPath = $input->getOption('configuration');
42
        if (null !== $customConfigPath && !is_readable($customConfigPath)) {
43
            throw new \RuntimeException(sprintf("The given configuration file ('%s') does not exist or it's not readable.", $customConfigPath));
44
        }
45
46
        if (null !== $customConfigPath && is_readable($customConfigPath)) {
47
            return $this->configFilePath = $customConfigPath;
48
        }
49
50
        $defaultConfigPath = ConfigPathGuesser::guess($this->projectDir, $input->getArgument('stage'));
51
        if (is_readable($defaultConfigPath)) {
52
            return $this->configFilePath = $defaultConfigPath;
53
        }
54
55
        throw new \RuntimeException(sprintf("The default configuration file does not exist or it's not readable, and no custom configuration file was given either. Create the '%s' configuration file and run this command again.", $defaultConfigPath));
56
    }
57
58
    protected function execute(InputInterface $input, OutputInterface $output)
59
    {
60
        $logFilePath = sprintf('%s/deploy_%s.log', $this->logDir, $input->getArgument('stage'));
61
        $context = new Context($input, $output, $this->projectDir, $logFilePath, true === $input->getOption('dry-run'), $output->isVerbose());
62
63
        $deployer = include $this->configFilePath;
64
        $deployer->initialize($context);
65
        $deployer->doRollback();
66
    }
67
}
68