Completed
Pull Request — master (#145)
by Vitaly
02:44
created

Target::getFilenamePlain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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