Completed
Push — master ( c5e677...4df15a )
by Sebastian
02:56
created

Compression::handleCompression()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
crap 2
1
<?php
2
namespace phpbu\App\Runner;
3
4
use phpbu\App\Backup\Compressor;
5
use phpbu\App\Backup\Source\Status;
6
use phpbu\App\Backup\Target;
7
use phpbu\App\Result;
8
9
/**
10
 * Compression Runner
11
 *
12
 * @package    phpbu
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 5.1.0
18
 */
19
trait Compression
20
{
21
    /**
22
     * Compress the backup if the source did not handle the compression.
23
     *
24
     * @param  \phpbu\App\Backup\Source\Status $status
25
     * @param  \phpbu\App\Backup\Target        $target
26
     * @param  \phpbu\App\Result               $result
27
     * @throws \phpbu\App\Exception
28
     */
29 12
    protected function compress(Status $status, Target $target, Result $result)
30
    {
31
        // if the target is not compressed yet
32
        // and should be compressed or at least archived (tar)
33 12
        if (!$status->handledCompression() && ($target->shouldBeCompressed() || $status->isDirectory())) {
34 3
            $this->handleCompression($target, $result, $status);
35
        }
36 12
    }
37
38
    /**
39
     * Handle directory compression for sources which can't handle compression by them self.
40
     *
41
     * @param  \phpbu\App\Backup\Target        $target
42
     * @param  \phpbu\App\Result               $result
43
     * @param  \phpbu\App\Backup\Source\Status $status
44
     * @throws \phpbu\App\Exception
45
     */
46 3
    protected function handleCompression(Target $target, Result $result, Status $status)
47
    {
48
        // is backup data a directory or a file
49 3
        if ($status->isDirectory()) {
50 2
            $this->compressDirectory($target, $result, $status->getDataPath());
51
        } else {
52 1
            $this->compressFile($target, $result, $status->getDataPath());
53
        }
54 3
    }
55
56
    /**
57
     * Compresses the target if the target is a directory.
58
     *
59
     * @param  \phpbu\App\Backup\Target $target
60
     * @param  \phpbu\App\Result        $result
61
     * @param  string                   $dataToCompress
62
     * @throws \phpbu\App\Exception
63
     */
64 2
    protected function compressDirectory(Target $target, Result $result, $dataToCompress)
65
    {
66
        // archive data
67 2
        $dirCompressor = new Compressor\Directory($dataToCompress);
68 2
        $archiveFile   = $this->executeCompressor($dirCompressor, $target, $result);
69
70
        // if target should be compressed but tar couldn't handle the compression
71
        // run extra file compression to compress the tar file
72 2
        if ($target->shouldBeCompressed() && !$dirCompressor->canCompress($target)) {
73 1
            $this->compressFile($target, $result, $archiveFile);
74
        }
75 2
    }
76
77
    /**
78
     * Compresses the target if the target is a single file.
79
     *
80
     * @param  \phpbu\App\Backup\Target $target
81
     * @param  \phpbu\App\Result        $result
82
     * @param  string                   $dataToCompress
83
     */
84 2
    protected function compressFile(Target $target, Result $result, $dataToCompress)
85
    {
86 2
        $fileCompressor = new Compressor\File($dataToCompress);
87 2
        $this->executeCompressor($fileCompressor, $target, $result);
88 2
    }
89
90
    /**
91
     * Execute the compressor.
92
     * Returns the path to the created archive file.
93
     *
94
     * @param  \phpbu\App\Backup\Compressor\Executable $compressor
95
     * @param  \phpbu\App\Backup\Target                $target
96
     * @param  \phpbu\App\Result                       $result
97
     * @return string
98
     */
99
    abstract protected function executeCompressor(Compressor\Executable $compressor, Target $target, Result $result) : string;
100
}
101