Completed
Push — master ( 0c38e0...116503 )
by Sebastian
03:25
created

Runner::executeCrypt()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
rs 8.8571
cc 5
eloc 14
nc 7
nop 2
1
<?php
2
namespace phpbu\App;
3
4
use phpbu\App\Backup;
5
use phpbu\App\Backup\Collector;
6
use phpbu\App\Backup\Compressor;
7
use phpbu\App\Backup\Target;
8
9
/**
10
 * Runner actually executes all backup jobs.
11
 *
12
 * @package    phpbu
13
 * @subpackage App
14
 * @author     Sebastian Feldmann <[email protected]>
15
 * @copyright  Sebastian Feldmann <[email protected]>
16
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
17
 * @link       http://phpbu.de/
18
 * @since      Class available since Release 1.0.0
19
 */
20
class Runner
21
{
22
    /**
23
     * phpbu Factory
24
     *
25
     * @var \phpbu\App\Factory
26
     */
27
    protected $factory;
28
29
    /**
30
     * Application result
31
     *
32
     * @var \phpbu\App\Result
33
     */
34
    protected $result;
35
36
    /**
37
     * Backup failed
38
     *
39
     * @var boolean
40
     */
41
    protected $failure;
42
43
    /**
44
     * App Configuration
45
     *
46
     * @var \phpbu\App\Configuration
47
     */
48
    protected $configuration;
49
50
    /**
51
     * Constructor
52
     *
53
     * @param \phpbu\App\Factory $factory
54
     */
55
    public function __construct(Factory $factory)
56
    {
57
        $this->factory = $factory;
58
    }
59
60
    /**
61
     * Run phpbu
62
     *
63
     * @param  \phpbu\App\Configuration $configuration
64
     * @param  \phpbu\App\Factory
65
     * @return \phpbu\App\Result
66
     */
67
    public function run(Configuration $configuration)
68
    {
69
        $stop                = false;
70
        $this->result        = new Result();
71
        $this->configuration = $configuration;
72
73
        $this->setupEnvironment($configuration);
74
        $this->setupLoggers($configuration);
75
        $this->result->phpbuStart($configuration);
76
77
        // create backups
78
        /** @var \phpbu\App\Configuration\Backup $backup */
79
        foreach ($configuration->getBackups() as $backup) {
80
            if ($stop) {
81
                break;
82
            }
83
            // setup target and collector, reset failure state
84
            $target        = $this->createTarget($backup->getTarget());
85
            $collector     = new Collector($target);
86
            $this->failure = false;
87
88
            try {
89
                /*      ___  ___  _______ ____  _____
90
                 *     / _ )/ _ |/ ___/ //_/ / / / _ \
91
                 *    / _  / __ / /__/ ,< / /_/ / ___/
92
                 *   /____/_/ |_\___/_/|_|\____/_/
93
                 */
94
                $this->executeSource($backup, $target);
95
96
                /*     _______ _____________ ______
97
                 *    / ___/ // / __/ ___/ //_/ __/
98
                 *   / /__/ _  / _// /__/ ,< _\ \
99
                 *   \___/_//_/___/\___/_/|_/___/
100
                 */
101
                $this->executeChecks($backup, $target, $collector);
102
103
                /*     __________  _____  ______
104
                 *    / ___/ _ \ \/ / _ \/_  __/
105
                 *   / /__/ , _/\  / ___/ / /
106
                 *   \___/_/|_| /_/_/    /_/
107
                 */
108
                $this->executeCrypt($backup, $target);
109
110
                /*      ______  ___  ___________
111
                 *     / __/\ \/ / |/ / ___/ __/
112
                 *    _\ \   \  /    / /___\ \
113
                 *   /___/   /_/_/|_/\___/___/
114
                 */
115
                $this->executeSyncs($backup, $target);
116
117
                /*     _______   _______   _  ____  _____
118
                 *    / ___/ /  / __/ _ | / |/ / / / / _ \
119
                 *   / /__/ /__/ _// __ |/    / /_/ / ___/
120
                 *   \___/____/___/_/ |_/_/|_/\____/_/
121
                 */
122
                $this->executeCleanup($backup, $target, $collector);
123
124
            } catch (\Exception $e) {
125
                $this->result->debug('exception: ' . $e->getMessage());
126
                $this->result->addError($e);
127
                $this->result->backupFailed($backup);
128
                if ($backup->stopOnFailure()) {
129
                    $stop = true;
130
                }
131
            }
132
        }
133
        $this->result->phpbuEnd();
134
135
        return $this->result;
136
    }
137
138
    /**
139
     * This executes a bootstrap runner to handle ini settings and the bootstrap file inclusion.
140
     *
141
     * @param  \phpbu\App\Configuration $configuration
142
     * @throws \phpbu\App\Exception
143
     */
144
    protected function setupEnvironment(Configuration $configuration)
145
    {
146
        $runner = $this->factory->createRunner('Bootstrap', $this->configuration->isSimulation());
147
        $runner->run($configuration);
148
    }
149
150
    /**
151
     * Create and register all configured loggers.
152
     *
153
     * @param  \phpbu\App\Configuration $configuration
154
     */
155
    protected function setupLoggers(Configuration $configuration)
156
    {
157
        foreach ($configuration->getLoggers() as $log) {
158
            // this is a already fully setup Listener so just add it
159
            if ($log instanceof Listener) {
160
                $logger = $log;
161
            } else {
162
                // this is a configuration blueprint for a logger, so create and add it
163
                /** @var \phpbu\App\Configuration\Logger $log */
164
                /** @var \phpbu\App\Listener $logger */
165
                $logger = $this->factory->createLogger($log->type, $log->options);
166
            }
167
            $this->result->addListener($logger);
168
        }
169
    }
170
171
    /**
172
     * Create a target.
173
     *
174
     * @param  \phpbu\App\Configuration\Backup\Target $conf
175
     * @return \phpbu\App\Backup\Target
176
     * @throws \phpbu\App\Exception
177
     */
178
    protected function createTarget(Configuration\Backup\Target $conf)
179
    {
180
        $target = new Target($conf->dirname, $conf->filename);
181
        $target->setupPath();
182
        // add possible compressor
183
        if (!empty($conf->compression)) {
184
            $compression = Target\Compression\Factory::create($conf->compression);
185
            $target->setCompression($compression);
186
        }
187
        return $target;
188
    }
189
190
    /**
191
     * Execute the backup.
192
     *
193
     * @param  \phpbu\App\Configuration\Backup $conf
194
     * @param  \phpbu\App\Backup\Target        $target
195
     * @throws \Exception
196
     */
197
    protected function executeSource(Configuration\Backup $conf, Target $target)
198
    {
199
        $this->result->backupStart($conf);
200
        /* @var \phpbu\App\Runner\Source $runner */
201
        $source = $this->factory->createSource($conf->getSource()->type, $conf->getSource()->options);
202
        $runner = $this->factory->createRunner('source', $this->configuration->isSimulation());
203
        $runner->run($source, $target, $this->result);
204
        $this->result->backupEnd($conf);
205
    }
206
207
    /**
208
     * Execute checks.
209
     *
210
     * @param  \phpbu\App\Configuration\Backup $backup
211
     * @param  \phpbu\App\Backup\Target        $target
212
     * @param  \phpbu\App\Backup\Collector     $collector
213
     * @throws \Exception
214
     */
215
    protected function executeChecks(Configuration\Backup $backup, Target $target, Collector $collector)
216
    {
217
        /* @var \phpbu\App\Runner\Check $runner */
218
        /* @var \phpbu\App\Configuration\Backup\Check $check */
219
        $runner = $this->factory->createRunner('check', $this->configuration->isSimulation());
220
        foreach ($backup->getChecks() as $config) {
221
            try {
222
                $this->result->checkStart($config);
223
                $check = $this->factory->createCheck($config->type);
224
                if ($runner->run($check, $target, $config->value, $collector, $this->result)) {
225
                    $this->result->checkEnd($config);
226
                } else {
227
                    $this->failure = true;
228
                    $this->result->checkFailed($config);
229
                }
230
            } catch (Exception $e) {
231
                $this->failure = true;
232
                $this->result->addError($e);
233
                $this->result->checkFailed($config);
234
            }
235
        }
236
    }
237
238
    /**
239
     * Execute encryption.
240
     *
241
     * @param \phpbu\App\Configuration\Backup $backup
242
     * @param \phpbu\App\Backup\Target        $target
243
     */
244
    protected function executeCrypt(Configuration\Backup $backup, Target $target)
245
    {
246
        if ($backup->hasCrypt()) {
247
            $crypt = $backup->getCrypt();
248
            try {
249
                $this->result->cryptStart($crypt);
250
                if ($this->failure && $crypt->skipOnFailure) {
251
                    $this->result->cryptSkipped($crypt);
252
                } else {
253
                    /* @var \phpbu\App\Runner\Crypter $runner */
254
                    $runner  = $this->factory->createRunner('crypter', $this->configuration->isSimulation());
255
                    $runner->run($this->factory->createCrypter($crypt->type, $crypt->options), $target, $this->result);
256
                }
257
            } catch (Backup\Crypter\Exception $e) {
258
                $this->failure = true;
259
                $this->result->addError($e);
260
                $this->result->cryptFailed($crypt);
261
            }
262
        }
263
    }
264
265
    /**
266
     * Execute all syncs.
267
     *
268
     * @param  \phpbu\App\Configuration\Backup $backup
269
     * @param  \phpbu\App\Backup\Target        $target
270
     * @throws \Exception
271
     */
272
    protected function executeSyncs(Configuration\Backup $backup, Target $target)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
273
    {
274
        /* @var \phpbu\App\Runner\Crypter $runner */
275
        /* @var \phpbu\App\Configuration\Backup\Sync $sync */
276
        $runner  = $this->factory->createRunner('sync', $this->configuration->isSimulation());
277
        foreach ($backup->getSyncs() as $sync) {
278
            try {
279
                $this->result->syncStart($sync);
280
                if ($this->failure && $sync->skipOnFailure) {
281
                    $this->result->syncSkipped($sync);
282
                } else {
283
                    $runner->run($this->factory->createSync($sync->type, $sync->options), $target, $this->result);
284
                    $this->result->syncEnd($sync);
285
                }
286
            } catch (Backup\Sync\Exception $e) {
287
                $this->failure = true;
288
                $this->result->addError($e);
289
                $this->result->syncFailed($sync);
290
            }
291
        }
292
    }
293
294
    /**
295
     * Execute the cleanup.
296
     *
297
     * @param  \phpbu\App\Configuration\Backup $backup
298
     * @param  \phpbu\App\Backup\Target        $target
299
     * @param  \phpbu\App\Backup\Collector     $collector
300
     * @throws \Exception
301
     */
302
    protected function executeCleanup(Configuration\Backup $backup, Target $target, Collector $collector)
303
    {
304
        /* @var \phpbu\App\Runner\Cleaner $runner */
305
        /* @var \phpbu\App\Configuration\Backup\Cleanup $cleanup */
306
        if ($backup->hasCleanup()) {
307
            $cleanup = $backup->getCleanup();
308
            try {
309
                $runner = $this->factory->createRunner('cleaner', $this->configuration->isSimulation());
310
                $this->result->cleanupStart($cleanup);
311
                if ($this->failure && $cleanup->skipOnFailure) {
312
                    $this->result->cleanupSkipped($cleanup);
313
                } else {
314
                    $cleaner = $this->factory->createCleaner($cleanup->type, $cleanup->options);
315
                    $runner->run($cleaner, $target, $collector, $this->result);
316
                    $this->result->cleanupEnd($cleanup);
317
                }
318
            } catch (Backup\Cleaner\Exception $e) {
319
                $this->failure = true;
320
                $this->result->addError($e);
321
                $this->result->cleanupFailed($cleanup);
322
            }
323
        }
324
    }
325
}
326