Completed
Push — master ( 90683e...d7a5bd )
by Sebastian
10:48
created

Runner::createTarget()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 11
rs 9.4286
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
namespace phpbu\App;
3
4
use phpbu\App\Backup;
5
use phpbu\App\Backup\Compressor;
6
use phpbu\App\Backup\Collector;
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
     * Constructor
45
     *
46
     * @param \phpbu\App\Factory $factory
47
     */
48
    public function __construct(Factory $factory)
49
    {
50
        $this->factory = $factory;
51
    }
52
53
    /**
54
     * Run phpbu
55
     *
56
     * @param  \phpbu\App\Configuration $configuration
57
     * @param  \phpbu\App\Factory
58
     * @return \phpbu\App\Result
59
     */
60
    public function run(Configuration $configuration)
61
    {
62
        // TODO: don't rely on static/global settings this is ugly
63
        Util\Cli::registerBase('configuration', $configuration->getWorkingDirectory());
64
65
        $stop         = false;
66
        $this->result = new Result();
67
        $this->result->phpbuStart($configuration);
68
69
        $this->setupEnvironment($configuration);
70
        $this->setupLoggers($configuration);
71
72
        // create backups
73
        /** @var \phpbu\App\Configuration\Backup $backup */
74
        foreach ($configuration->getBackups() as $backup) {
75
            if ($stop) {
76
                break;
77
            }
78
            // setup target and collector, reset failure state
79
            $target        = $this->createTarget($backup->getTarget());
80
            $collector     = new Collector($target);
81
            $this->failure = false;
82
83
            try {
84
                /*      ___  ___  _______ ____  _____
85
                 *     / _ )/ _ |/ ___/ //_/ / / / _ \
86
                 *    / _  / __ / /__/ ,< / /_/ / ___/
87
                 *   /____/_/ |_\___/_/|_|\____/_/
88
                 */
89
                $this->executeSource($backup, $target);
90
91
                /*     _______ _____________ ______
92
                 *    / ___/ // / __/ ___/ //_/ __/
93
                 *   / /__/ _  / _// /__/ ,< _\ \
94
                 *   \___/_//_/___/\___/_/|_/___/
95
                 */
96
                $this->executeChecks($backup, $target, $collector);
97
98
                /*     __________  _____  ______
99
                 *    / ___/ _ \ \/ / _ \/_  __/
100
                 *   / /__/ , _/\  / ___/ / /
101
                 *   \___/_/|_| /_/_/    /_/
102
                 */
103
                $this->executeCrypt($backup, $target);
104
105
                /*      ______  ___  ___________
106
                 *     / __/\ \/ / |/ / ___/ __/
107
                 *    _\ \   \  /    / /___\ \
108
                 *   /___/   /_/_/|_/\___/___/
109
                 */
110
                $this->executeSyncs($backup, $target);
111
112
                /*     _______   _______   _  ____  _____
113
                 *    / ___/ /  / __/ _ | / |/ / / / / _ \
114
                 *   / /__/ /__/ _// __ |/    / /_/ / ___/
115
                 *   \___/____/___/_/ |_/_/|_/\____/_/
116
                 */
117
                $this->executeCleanup($backup, $target, $collector);
118
119
            } catch (\Exception $e) {
120
                $this->result->debug('exception: ' . $e->getMessage());
121
                $this->result->addError($e);
122
                $this->result->backupFailed($backup);
123
                if ($backup->stopOnFailure()) {
124
                    $stop = true;
125
                }
126
            }
127
        }
128
        $this->result->phpbuEnd();
129
130
        return $this->result;
131
    }
132
133
    /**
134
     * This executes a bootstrap runner to handle ini settings and the bootstrap file inclusion.
135
     *
136
     * @param  \phpbu\App\Configuration $configuration
137
     * @throws \phpbu\App\Exception
138
     */
139
    protected function setupEnvironment(Configuration $configuration)
140
    {
141
        $runner = $this->factory->createRunner('Bootstrap');
142
        $runner->run($configuration);
143
    }
144
145
    /**
146
     * Create and register all configured loggers.
147
     *
148
     * @param  \phpbu\App\Configuration $configuration
149
     */
150
    protected function setupLoggers(Configuration $configuration)
151
    {
152
        foreach ($configuration->getLoggers() as $log) {
153
            // this is a already fully setup Listener so just add it
154
            if (is_a($log, '\\phpbu\\App\\Listener')) {
155
                $logger = $log;
156
            } else {
157
                // this is a configuration blueprint for a logger, so create and add it
158
                /** @var \phpbu\App\Configuration\Logger $log */
159
                /** @var \phpbu\App\Listener $logger */
160
                $logger = $this->factory->createLogger($log->type, $log->options);
161
            }
162
            $this->result->addListener($logger);
163
        }
164
    }
165
166
    /**
167
     * Create a target.
168
     *
169
     * @param  \phpbu\App\Configuration\Backup\Target $conf
170
     * @return \phpbu\App\Backup\Target
171
     * @throws \phpbu\App\Exception
172
     */
