Completed
Pull Request — master (#211)
by
unknown
03:31
created

Restore::printRestoreCommands()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19

Duplication

Lines 7
Ratio 36.84 %

Code Coverage

Tests 12
CRAP Score 4.0072

Importance

Changes 0
Metric Value
dl 7
loc 19
ccs 12
cts 13
cp 0.9231
rs 9.6333
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4.0072
1
<?php
2
namespace phpbu\App\Runner;
3
4
use phpbu\App\Backup\Crypter;
5
use phpbu\App\Backup\Decompressor;
6
use phpbu\App\Backup\Restore\Plan;
7
use phpbu\App\Backup\Source;
8
use phpbu\App\Backup\Target;
9
use phpbu\App\Configuration;
10
use phpbu\App\Result;
11
use SebastianFeldmann\Cli\Util;
12
13
/**
14
 * Restore Runner
15
 *
16
 * @package    phpbu
17
 * @author     Sebastian Feldmann <[email protected]>
18
 * @copyright  Sebastian Feldmann <[email protected]>
19
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
20
 * @link       https://phpbu.de/
21
 * @since      Class available since Release 6.0.0
22
 * @internal
23
 */
24
class Restore extends Process
25
{
26
    /**
27
     * Restore all backups
28
     *
29
     * @param  \phpbu\App\Configuration $configuration
30
     * @return \phpbu\App\Result
31
     * @throws \Exception
32
     */
33 3
    public function run(Configuration $configuration) : Result
34
    {
35 3
        foreach ($configuration->getBackups() as $backup) {
36 3
            if ($configuration->isBackupActive($backup->getName())) {
37 3
                $this->printPlan($this->createRestorePlan($backup));
38
            }
39
        }
40
41 3
        return $this->result;
42
    }
43
44
    /**
45
     * Collects all restore commands in a restore plan
46
     *
47
     * @param  \phpbu\App\Configuration\Backup $backup
48
     * @return \phpbu\App\Backup\Restore\Plan
49
     * @throws \phpbu\App\Exception
50
     */
51 3
    private function createRestorePlan(Configuration\Backup $backup): Plan
52
    {
53 3
        $plan   = new Plan();
54 3
        $target = $this->factory->createTarget($backup->getTarget());
55 3
        $this->decryptBackup($target, $backup, $plan);
56 3
        $this->restoreBackup($target, $backup, $plan);
57
58 3
        return $plan;
59
    }
60
61
    /**
62
     * Output the restore plan
63
     *
64
     * @param \phpbu\App\Backup\Restore\Plan $plan
65
     */
66 3
    private function printPlan(Plan $plan)
67
    {
68 3
        $this->printDecryptionCommands($plan);
69 3
        $this->printRestoreCommands($plan);
70 3
    }
71
72
    /**
73
     * @param  \phpbu\App\Backup\Target        $target
74
     * @param  \phpbu\App\Configuration\Backup $backup
75
     * @param  \phpbu\App\Backup\Restore\Plan  $plan
76
     * @throws \phpbu\App\Exception
77
     */
78 3
    private function decryptBackup(Target $target, Configuration\Backup $backup, Plan $plan)
79
    {
80 3
        if (!$backup->hasCrypt()) {
81 1
            return;
82
        }
83 2
        $cryptConf = $backup->getCrypt();
84 2
        $crypt     = $this->factory->createCrypter($cryptConf->type, $cryptConf->options);
85
86
        // check if decryption is supported
87 2
        if (!$crypt instanceof Crypter\Restorable) {
88 1
            $plan->markCryptAsUnsupported();
89 1
            return;
90
        }
91 1
        $crypt->restore($target, $plan);
92 1
    }
93
94
    /**
95
     * @param  \phpbu\App\Backup\Target        $target
96
     * @param  \phpbu\App\Configuration\Backup $backup
97
     * @param  \phpbu\App\Backup\Restore\Plan  $plan
98
     * @throws \phpbu\App\Exception
99
     */
100 3
    private function restoreBackup(Target $target, Configuration\Backup $backup, Plan $plan)
101
    {
102 3
        $sourceConf = $backup->getSource();
103 3
        $source     = $this->factory->createSource($sourceConf->type, $sourceConf->options);
104
        // make sure restore is supported
105 3
        if (!$source instanceof Source\Restorable) {
106 1
            $plan->markSourceAsUnsupported();
107 1
            return;
108
        }
109
        // pass plan to source to collect restore commands
110 2
        $status = $source->restore($target, $plan);
111
112
        // make sure we decompress the backup
113 2
        if ($target->shouldBeCompressed()) {
114
            $decompressor = new Decompressor\File();
115
            $command      = $decompressor->decompress($target);
116
            $plan->addDecompressionCommand($command);
117
            $target->disableCompression();
118
        }
119
120
        // if source created a directory we have to un-tar the decompressed backup
121 2
        if ($status->isDirectory()) {
122
            $decompressor = new Decompressor\Directory();
123
            $command      = $decompressor->decompress($target);
124
125
            $target->removeFileSuffix('tar');
126
            $plan->addDecompressionCommand($command);
127
        }
128 2
    }
129
130
    /**
131
     * Output the decryption commands
132
     *
133
     * @param \phpbu\App\Backup\Restore\Plan $plan
134
     * @return void
135
     */
136 3
    private function printDecryptionCommands(Plan $plan): void
137
    {
138 3
        if (!$plan->isCryptSupported()) {
139 1
            echo Util::formatWithColor('fg-red', "WARNING: Your configured crypt does not support restore for now.\n");
140 1
            return;
141
        }
142 2
        $commands = $plan->getDecryptionCommands();
143
144 2
        if (empty($commands)) {
145 1
            return;
146
        }
147
148 1
        echo Util::formatWithColor('fg-yellow', "# Decrypt your backup\n");
149 1 View Code Duplication
        foreach ($commands as $cmd) {
150 1
            if ($cmd['color'] == null) {
151 1
                echo $cmd['command'] . PHP_EOL;
152 1
                continue;
153
            }
154
            echo Util::formatWithColor($cmd['color'], $cmd['command']);
155
        }
156 1
        echo PHP_EOL;
157 1
    }
158
159
    /**
160
     * Output the restore commands
161
     *
162
     * @param \phpbu\App\Backup\Restore\Plan $plan
163
     * @return void
164
     */
165 3
    private function printRestoreCommands(Plan $plan): void
166
    {
167 3
        if (!$plan->isSourceSupported()) {
168 1
            echo Util::formatWithColor('fg-red', "WARNING: Your configured source does not support restore for now.\n");
169 1
            return;
170
        }
171
172 2
        $this->printExtractionCommands($plan->getDecompressionCommands());
173
174 2
        echo Util::formatWithColor('fg-yellow', "# Restore your data [BE CAREFUL]\n");
175 2 View Code Duplication
        foreach ($plan->getRestoreCommands() as $cmd) {
176 1
            if ($cmd['color'] == null) {
177 1
                echo $cmd['command'] . PHP_EOL;
178 1
                continue;
179
            }
180
            echo Util::formatWithColor($cmd['color'], $cmd['command']);
181
        }
182 2
        echo PHP_EOL;
183 2
    }
184
185
    /**
186
     * Output extraction commands
187
     *
188
     * @param  array $commands
189
     * @return void
190
     */
191 2
    private function printExtractionCommands(array $commands): void
192
    {
193 2
        if (!empty($commands)) {
194
            echo Util::formatWithColor('fg-yellow', "# Extract your backup \n");
195 View Code Duplication
            foreach ($commands as $cmd) {
196
                if ($cmd['color'] == null) {
197
                    echo $cmd['command'] . PHP_EOL;
198
                    continue;
199
                }
200
                echo Util::formatWithColor($cmd['color'], $cmd['command']);
201
            }
202
            echo PHP_EOL;
203
        }
204 2
    }
205
}
206