Passed
Push — devel-3.0 ( 40e719...463a23 )
by Rubén
03:27
created

ArchiveHandler::compressFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Storage\File;
26
27
28
use SP\Core\Exceptions\CheckException;
29
use SP\Core\PhpExtensionChecker;
30
31
/**
32
 * Class ArchiveHandler
33
 *
34
 * @package SP\Storage\File
35
 */
36
final class ArchiveHandler
37
{
38
    const COMPRESS_EXTENSION = '.tar.gz';
39
40
    /**
41
     * @var PhpExtensionChecker
42
     */
43
    private $extensionChecker;
44
    /**
45
     * @var FileHandler
46
     */
47
    private $archive;
48
49
    /**
50
     * ArchiveHandler constructor.
51
     *
52
     * @param string              $archive
53
     * @param PhpExtensionChecker $extensionChecker
54
     */
55
    public function __construct(string $archive, PhpExtensionChecker $extensionChecker)
56
    {
57
        $this->extensionChecker = $extensionChecker;
58
        $this->archive = new FileHandler($archive . self::getArchiveExtension());
59
    }
60
61
    /**
62
     * @return bool|string
63
     */
64
    private static function getArchiveExtension()
65
    {
66
        return substr(self::COMPRESS_EXTENSION, 0, strrpos(self::COMPRESS_EXTENSION, '.'));
67
    }
68
69
    /**
70
     * Realizar un backup de la aplicación y comprimirlo.
71
     *
72
     * @param string      $directory
73
     * @param string|null $regex
74
     *
75
     * @throws CheckException
76
     * @throws FileException
77
     */
78
    public function compressDirectory(string $directory, string $regex = null)
79
    {
80
        $this->extensionChecker->checkPharAvailable(true);
81
82
        $archive = new \PharData($this->archive->getFile());
83
        $archive->buildFromDirectory($directory, $regex);
84
        $archive->compress(\Phar::GZ);
85
86
        // Delete the non-compressed archive
87
        $this->archive->delete();
88
    }
89
90
    /**
91
     * Realizar un backup de la aplicación y comprimirlo.
92
     *
93
     * @param string $file
94
     *
95
     * @throws CheckException
96
     * @throws FileException
97
     */
98
    public function compressFile(string $file)
99
    {
100
        $this->extensionChecker->checkPharAvailable(true);
101
102
        $archive = new \PharData($this->archive->getFile());
103
        $archive->addFile($file, basename($file));
104
        $archive->compress(\Phar::GZ);
105
106
        // Delete the non-compressed archive
107
        $this->archive->delete();
108
    }
109
110
    /**
111
     * @return FileHandler
112
     */
113
    public function getArchive(): FileHandler
114
    {
115
        return $this->archive;
116
    }
117
}