Completed
Push — master ( 8eb905...4f1d49 )
by Sebastian
03:16
created

Target::getCrypter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
namespace phpbu\App\Backup;
3
4
use phpbu\App\Exception;
5
use phpbu\App\Util\Str;
6
7
/**
8
 * Backup Target class.
9
 *
10
 * @package    phpbu
11
 * @subpackage Backup
12
 * @author     Sebastian Feldmann <[email protected]>
13
 * @copyright  Sebastian Feldmann <[email protected]>
14
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
15
 * @link       http://phpbu.de/
16
 * @since      Class available since Release 1.0.0
17
 */
18
class Target
19
{
20
    /**
21
     * Absolute path to the directory where to store the backup.
22
     *
23
     * @var string
24
     */
25
    private $path;
26
27
    /**
28
     * Path to the backup with potential date placeholders like %d.
29
     *
30
     * @var string
31
     */
32
    private $pathRaw;
33
34
    /**
35
     * Indicates if the path changes over time.
36
     *
37
     * @var boolean
38
     */
39
    private $pathIsChanging = false;
40
41
    /**
42
     * Part of the path without placeholders
43
     *
44
     * @var string
45
     */
46
    private $pathNotChanging;
47
48
    /**
49
     * List of directories containing date placeholders
50
     *
51
     * @var array
52
     */
53
    private $pathElementsChanging = [];
54
55
    /**
56
     * Backup filename.
57
     *
58
     * @var string
59
     */
60
    private $filename;
61
62
    /**
63
     * Filename with potential date placeholders like %d.
64
     *
65
     * @var string
66
     */
67
    private $filenameRaw;
68
69
    /**
70
     * Indicates if the filename changes over time.
71
     *
72
     * @var boolean
73
     */
74
    private $filenameIsChanging = false;
75
76
    /**
77
     * Target MIME type
78
     *
79
     * @var string
80
     */
81
    private $mimeType = 'text/plain';
82
83
    /**
84
     * Size in bytes
85
     *
86
     * @var integer
87
     */
88
    private $size;
89
90
    /**
91
     * Should the file be compressed.
92
     *
93
     * @var boolean
94
     */
95
    private $compress = false;
96
97
    /**
98
     * File compression.
99
     *
100
     * @var \phpbu\App\Backup\Compressor
101
     */
102
    private $compressor;
103
104
    /**
105
     * Should the file be encrypted.
106
     *
107
     * @var boolean
108
     */
109
    private $crypt = false;
110
111
    /**
112
     * File crypter.
113
     *
114
     * @var \phpbu\App\Backup\Crypter
115
     */
116
    private $crypter;
117
118
    /**
119
     * Constructor.
120
     *
121
     * @param  string  $path
122
     * @param  string  $filename
123
     * @param  integer $time
124
     * @throws \phpbu\App\Exception
125
     */
126 34
    public function __construct($path, $filename, $time = null)
127
    {
128 34
        $this->setPath($path, $time);
129 34
        $this->setFile($filename, $time);
130 34
    }
131
132
    /**
133
     * Directory setter.
134
     *
135
     * @param  string  $path
136
     * @param  integer $time
137
     * @throws \phpbu\App\Exception
138
     */
139 34
    public function setPath($path, $time = null)
140
    {
141 34
        $this->pathRaw = $path;
142 34
        if (false !== strpos($path, '%')) {
143 10
            $this->pathIsChanging = true;
144
            // path should be absolute so we remove the root slash
145 10
            $dirs = explode('/', substr($this->pathRaw, 1));
146
147 10
            $this->pathNotChanging = '';
148 10
            $foundChangingElement  = false;
149 10
            foreach ($dirs as $d) {
150 10
                if ($foundChangingElement || false !== strpos($d, '%')) {
151 10
                    $this->pathElementsChanging[] = $d;
152 10
                    $foundChangingElement         = true;
153 10
                } else {
154 10
                    $this->pathNotChanging .= DIRECTORY_SEPARATOR . $d;
155
                }
156 10
            }
157
            // replace potential date placeholder
158 10
            $path = Str::replaceDatePlaceholders($path, $time);
159 10
        } else {
160 24
            $this->pathNotChanging = $path;
161
        }
162 34
        $this->path = rtrim($path, DIRECTORY_SEPARATOR);
163 34
    }
164
165
    /**
166
     * Filename setter.
167
     *
168
     * @param string  $file
169
     * @param integer $time
170
     */
171 34
    public function setFile($file, $time = null)
172
    {
173 34
        $this->filenameRaw = $file;
174 34
        if (false !== strpos($file, '%')) {
175 20
            $this->filenameIsChanging = true;
176 20
            $file                     = Str::replaceDatePlaceholders($file, $time);
177 20
        }
178 34
        $this->filename = $file;
179 34
    }
180
181
    /**
182
     * Checks if the backup target directory is writable.
183
     * Creates the Directory if it doesn't exist.
184
     *
185
     * @throws \phpbu\App\Exception
186
     */
187 4
    public function setupPath()
188
    {
189
        // if directory doesn't exist, create it
190 4
        if (!is_dir($this->path)) {
191 3
            $reporting = error_reporting();
192 3
            error_reporting(0);
193 3
            $created = mkdir($this->path, 0755, true);
194 3
            error_reporting($reporting);
195 3
            if (!$created) {
196 1
                throw new Exception(sprintf('cant\'t create directory: %s', $this->path));
197
            }
198 2
        }
199 3
        if (!is_writable($this->path)) {
200 1
            throw new Exception(sprintf('no write permission for directory: %s', $this->path));
201
        }
202 2
    }
203
204
    /**
205
     * Target file MIME type setter.
206
     *
207
     * @param string $mime
208
     */
209 1
    public function setMimeType($mime)
210
    {
211 1
        $this->mimeType = $mime;
212 1
    }
213
214
    /**
215
     * Return the path to the backup file.
216
     *
217
     * @return string
218
     */
219 5
    public function getPath()
220
    {
221 5
        return $this->path;
222
    }
223
224
    /**
225
     * Return the path to the backup file.
226
     *
227
     * @return string
228
     */
229 1
    public function getPathRaw()
230
    {
231 1
        return $this->pathRaw;
232
    }
233
234
    /**
235
     * Return the name to the backup file.
236
     *
237
     * @param  boolean $plain
238
     * @return string
239
     */
240 18
    public function getFilename($plain = false)
241
    {
242 18
        $suffix = '';
243 18
        if (!$plain) {
244 16
            $suffix .= $this->shouldBeCompressed() ? '.' . $this->compressor->getSuffix() : '';
245 16
            $suffix .= $this->shouldBeEncrypted() ? '.' . $this->crypter->getSuffix() : '';
246 16
        }
247 18
        return $this->filename . $suffix;
248
    }
249
250
    /**
251
     * Return the name of the backup file without compressor or encryption suffix.
252
     *
253
     * @return string
254
     */
255 1
    public function getFilenamePlain()
256
    {
257 1
        return $this->getFilename(true);
258
    }
259
260
    /**
261
     * Return the raw name of the backup file incl. date placeholder.
262
     *
263
     * @return string
264
     */
265 7
    public function getFilenameRaw()
266
    {
267 7
        return $this->filenameRaw;
268
    }
269
270
    /**
271
     * Return file MIME type.
272
     *
273
     * @return string
274
     */
275 3
    public function getMimeType()
276
    {
277 3
        $mimeType = $this->mimeType;
278 3
        if ($this->shouldBeCompressed()) {
279 1
            $mimeType = $this->compressor->getMimeType();
280 1
        }
281 3
        return $mimeType;
282
    }
283
284
    /**
285
     * Return the actual filesize in bytes.
286
     *
287
     * @throws Exception
288
     * @return integer
289
     */
290 2
    public function getSize()
291
    {
292 2
        if (null === $this->size) {
293 2
            if (!file_exists($this)) {
294 1
                throw new Exception(sprintf('target file \'%s\' doesn\'t exist', $this->getFilename()));
295
            }
296 1
            $this->size = filesize($this);
297 1
        }
298 1
        return $this->size;
299
    }
300
301
    /**
302
     * Target file exists already.
303
     *
304
     * @param  boolean $plain
305
     * @return boolean
306
     */
307 4
    public function fileExists($plain = false)
308
    {
309 4
        return file_exists($this->getPathname($plain));
310
    }
311
312
    /**
313
     * Return as backup file object.
314
     *
315
     * @return \phpbu\App\Backup\File
316
     */
317
    public function toFile()
318 3
    {
319
        return new File(new \SplFileInfo($this->getPathname()));
320 3
    }
321 1
322
    /**
323 2
     * Deletes the target file.
324 1
     *
325
     * @param  boolean $plain
326 1
     * @throws \phpbu\App\Exception
327 1
     */
328
    public function unlink($plain = false)
329
    {
330
        if (!$this->fileExists($plain)) {
331
            throw new Exception(sprintf('target file \'%s\' doesn\'t exist', $this->getFilename($plain)));
332
        }
333
        if (!is_writable($this->getPathname($plain))) {
334
            throw new Exception(sprintf('can\t delete file \'%s\'', $this->getFilename($plain)));
335 14
        }
336
        unlink($this->getPathname($plain));
337 14
    }
338
339 14
    /**
340
     * Return path and filename of the backup file.
341
     *
342
     * @param  boolean $plain
343
     * @return string
344
     */
345
    public function getPathname($plain = false)
346
    {
347 1
        return $this->path
348
        . DIRECTORY_SEPARATOR
349 1
        . $this->getFilename($plain);
350
    }
351
352
    /**
353
     * Return path and plain filename of the backup file.
354
     *
355
     * @return string
356
     */
357 2
    public function getPathnamePlain()
358
    {
359 2
        return $this->getPathname(true);
360
    }
361
362
    /**
363
     * Is dirname configured with any date placeholders.
364
     *
365
     * @return boolean
366
     */
367 7
    public function hasChangingPath()
368
    {
369 7
        return $this->pathIsChanging;
370
    }
371
372
    /**
373
     * Return the part of the path that is not changing.
374
     *
375
     * @return string
376
     */
377 1
    public function getPathThatIsNotChanging()
378
    {
379 1
        return $this->pathNotChanging;
380
    }
381
382
    /**
383
     * Changing path elements getter.
384
     *
385
     * @return array
386
     */
387 9
    public function getChangingPathElements()
388
    {
389 9
        return $this->pathElementsChanging;
390
    }
391
392
    /**
393
     * Return amount of changing path elements.
394
     *
395
     * @return integer
396
     */
397 2
    public function countChangingPathElements()
398
    {
399 2
        return count($this->pathElementsChanging);
400
    }
401
402
    /**
403
     * Filename configured with any date placeholders.
404
     *
405
     * @return boolean
406
     */
407
    public function hasChangingFilename()
408
    {
409
        return $this->filenameIsChanging;
410
    }
411
412
    /**
413
     * Disable file compression.
414
     */
415
    public function disableCompression()
416
    {
417
        $this->compress = false;
418
    }
419
420
    /**
421
     * Enable file compression.
422
     *
423
     * @throws \phpbu\App\Exception
424
     */
425
    public function enableCompression()
426
    {
427
        if (null == $this->compressor) {
428 7
            throw new Exception('can\'t enable compression without a compressor');
429
        }
430 7
        $this->compress = true;
431 7
    }
432 7
433
    /**
434
     * Compressor setter.
435
     *
436
     * @param \phpbu\App\Backup\Compressor $compressor
437
     */
438
    public function setCompressor(Compressor $compressor)
439 2
    {
440
        $this->compressor = $compressor;
441 2
        $this->compress   = true;
442
    }
443
444
    /**
445
     * Compressor getter.
446
     *
447
     * @return \phpbu\App\Backup\Compressor
448
     */
449 20
    public function getCompressor()
450
    {
451 20
        return $this->compressor;
452
    }
453
454
    /**
455
     * Is a compressor set?
456
     *
457
     * @return boolean
458
     */
459 1
    public function shouldBeCompressed()
460
    {
461 1
        return $this->compress !== false;
462
    }
463
464
    /**
465
     * Is the target already compressed.
466
     *
467
     * @return boolean
468
     */
469 2
    public function isCompressed()
470
    {
471 2
        return $this->shouldBeCompressed() ? file_exists($this->getPathname()) : false;
472 2
    }
473 2
474
    /**
475
     * Crypter setter.
476
     *
477
     * @param \phpbu\App\Backup\Crypter $crypter
478
     */
479
    public function setCrypter(Crypter $crypter)
480 1
    {
481
        $this->crypter = $crypter;
482 1
        $this->crypt   = true;
483
    }
484
485
    /**
486
     * Crypter getter.
487
     *
488
     * @return \phpbu\App\Backup\Crypter
489
     */
490
    public function getCrypter()
491
    {
492
        return $this->crypter;
493
    }
494
495
    /**
496
     * Disable file encryption.
497
     */
498 16
    public function disableEncryption()
499
    {
500 16
        $this->crypt = false;
501
    }
502
503
    /**
504
     * Is a crypter set?
505
     *
506
     * @return boolean
507
     */
508 2
    public function shouldBeEncrypted()
509
    {
510 2
        return $this->crypt !== false;
511
    }
512
513
    /**
514
     * Magic to string method.
515
     *
516
     * @return string
517
     */
518
    public function __toString()
519
    {
520
        return $this->getPathname();
521
    }
522
}
523