Completed
Push — master ( 35413c...f854d5 )
by Sebastian
21:09
created

Tar::backup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 16
loc 16
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
crap 2
1
<?php
2
namespace phpbu\App\Backup\Source;
3
4
use phpbu\App\Backup\Target;
5
use phpbu\App\Cli\Executable;
6
use phpbu\App\Cli\Result as CliResult;
7
use phpbu\App\Exception;
8
use phpbu\App\Result;
9
use phpbu\App\Util;
10
11
/**
12
 * Tar source class.
13
 *
14
 * @package    phpbu
15
 * @subpackage Backup
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 1.0.0
21
 */
22
class Tar extends SimulatorExecutable implements Simulator
23
{
24
    /**
25
     * Tar Executable
26
     *
27
     * @var \phpbu\App\Cli\Executable\Tar
28
     */
29
    protected $executable;
30
31
    /**
32
     * Path to executable.
33
     *
34
     * @var string
35
     */
36
    private $pathToTar;
37
38
    /**
39
     * Path to backup
40
     *
41
     * @var string
42
     */
43
    private $path;
44
45
    /**
46
     * List of paths to exclude
47
     * --exclude
48
     *
49
     * @var array
50
     */
51
    private $excludes;
52
53
    /**
54
     * Special compression program
55
     * --use-compress-program
56
     *
57
     * @var string
58
     */
59
    private $compressProgram;
60
61
    /**
62
     * Force local file resolution
63
     *
64
     * --force-local
65
     *
66
     * @var bool
67
     */
68
    private $forceLocal;
69
70
    /**
71
     * Tar should ignore failed reads
72
     * --ignore-failed-read
73
     *
74
     * @var bool
75
     */
76
    private $ignoreFailedRead;
77
78
    /**
79
     * Remove the packed data
80
     *
81
     * @var bool
82
     */
83
    private $removeSourceDir;
84
85
    /**
86
     * Compression to use.
87
     *
88
     * @var string
89
     */
90
    private $compression = '';
91
92
    /**
93
     * Throttle cpu usage.
94
     *
95
     * @var string
96
     */
97
    private $throttle = '';
98
99
    /**
100
     * Path where to store the archive.
101
     *
102
     * @var string
103
     */
104
    private $pathToArchive;
105
106
    /**
107
     * Setup.
108
     *
109
     * @see    \phpbu\App\Backup\Source
110
     * @param  array $conf
111
     * @throws \phpbu\App\Exception
112
     */
113 17
    public function setup(array $conf = [])
114
    {
115 17
        $this->pathToTar        = Util\Arr::getValue($conf, 'pathToTar', '');
116 17
        $this->path             = Util\Arr::getValue($conf, 'path', '');
117 17
        $this->excludes         = Util\Str::toList(Util\Arr::getValue($conf, 'exclude', ''));
118 17
        $this->compressProgram  = Util\Arr::getValue($conf, 'compressProgram', '');
119 17
        $this->throttle         = Util\Arr::getValue($conf, 'throttle', '');
120 17
        $this->forceLocal       = Util\Str::toBoolean(Util\Arr::getValue($conf, 'forceLocal', ''), false);
121 17
        $this->ignoreFailedRead = Util\Str::toBoolean(Util\Arr::getValue($conf, 'ignoreFailedRead', ''), false);
122 17
        $this->removeSourceDir  = Util\Str::toBoolean(Util\Arr::getValue($conf, 'removeSourceDir', ''), false);
123
124 17
        if (empty($this->path)) {
125 1
            throw new Exception('path option is mandatory');
126
        }
127 16
    }
128
129
    /**
130
     * Execute the backup.
131
     *
132
     * @see    \phpbu\App\Backup\Source
133
     * @param  \phpbu\App\Backup\Target $target
134
     * @param  \phpbu\App\Result        $result
135
     * @return \phpbu\App\Backup\Source\Status
136
     * @throws \phpbu\App\Exception
137
     */
138 7 View Code Duplication
    public function backup(Target $target, Result $result) : Status
139
    {
140
        // make sure source path is a directory
141 7
        $this->validatePath();
142
        // set uncompressed default MIME type
143 6
        $target->setMimeType('application/x-tar');
144 6
        $tar = $this->execute($target);
145
146 5
        $result->debug($tar->getCmdPrintable());
147
148 5
        if (!$tar->isSuccessful()) {
149 2
            throw new Exception('tar failed: ' . $tar->getStdErr());
150
        }
151
152 3
        return $this->createStatus($target);
153
    }
154
155
    /**
156
     * Setup the Executable to run the 'tar' command.
157
     *
158
     * @param  \phpbu\App\Backup\Target
159
     * @return \phpbu\App\Cli\Executable
160
     */
161 15
    protected function createExecutable(Target $target) : Executable
162
    {
163 15
        $this->pathToArchive = $target->getPathnamePlain();
164
165
        // check if archive should be compressed and tar supports requested compression
166 15
        if ($target->shouldBeCompressed()
167 15
            && Executable\Tar::isCompressionValid($target->getCompression()->getCommand())) {
168 5
            $this->pathToArchive = $target->getPathname();
169 5
            $this->compression   = $target->getCompression()->getCommand();
170
        }
171
172 15
        $executable = new Executable\Tar($this->pathToTar);
173 15
        $executable->archiveDirectory($this->path)
174 15
                   ->useCompression($this->compression)
175 15
                   ->useCompressProgram($this->compressProgram)
176 15
                   ->forceLocal($this->forceLocal)
177 15
                   ->ignoreFailedRead($this->ignoreFailedRead)
178 15
                   ->removeSourceDirectory($this->removeSourceDir)
179 15
                   ->throttle($this->throttle)
180 15
                   ->archiveTo($this->pathToArchive);
181
        // add paths to exclude
182 15
        foreach ($this->excludes as $path) {
183 1
            $executable->addExclude($path);
184
        }
185
186 15
        return $executable;
187
    }
188
189
    /**
190
     * Check the source to compress.
191
     *
192
     * @throws \phpbu\App\Exception
193
     */
194 7
    private function validatePath()
195
    {
196 7
        if (!is_dir($this->path)) {
197 1
            throw new Exception('path to compress has to be a directory');
198
        }
199 6
    }
200
201
    /**
202
     * Create backup status.
203
     *
204
     * @param  \phpbu\App\Backup\Target
205
     * @return \phpbu\App\Backup\Source\Status
206
     */
207 3
    protected function createStatus(Target $target) : Status
208
    {
209 3
        $status = Status::create();
210
        // if tar doesn't handle the compression mark status uncompressed
211
        // so the app can take care of compression
212 3
        if (!$this->executable->handlesCompression()) {
213 1
            $status->uncompressedFile($target->getPathnamePlain());
214
        }
215 3
        return $status;
216
    }
217
}
218