173
    protected function createTarget(Configuration\Backup\Target $conf)
174
    {
175
        $target = new Target($conf->dirname, $conf->filename);
176
        $target->setupPath();
177
        // add possible compressor
178
        if (!empty($conf->compression)) {
179
            $compressor = Compressor::create($conf->compression);
180
            $target->setCompressor($compressor);
181
        }
182
        return $target;
183
    }
184
185
    /**
186
     * Execute the backup.
187
     *
188
     * @param  \phpbu\App\Configuration\Backup $conf
189
     * @param  \phpbu\App\Backup\Target        $target
190
     * @throws \Exception
191
     */
192
    protected function executeSource(Configuration\Backup $conf, Target $target)
193
    {
194
        $this->result->backupStart($conf);
195
        $source = $this->factory->createSource($conf->getSource()->type, $conf->getSource()->options);
196
        $runner = $this->factory->createRunner('source');
197
        $runner->run($source, $target, $this->result);
198
        $this->result->backupEnd($conf);
199
    }
200
201
    /**
202
     * Execute checks.
203
     *
204
     * @param  \phpbu\App\Configuration\Backup $backup
205
     * @param  \phpbu\App\Backup\Target        $target
206
     * @param  \phpbu\App\Backup\Collector     $collector
207
     * @throws \Exception
208
     */
209
    protected function executeChecks(Configuration\Backup $backup, Target $target, Collector $collector)
210
    {
211
        $runner = $this->factory->createRunner('check');
212
        /** @var \phpbu\App\Configuration\Backup\Check $check */
213
        foreach ($backup->getChecks() as $config) {
214
            $check = $this->factory->createCheck($config->type);
215
            $runner->run($check, $config, $target, $collector, $this->result);
216
        }
217
        $this->failure = $runner->hasFailed();
218
    }
219
220
    /**
221
     * Execute encryption.
222
     *
223
     * @param \phpbu\App\Configuration\Backup $backup
224
     * @param \phpbu\App\Backup\Target        $target
225
     */
226 View Code Duplication
    protected function executeCrypt(Configuration\Backup $backup, Target $target)
227
    {
228
        $crypt = $backup->getCrypt();
229
        if (!empty($crypt)) {
230
            try {
231
                $this->result->cryptStart($crypt);
232
                if ($this->failure && $crypt->skipOnFailure) {
233
                    $this->result->cryptSkipped($crypt);
234
                } else {
235
                    $c = $this->factory->createCrypter($crypt->type, $crypt->options);
236
                    $c->crypt($target, $this->result);
237
                    $target->setCrypter($c);
238
                }
239
            } catch (Backup\Crypter\Exception $e) {
240
                $this->failure = true;
241
                $this->result->addError($e);
242
                $this->result->cryptFailed($crypt);
243
            }
244
        }
245
    }
246
247
    /**
248
     * Execute the syncs.
249
     *
250
     * @param  \phpbu\App\Configuration\Backup $backup
251
     * @param  \phpbu\App\Backup\Target        $target
252
     * @throws \Exception
253
     */
254
    protected function executeSyncs(Configuration\Backup $backup, Target $target)
255
    {
256
        /** @var \phpbu\App\Configuration\Backup\Sync $sync */
257
        foreach ($backup->getSyncs() as $sync) {
258
            try {
259
                $this->result->syncStart($sync);
260
                if ($this->failure && $sync->skipOnFailure) {
261
                    $this->result->syncSkipped($sync);
262
                } else {
263
                    $s = $this->factory->createSync($sync->type, $sync->options);
264
                    $s->sync($target, $this->result);
265
                    $this->result->syncEnd($sync);
266
                }
267
            } catch (Backup\Sync\Exception $e) {
268
                $this->failure = true;
269
                $this->result->addError($e);
270
                $this->result->syncFailed($sync);
271
            }
272
        }
273
    }
274
275
    /**
276
     * Execute the cleanup.
277
     *
278
     * @param  \phpbu\App\Configuration\Backup $backup
279
     * @param  \phpbu\App\Backup\Target        $target
280
     * @param  \phpbu\App\Backup\Collector     $collector
281
     * @throws \Exception
282
     */
283 View Code Duplication
    protected function executeCleanup(Configuration\Backup $backup, Target $target, Collector $collector)
284
    {
285
        $cleanup = $backup->getCleanup();
286
        if (!empty($cleanup)) {
287
            try {
288
                $this->result->cleanupStart($cleanup);
289
                if ($this->failure && $cleanup->skipOnFailure) {
290
                    $this->result->cleanupSkipped($cleanup);
291
                } else {
292
                    $cleaner = $this->factory->createCleaner($cleanup->type, $cleanup->options);
293
                    $cleaner->cleanup($target, $collector, $this->result);
294
                    $this->result->cleanupEnd($cleanup);
295
                }
296
            } catch (Backup\Cleaner\Exception $e) {
297
                $this->failure = true;
298
                $this->result->addError($e);
299
                $this->result->cleanupFailed($cleanup);
300
            }
301
        }
302
    }
303
}
304