Passed
Push — master ( d56c43...736831 )
by Josh
04:39
created

Deploy::runLocalBlendMigration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 12
rs 10
1
<?php
2
3
namespace LCI\MODX\Orchestrator\Deploy;
4
5
use LCI\MODX\Orchestrator\Orchestrator;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\ArrayInput;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Question\ConfirmationQuestion;
11
12
class Deploy implements DeployInterface
13
{
14
    /** @var Command */
15
    protected $command;
16
17
    /**
18
     * Deploy constructor.
19
     * @param Command $application
20
     */
21
    public function __construct(Command $application)
22
    {
23
        $this->command = $application;
24
    }
25
26
    /**
27
     * @param OutputInterface $output
28
     */
29
    public function describe(OutputInterface $output)
30
    {
31
        $output->writeln('1. You will be prompted with a question if you would like to clear the MODX cache before running migrations');
32
        $output->writeln('2. Run all Blend migrations that are ready for all orchestrator packages');
33
        $output->writeln('3. Run all Blend migrations that are ready for your local project');
34
        $output->writeln('4. You will be prompted with a question if you would like to clear the MODX cache after the migrations have been completed.');
35
    }
36
37
    /**
38
     * @param InputInterface $input
39
     * @param OutputInterface $output
40
     * @throws \LCI\Blend\Exception\MigratorException
41
     */
42
    public function run(InputInterface $input, OutputInterface $output)
43
    {
44
        $this->runBefore($input, $output);
45
46
        // clear all cache Y/N?
47
        $helper = $this->command->getHelper('question');
48
        $question = new ConfirmationQuestion('Clear MODX cache before running migrations?', true);
49
50
        if ($helper->ask($input, $output, $question)) {
51
            $output->write($this->clearMODXCache($input, $output));
52
        }
53
54
        // run all package migrations
55
        Orchestrator::updateAllOrchestratorComposerPackages();
56
57
        // run local blend migrations
58
        $this->runLocalBlendMigration($input, $output);
59
60
        $helper = $this->command->getHelper('question');
61
        $question = new ConfirmationQuestion('Migrations have been ran, clear MODX cache again?', true);
62
63
        if ($helper->ask($input, $output, $question)) {
64
            $output->write($this->clearMODXCache($input, $output));
65
        }
66
67
        $this->runAfter($input, $output);
68
    }
69
70
    /**
71
     * @param InputInterface $input
72
     * @param OutputInterface $output
73
     * @return int
74
     * @throws \Exception
75
     */
76
    protected function clearMODXCache(InputInterface $input, OutputInterface $output)
77
    {
78
        $command = $this->command->getApplication()->find('console:clear');
79
80
        $arguments = [
81
            'command' => 'console:clear'
82
        ];
83
84
        /** @var  $greetInput */
85
        $greetInput = new ArrayInput($arguments);
86
87
        return $command->run($greetInput, $output);
88
    }
89
90
    /**
91
     * @param InputInterface $input
92
     * @param OutputInterface $output
93
     * @return int
94
     * @throws \Exception
95
     */
96
    protected function runLocalBlendMigration(InputInterface $input, OutputInterface $output)
97
    {
98
        $command = $this->command->getApplication()->find('blend:migrate');
99
100
        $arguments = [
101
            'command' => 'blend:migrate'
102
        ];
103
104
        /** @var  $greetInput */
105
        $greetInput = new ArrayInput($arguments);
106
107
        return $command->run($greetInput, $output);
108
    }
109
110
    /**
111
     * Override this method to run any custom scripts before Deploy->run
112
     *
113
     * @param InputInterface $input
114
     * @param OutputInterface $output
115
     */
116
    protected function runBefore(InputInterface $input, OutputInterface $output)
117
    {
118
        // Could have a backup database script here
119
    }
120
121
    /**
122
     * Override this method to run any custom scripts after deploy
123
     *
124
     * @param InputInterface $input
125
     * @param OutputInterface $output
126
     */
127
    protected function runAfter(InputInterface $input, OutputInterface $output)
128
    {
129
        // If you are using stockpile could have this as a confirmation Y/N
130
        // Elastic search
131
    }
132
}
133