Completed
Push — master ( eaaddc...057168 )
by Sebastian
09:01
created

Source::compressDirectory()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 3
crap 3
1
<?php
2
namespace phpbu\App\Runner\Backup;
3
4
use phpbu\App\Backup\Compressor;
5
use phpbu\App\Backup\Source as SourceExe;
6
use phpbu\App\Backup\Source\Simulator;
7
use phpbu\App\Backup\Source\Status;
8
use phpbu\App\Backup\Target;
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       https://phpbu.de/
20
 * @since      Class available since Release 3.0.0
21
 */
22
class Source extends Abstraction
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 4
    public function run(SourceExe $source, Target $target, Result $result)
33
    {
34 4
        $this->isSimulation() ? $this->simulate($source, $target, $result) : $this->backup($source, $target, $result);
35 4
    }
36
37
    /**
38
     * Executes the backup and compression.
39
     *
40
     * @param  \phpbu\App\Backup\Source $source
41
     * @param  \phpbu\App\Backup\Target $target
42
     * @param  \phpbu\App\Result        $result
43
     * @throws \phpbu\App\Exception
44
     */
45 2
    protected function backup(SourceExe $source, Target $target, Result $result)
46
    {
47 2
        $status = $source->backup($target, $result);
48 2
        $this->compress($status, $target, $result);
49 2
    }
50
51
    /**
52
     * Simulates the backup.
53
     *
54
     * @param  \phpbu\App\Backup\Source $source
55
     * @param  \phpbu\App\Backup\Target $target
56
     * @param  \phpbu\App\Result        $result
57
     * @throws \phpbu\App\Exception
58
     */
59 2
    protected function simulate(SourceExe $source, Target $target, Result $result)
60
    {
61 2
        if ($source instanceof Simulator) {
62 2
            $status = $source->simulate($target, $result);
63 2
            $this->compress($status, $target, $result);
64
        }
65 2
    }
66
67
    /**
68
     * Compress the backup if the source did not handle the compression.
69
     *
70
     * @param  \phpbu\App\Backup\Source\Status $status
71
     * @param  \phpbu\App\Backup\Target        $target
72
     * @param  \phpbu\App\Result               $result
73
     * @throws \phpbu\App\Exception
74
     */
75 4
    protected function compress(Status $status, Target $target, Result $result)
76
    {
77
        // if the target is not compressed yet
78
        // and should be compressed or at least archived (tar)
79 4
        if (!$status->handledCompression() && ($target->shouldBeCompressed() || $status->isDirectory())) {
80 3
            $this->handleCompression($target, $result, $status);
81
        }
82 4
    }
83
84
    /**
85
     * Handle directory compression for sources which can't handle compression by them self.
86
     *
87
     * @param  \phpbu\App\Backup\Target        $target
88
     * @param  \phpbu\App\Result               $result
89
     * @param  \phpbu\App\Backup\Source\Status $status
90
     * @throws \phpbu\App\Exception
91
     */
92 3
    private function handleCompression(Target $target, Result $result, Status $status)
93
    {
94
        // is backup data a directory or a file
95 3
        if ($status->isDirectory()) {
96 1
            $this->compressDirectory($target, $result, $status->getDataPath());
97
        } else {
98 2
            $this->compressFile($target, $result, $status->getDataPath());
99
        }
100 3
    }
101
102
    /**
103
     * Compresses the target if the target is a directory.
104
     *
105
     * @param  \phpbu\App\Backup\Target $target
106
     * @param  \phpbu\App\Result        $result
107
     * @param  string                   $dataToCompress
108
     * @throws \phpbu\App\Exception
109
     */
110 1
    private function compressDirectory(Target $target, Result $result, $dataToCompress)
111
    {
112
        // archive data
113 1
        $dirCompressor = new Compressor\Directory($dataToCompress);
114 1
        $archiveFile   = $this->executeCompressor($dirCompressor, $target, $result);
115
116
        // if target should be compressed but tar couldn't handle the compression
117
        // run extra file compression to compress the tar file
118 1
        if ($target->shouldBeCompressed() && !$dirCompressor->canCompress($target)) {
119 1
            $this->compressFile($target, $result, $archiveFile);
120
        }
121 1
    }
122
123
    /**
124
     * Compresses the target if the target is a single file.
125
     *
126
     * @param  \phpbu\App\Backup\Target $target
127
     * @param  \phpbu\App\Result        $result
128
     * @param  string                   $dataToCompress
129
     * @throws \phpbu\App\Exception
130
     */
131 3
    private function compressFile(Target $target, Result $result, $dataToCompress)
132
    {
133 3
        $fileCompressor = new Compressor\File($dataToCompress);
134 3
        $this->executeCompressor($fileCompressor, $target, $result);
135 3
    }
136
137
    /**
138
     * Execute the compressor.
139
     * Returns the path to the created archive file.
140
     *
141
     * @param  \phpbu\App\Backup\Compressor\Executable $compressor
142
     * @param  \phpbu\App\Backup\Target                $target
143
     * @param  \phpbu\App\Result                       $result
144
     * @return string
145
     */
146 3
    private function executeCompressor(Compressor\Executable $compressor, Target $target, Result $result) : string
147
    {
148
        // if this is a simulation just debug the command that would have been executed
149 3
        if ($this->isSimulation()) {
150 2
            $result->debug($compressor->getExecutable($target)->getCommand());
151 2
            return $compressor->getArchiveFile($target);
152
        } else {
153 1
            return $compressor->compress($target, $result);
154
        }
155
    }
156
}
157