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

Source::handleCompression()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4286
cc 2
eloc 5
nc 2
nop 3
1
<?php
2
namespace phpbu\App\Runner;
3
4
use phpbu\App\Backup\Compressor;
5
use phpbu\App\Backup\Source as SourceExe;
6
use phpbu\App\Backup\Source\Status;
7
use phpbu\App\Backup\Target;
8
use phpbu\App\Configuration;
9
use phpbu\App\Result;
10
11
/**
12
 * Backup Runner
13
 *
14
 * @package    phpbu
15
 * @subpackage app
16
 * @author     Sebastian Feldmann <[email protected]>
17
 * @copyright  Sebastian Feldmann <[email protected]>
18
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
19
 * @link       http://phpbu.de/
20
 * @since      Class available since Release 3.0.0
21
 */
22
class Source
23
{
24
    /**
25
     * Executes the backup and compression.
26
     *
27
     * @param  \phpbu\App\Backup\Source $source
28
     * @param  \phpbu\App\Backup\Target $target
29
     * @param  \phpbu\App\Result        $result
30
     * @throws \phpbu\App\Exception
31
     */
32
    public function run(SourceExe $source, Target $target, Result $result)
33
    {
34
        $status = $source->backup($target, $result);
35
        if ($target->shouldBeCompressed()) {
36
            if (is_a($status, '\\phpbu\\App\\Backup\\Source\\Status') && !$status->handledCompression()) {
37
                $this->handleCompression($target, $result, $status->getDataPath());
38
            }
39
        }
40
    }
41
42
    /**
43
     * Handle directory compression for sources which can't handle compression by them self.
44
     *
45
     * @param  \phpbu\App\Backup\Target $target
46
     * @param  \phpbu\App\Result        $result
47
     * @param  string                   $dataToCompress
48
     * @throws \phpbu\App\Exception
49
     */
50
    protected function handleCompression(Target $target, Result $result, $dataToCompress)
51
    {
52
        // is backup data a directory or a file
53
        if (is_dir($dataToCompress)) {
54
            $this->compressDirectory($target, $result, $dataToCompress);
55
        } else {
56
            $this->compressFile($target, $result, $dataToCompress);
57
58
        }
59
    }
60
61
    /**
62
     * Compresses the target if the target is a directory.
63
     *
64
     * @param  \phpbu\App\Backup\Target $target
65
     * @param  \phpbu\App\Result        $result
66
     * @param  string                   $dataToCompress
67
     * @throws \phpbu\App\Exception
68
     */
69
    protected function compressDirectory(Target $target, Result $result, $dataToCompress)
70
    {
71
        // archive data
72
        $dirCompressor = new Compressor\Directory($dataToCompress);
73
        $dirCompressor->compress($target, $result);
74
        // directory is archived but not compressed because tar couldn't handle the compression
75
        if (!file_exists($target->getPathname()) && file_exists($target->getPathnamePlain())) {
76
            // compress the tar with the configured compression
77
            $this->compressFile($target, $result, $target->getPathnamePlain());
78
        }
79
    }
80
81
    /**
82
     * Compresses the target if the target is a single file.
83
     *
84
     * @param  \phpbu\App\Backup\Target $target
85
     * @param  \phpbu\App\Result        $result
86
     * @param  string                   $dataToCompress
87
     * @throws \phpbu\App\Exception
88
     */
89
    protected function compressFile(Target $target, Result $result, $dataToCompress)
90
    {
91
        $fileCompressor = new Compressor\File($dataToCompress);
92
        $fileCompressor->compress($target, $result);
93
    }
94
}
95