Completed
Push — master ( c5e677...4df15a )
by Sebastian
02:56
created

Backup::executeChecks()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 13
cts 13
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 14
nc 8
nop 3
crap 4
1
<?php
2
namespace phpbu\App\Runner;
3
4
use phpbu\App\Backup\Compressor;
5
use phpbu\App\Exception;
6
use phpbu\App\Backup\Cleaner;
7
use phpbu\App\Backup\Collector;
8
use phpbu\App\Backup\Crypter;
9
use phpbu\App\Backup\Sync;
10
use phpbu\App\Backup\Target;
11
use phpbu\App\Configuration;
12
use phpbu\App\Result;
13
14
/**
15
 * Backup Runner
16
 *
17
 * @package    phpbu
18
 * @author     Sebastian Feldmann <[email protected]>
19
 * @copyright  Sebastian Feldmann <[email protected]>
20
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
21
 * @link       https://phpbu.de/
22
 * @since      Class available since Release 5.1.0
23
 */
24
class Backup extends Process
25
{
26
    use Compression;
27
28
    /**
29
     * Backup failed
30
     *
31
     * @var bool
32
     */
33
    protected $failure;
34
35
    /**
36
     * Execute backups.
37
     *
38
     * @param  \phpbu\App\Configuration $configuration
39
     * @return \phpbu\App\Result
40
     * @throws \phpbu\App\Exception
41
     */
42 8
    public function run(Configuration $configuration) : Result
43
    {
44 8
        $this->configuration = $configuration;
45 8
        $stop                = false;
46
47 8
        $this->result->phpbuStart($configuration);
48
49
        // create backups
50
        /** @var \phpbu\App\Configuration\Backup $backup */
51 8
        foreach ($configuration->getBackups() as $backup) {
52 8
            if ($stop) {
53 1
                break;
54
            }
55
            // make sure the backup should be executed and is not excluded via the --limit option
56 8 View Code Duplication
            if (!$configuration->isBackupActive($backup->getName())) {
57 1
                $this->result->debug('skipping backup: ' . $backup->getName() . PHP_EOL);
58 1
                continue;
59
            }
60
            // setup target and collector, reset failure state
61 8
            $target        = $this->factory->createTarget($backup->getTarget());
62 8
            $collector     = new Collector($target);
63 8
            $this->failure = false;
64
65
            try {
66
                /*      ___  ___  _______ ____  _____
67
                 *     / _ )/ _ |/ ___/ //_/ / / / _ \
68
                 *    / _  / __ / /__/ ,< / /_/ / ___/
69
                 *   /____/_/ |_\___/_/|_|\____/_/
70
                 */
71 8
                $this->executeSource($backup, $target);
72
73
                /*     _______ _____________ ______
74
                 *    / ___/ // / __/ ___/ //_/ __/
75
                 *   / /__/ _  / _// /__/ ,< _\ \
76
                 *   \___/_//_/___/\___/_/|_/___/
77
                 */
78 7
                $this->executeChecks($backup, $target, $collector);
79
80
                /*     __________  _____  ______
81
                 *    / ___/ _ \ \/ / _ \/_  __/
82
                 *   / /__/ , _/\  / ___/ / /
83
                 *   \___/_/|_| /_/_/    /_/
84
                 */
85 7
                $this->executeCrypt($backup, $target);
86
87
                /*      ______  ___  ___________
88
                 *     / __/\ \/ / |/ / ___/ __/
89
                 *    _\ \   \  /    / /___\ \
90
                 *   /___/   /_/_/|_/\___/___/
91
                 */
92 7
                $this->executeSyncs($backup, $target);
93
94
                /*     _______   _______   _  ____  _____
95
                 *    / ___/ /  / __/ _ | / |/ / / / / _ \
96
                 *   / /__/ /__/ _// __ |/    / /_/ / ___/
97
                 *   \___/____/___/_/ |_/_/|_/\____/_/
98
                 */
99 7
                $this->executeCleanup($backup, $target, $collector);
100
101 1
            } catch (\Exception $e) {
102 1
                $this->result->debug('exception: ' . $e->getMessage());
103 1
                $this->result->addError($e);
104 1
                $this->result->backupFailed($backup);
105 1
                if ($backup->stopOnFailure()) {
106 8
                    $stop = true;
107
                }
108
            }
109
        }
110 8
        $this->result->phpbuEnd();
111
112 8
        return $this->result;
113
    }
114
115
    /**
116
     * Execute the backup.
117
     *
118
     * @param  \phpbu\App\Configuration\Backup $conf
119
     * @param  \phpbu\App\Backup\Target        $target
120
     * @throws \Exception
121
     */
122 8 View Code Duplication
    protected function executeSource(Configuration\Backup $conf, Target $target)
123
    {
124 8
        $this->result->backupStart($conf);
125
        /* @var \phpbu\App\Runner\Source $runner */
126 8
        $source = $this->factory->createSource($conf->getSource()->type, $conf->getSource()->options);
127 8
        $status = $source->backup($target, $this->result);
128 7
        $this->compress($status, $target, $this->result);
129 7
        $this->result->backupEnd($conf);
130 7
    }
131
132
    /**
133
     * Execute checks.
134
     *
135
     * @param  \phpbu\App\Configuration\Backup $backup
136
     * @param  \phpbu\App\Backup\Target        $target
137
     * @param  \phpbu\App\Backup\Collector     $collector
138
     * @throws \Exception
139
     */
140 7
    protected function executeChecks(Configuration\Backup $backup, Target $target, Collector $collector)
141
    {
142 7
        foreach ($backup->getChecks() as $config) {
143
            try {
144 7
                $this->result->checkStart($config);
145 7
                $check = $this->factory->createCheck($config->type);
146 7
                if ($check->pass($target, $config->value, $collector, $this->result)) {
147 5
                    $this->result->checkEnd($config);
148
                } else {
149 1
                    $this->failure = true;
150 6
                    $this->result->checkFailed($config);
151
                }
152 1
            } catch (Exception $e) {
153 1
                $this->failure = true;
154 1
                $this->result->addError($e);
155 7
                $this->result->checkFailed($config);
156
            }
157
        }
158 7
    }
159
160
    /**
161
     * Execute encryption.
162
     *
163
     * @param  \phpbu\App\Configuration\Backup $backup
164
     * @param  \phpbu\App\Backup\Target        $target
165
     * @throws \phpbu\App\Exception
166
     */
167 7 View Code Duplication
    protected function executeCrypt(Configuration\Backup $backup, Target $target)
168
    {
169 7
        if ($backup->hasCrypt()) {
170 7
            $config = $backup->getCrypt();
171
            try {
172 7
                $this->result->cryptStart($config);
173 7
                if ($this->failure && $config->skipOnFailure) {
174 2
                    $this->result->cryptSkipped($config);
175 2
                    return;
176
                }
177 5
                $crypter = $this->factory->createCrypter($config->type, $config->options);
178 5
                $crypter->crypt($target, $this->result);
179 4
                $this->result->cryptEnd($config);
180
181 1
            } catch (Crypter\Exception $e) {
182 1
                $this->failure = true;
183 1
                $this->result->addError($e);
184 1
                $this->result->cryptFailed($config);
185
            }
186
        }
187 5
    }
188
189
    /**
190
     * Execute all syncs.
191
     *
192
     * @param  \phpbu\App\Configuration\Backup $backup
193
     * @param  \phpbu\App\Backup\Target        $target
194
     * @throws \Exception
195
     */
196 7 View Code Duplication
    protected function executeSyncs(Configuration\Backup $backup, Target $target)
197
    {
198
        /* @var \phpbu\App\Configuration\Backup\Sync $sync */
199 7
        foreach ($backup->getSyncs() as $config) {
200
            try {
201 7
                $this->result->syncStart($config);
202 7
                if ($this->failure && $config->skipOnFailure) {
203 3
                    $this->result->syncSkipped($config);
204 3
                    return;
205
                }
206 4
                $sync = $this->factory->createSync($config->type, $config->options);
207 4
                $sync->sync($target, $this->result);
208 3
                $this->result->syncEnd($config);
209
210 1
            } catch (Sync\Exception $e) {
211 1
                $this->failure = true;
212 1
                $this->result->addError($e);
213 4
                $this->result->syncFailed($config);
214
            }
215
        }
216 4
    }
217
218
    /**
219
     * Execute the cleanup.
220
     *
221
     * @param  \phpbu\App\Configuration\Backup $backup
222
     * @param  \phpbu\App\Backup\Target        $target
223
     * @param  \phpbu\App\Backup\Collector     $collector
224
     * @throws \phpbu\App\Exception
225
     */
226 7 View Code Duplication
    protected function executeCleanup(Configuration\Backup $backup, Target $target, Collector $collector)
227
    {
228
        /* @var \phpbu\App\Configuration\Backup\Cleanup $config */
229 7
        if ($backup->hasCleanup()) {
230 7
            $config = $backup->getCleanup();
231
            try {
232 7
                $this->result->cleanupStart($config);
233 7
                if ($this->failure && $config->skipOnFailure) {
234 4
                    $this->result->cleanupSkipped($config);
235 4
                    return;
236
                }
237 3
                $cleaner = $this->factory->createCleaner($config->type, $config->options);
238 3
                $cleaner->cleanup($target, $collector, $this->result);
239 2
                $this->result->cleanupEnd($config);
240
241 1
            } catch (Cleaner\Exception $e) {
242 1
                $this->failure = true;
243 1
                $this->result->addError($e);
244 1
                $this->result->cleanupFailed($config);
245
            }
246
        }
247 3
    }
248
249
    /**
250
     * Execute the compressor.
251
     * Returns the path to the created archive file.
252
     *
253
     * @param  \phpbu\App\Backup\Compressor\Executable $compressor
254
     * @param  \phpbu\App\Backup\Target                $target
255
     * @param  \phpbu\App\Result                       $result
256
     * @return string
257
     */
258
    protected function executeCompressor(Compressor\Executable $compressor, Target $target, Result $result) : string
259
    {
260
        return $compressor->compress($target, $result);
261
    }
262
}
263