Completed
Push — master ( ee3fe7...343c0b )
by Sebastian
03:07
created

Tar::tarFailed()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 1
cts 1
cp 1
rs 9.4285
cc 3
eloc 3
nc 4
nop 1
crap 3
1
<?php
2
namespace phpbu\App\Backup\Source;
3
4
use phpbu\App\Backup\Source;
5
use phpbu\App\Backup\Target;
6
use phpbu\App\Cli\Executable;
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
     * Tar should ignore failed reads
47
     * --ignore-failed-read
48
     *
49
     * @var boolean
50
     */
51
    private $ignoreFailedRead;
52
53
    /**
54
     * Remove the packed data
55
     *
56
     * @var boolean
57
     */
58
    private $removeSourceDir;
59
60
    /**
61
     * Compression to use.
62
     *
63
     * @var string
64
     */
65
    private $compression;
66
67
    /**
68
     * Path where to store the archive.
69
     *
70
     * @var string
71
     */
72
    private $pathToArchive;
73
74
    /**
75
     * Setup.
76
     *
77
     * @see    \phpbu\App\Backup\Source
78
     * @param  array $conf
79
     * @throws \phpbu\App\Exception
80
     */
81 7
    public function setup(array $conf = [])
82
    {
83 7
        $this->pathToTar        = Util\Arr::getValue($conf, 'pathToTar');
84 7
        $this->path             = Util\Arr::getValue($conf, 'path');
85 7
        $this->ignoreFailedRead = Util\Str::toBoolean(Util\Arr::getValue($conf, 'ignoreFailedRead', ''), false);
86 7
        $this->removeSourceDir  = Util\Str::toBoolean(Util\Arr::getValue($conf, 'removeSourceDir', ''), false);
87
88 7
        if (empty($this->path)) {
89 1
            throw new Exception('path option is mandatory');
90
        }
91 6
    }
92
93
    /**
94
     * Execute the backup.
95
     *
96
     * @see    \phpbu\App\Backup\Source
97
     * @param  \phpbu\App\Backup\Target $target
98
     * @param  \phpbu\App\Result        $result
99
     * @return \phpbu\App\Backup\Source\Status
100
     * @throws \phpbu\App\Exception
101
     */
102 3
    public function backup(Target $target, Result $result)
103
    {
104
        // make sure source path is a directory
105 3
        $this->validatePath();
106 3
        // set uncompressed default MIME type
107 3
        $target->setMimeType('application/x-tar');
108
        $tar = $this->execute($target);
109
110 3
        $result->debug($tar->getCmd());
111 1
112 1
        if ($this->tarFailed($tar)) {
113
            throw new Exception('tar failed: ' . $tar->getStdErr());
114 3
        }
115
116 3
        return $this->createStatus($target);
117 1
    }
118
119
    /**
120 2
     * Check if tar failed.
121
     *
122
     * @param  \phpbu\App\Cli\Result $tar
123
     * @return bool
124
     */
125
    public function tarFailed(\phpbu\App\Cli\Result $tar)
126
    {
127
        $ignoreFail = $this->ignoreFailedRead && $tar->getCode() == 1;
128
        return !$tar->wasSuccessful() && !$ignoreFail;
129 6
    }
130
131 6
    /**
132
     * Setup the Executable to run the 'tar' command.
133 3
     *
134 2
     * @param  \phpbu\App\Backup\Target
135 1
     * @return \phpbu\App\Cli\Executable
136 1
     */
137
    public function getExecutable(Target $target)
138 1
    {
139 1
        if (null == $this->executable) {
140
            // check if tar supports requested compression
141 2
            if ($target->shouldBeCompressed()) {
142
                if (!Executable\Tar::isCompressorValid($target->getCompressor()->getCommand())) {
143 1
                    $this->pathToArchive = $target->getPathnamePlain();
144
                } else {
145 3
                    // compression could be handled by the tar command
146 3
                    $this->pathToArchive = $target->getPathname();
147 3
                    $this->compression   = $target->getCompressor()->getCommand();
148 3
                }
149 3
            } else {
150 3
                // no compression at all
151 6
                $this->pathToArchive = $target->getPathname();
152
            }
153
154
            $this->executable = new Executable\Tar($this->pathToTar);
155
            $this->executable->archiveDirectory($this->path)
156
                             ->useCompression($this->compression)
157
                             ->ignoreFailedRead($this->ignoreFailedRead)
158
                             ->removeSourceDirectory($this->removeSourceDir)
159
                             ->archiveTo($this->pathToArchive);
160
        }
161
        return $this->executable;
162
    }
163
164
    /**
165
     * Check the source to compress.
166
     *
167
     * @throws \phpbu\App\Exception
168
     */
169
    private function validatePath()
170
    {
171
        if (!is_dir($this->path)) {
172
            throw new Exception('path to compress has to be a directory');
173
        }
174
    }
175
176
    /**
177
     * Create backup status.
178
     *
179
     * @param  \phpbu\App\Backup\Target
180
     * @return \phpbu\App\Backup\Source\Status
181
     */
182
    protected function createStatus(Target $target)
183
    {
184
        $status = Status::create();
185
        // if tar doesn't handle the compression mark status uncompressed so the app can take care of compression
186
        if (!$this->getExecutable($target)->handlesCompression()) {
187
            $status->uncompressedFile($target->getPathnamePlain());
188
        }
189
        return $status;
190
    }
191
}
192