Passed
Push — master ( ae0161...6e4dff )
by Sebastian
01:09
created

Restore::printExtractionCommands()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.679

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 3
cts 7
cp 0.4286
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 4.679
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
            $plan->markSourceAsUnsupported();
107
            return;
108
        }
109
        // pass plan to source to collect restore commands
110 3
        $status = $source->restore($target, $plan);
111
112
        // make sure we decompress the backup
113 3
        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 3
        if ($status->isDirectory()) {
122
            $decompressor = new Decompressor\Directory();
123
            $command      = $decompressor->decompress($target);
124
125
            $target->removeFileSuffix('tar');
126
            $plan->addDecompressionCommand($command);
127
        }
128 3
    }
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
        foreach ($commands as $cmd) {
150 1
            echo $cmd . PHP_EOL;
151
        }
152 1
        echo PHP_EOL;
153 1
    }
154
155
    /**
156
     * Output the restore commands
157
     *
158
     * @param \phpbu\App\Backup\Restore\Plan $plan
159
     * @return void
160
     */
161 3
    private function printRestoreCommands(Plan $plan): void
162
    {
163 3
        if (!$plan->isSourceSupported()) {
164
            echo Util::formatWithColor('fg-red', "WARNING: Your configured source does not support restore for now.\n");
165
            return;
166
        }
167
168 3
        $this->printExtractionCommands($plan->getDecompressionCommands());
169
170 3
        echo Util::formatWithColor('fg-yellow', "# Restore your data [BE CAREFUL]\n");
171 3
        foreach ($plan->getRestoreCommands() as $cmd) {
172 1
            echo $cmd . PHP_EOL;
173
        }
174 3
        echo PHP_EOL;
175 3
    }
176
177
    /**
178
     * Output extraction commands
179
     *
180
     * @param  array $commands
181
     * @return void
182
     */
183 3
    private function printExtractionCommands(array $commands): void
184
    {
185 3
        if (!empty($commands)) {
186
            echo Util::formatWithColor('fg-yellow', "# Extract your backup \n");
187
            foreach ($commands as $cmd) {
188
                echo $cmd . PHP_EOL;
189
            }
190
            echo PHP_EOL;
191
        }
192 3
    }
193
}
194