Passed
Push — master ( ce1e4f...1e5872 )
by Josh
04:31
created

Deploy::runRequireUpdateMODXPackages()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 14
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 23
rs 9.2222
1
<?php
2
3
namespace LCI\MODX\Orchestrator\Deploy;
4
5
use LCI\Blend\Exception\TransportException;
6
use LCI\Blend\Exception\TransportNotFoundException;
7
use LCI\Blend\Transport\MODXPackages;
8
use LCI\Blend\Transport\MODXPackagesConfig;
9
use LCI\MODX\Console\Console;
10
use LCI\MODX\Console\Helpers\ConsoleUserInteractionHandler;
11
use LCI\MODX\Console\Helpers\UserInteractionHandler;
12
use LCI\MODX\Orchestrator\Orchestrator;
13
use modX;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\ArrayInput;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Question\ConfirmationQuestion;
19
20
class Deploy implements DeployInterface
21
{
22
    /** @var Command */
23
    protected $command;
24
25
    /** @var modX  */
26
    protected $modx;
27
28
    /**
29
     * Deploy constructor.
30
     * @param Command $application
31
     * @param modX $modx
32
     */
33
    public function __construct(Command $application, modX $modx)
34
    {
35
        $this->command = $application;
36
        $this->modx = $modx;
37
    }
38
39
    /**
40
     * @param OutputInterface $output
41
     */
42
    public function describe(OutputInterface $output)
43
    {
44
        $output->writeln('1. You will be prompted with a question if you would like to clear the MODX cache before running migrations');
45
        $output->writeln('2. Require/update any MODX Extras as defined in the core/config/lci_modx_transport_package.php');
46
        $output->writeln('3. Run all Blend migrations that are ready for all orchestrator packages');
47
        $output->writeln('4. Run all Blend migrations that are ready for your local project');
48
        $output->writeln('5. You will be prompted with a question if you would like to clear the MODX cache after the migrations have been completed.');
49
    }
50
51
    /**
52
     * @param InputInterface $input
53
     * @param OutputInterface $output
54
     * @throws \LCI\Blend\Exception\MigratorException
55
     */
56
    public function run(InputInterface $input, OutputInterface $output)
57
    {
58
        $this->runBefore($input, $output);
59
60
        // clear all cache Y/N?
61
        $helper = $this->command->getHelper('question');
62
        $question = new ConfirmationQuestion('Clear MODX cache before running migrations?', true);
63
64
        if ($helper->ask($input, $output, $question)) {
65
            $output->writeln($this->clearMODXCache($input, $output));
66
        }
67
68
        $output->writeln(PHP_EOL.'### Orchestrator require/update MODX Extras as defined in: core/config/lci_modx_transport_package.php');
69
        $this->runRequireUpdateMODXPackages($input, $output);
70
71
        // run all package migrations
72
        $output->writeln(PHP_EOL.'### Orchestrator package Blend migrations');
73
        Orchestrator::updateAllOrchestratorComposerPackages();
74
75
        $output->writeln(PHP_EOL.'### Local Blend migrations');
76
        // run local blend migrations
77
        $this->runLocalBlendMigration($input, $output);
78
79
        $helper = $this->command->getHelper('question');
80
        $question = new ConfirmationQuestion('Migrations have been ran, clear MODX cache again?', true);
81
82
        if ($helper->ask($input, $output, $question)) {
83
            $output->writeln($this->clearMODXCache($input, $output));
84
        }
85
86
        $this->runAfter($input, $output);
87
    }
88
89
    /**
90
     * @param InputInterface $input
91
     * @param OutputInterface $output
92
     * @return int
93
     * @throws \Exception
94
     */
95
    protected function clearMODXCache(InputInterface $input, OutputInterface $output)
96
    {
97
        $command = $this->command->getApplication()->find('console:clear');
98
99
        $arguments = [
100
            'command' => 'console:clear'
101
        ];
102
103
        /** @var  $greetInput */
104
        $greetInput = new ArrayInput($arguments);
105
106
        return $command->run($greetInput, $output);
107
    }
108
109
    /**
110
     * @param InputInterface $input
111
     * @param OutputInterface $output
112
     * @return int
113
     * @throws \Exception
114
     */
115
    protected function runLocalBlendMigration(InputInterface $input, OutputInterface $output)
116
    {
117
        $command = $this->command->getApplication()->find('blend:migrate');
118
119
        $arguments = [
120
            'command' => 'blend:migrate'
121
        ];
122
123
        /** @var  $greetInput */
124
        $greetInput = new ArrayInput($arguments);
125
126
        return $command->run($greetInput, $output);
127
    }
128
129
    /**
130
     * @param InputInterface $input
131
     * @param OutputInterface $output
132
     */
133
    protected function runRequireUpdateMODXPackages(InputInterface $input, OutputInterface $output)
134
    {
135
        $packages = MODXPackagesConfig::getPackages();
136
137
        $interactionHandler = '';
138
        if (isset($this->command->consoleUserInteractionHandler) && $this->command->consoleUserInteractionHandler instanceof UserInteractionHandler) {
139
            /** @var ConsoleUserInteractionHandler $interactionHandler */
140
            $interactionHandler = $this->command->consoleUserInteractionHandler;
141
        } else {
142
            $interactionHandler = new ConsoleUserInteractionHandler($input, $output);
143
        }
144
145
        $modxPackages = new MODXPackages($this->modx, $interactionHandler);
146
147
        foreach ($packages as $name => $info) {
148
            try {
149
                $modxPackages->requirePackage($info['signature'], $info['latest'], $info['provider']);
150
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
151
            }  catch (TransportNotFoundException $exception) {
152
                $output->writeln('Error: '.$exception->getMessage());
153
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
154
            } catch (TransportException $exception) {
155
                $output->writeln('Error: '.$exception->getMessage());
156
            }
157
        }
158
    }
159
160
    /**
161
     * Override this method to run any custom scripts before Deploy->run
162
     *
163
     * @param InputInterface $input
164
     * @param OutputInterface $output
165
     */
166
    protected function runBefore(InputInterface $input, OutputInterface $output)
167
    {
168
        // Could have a backup database script here
169
    }
170
171
    /**
172
     * Override this method to run any custom scripts after deploy
173
     *
174
     * @param InputInterface $input
175
     * @param OutputInterface $output
176
     */
177
    protected function runAfter(InputInterface $input, OutputInterface $output)
178
    {
179
        // If you are using stockpile could have this as a confirmation Y/N
180
        // Elastic search
181
    }
182
}
183