Completed
Pull Request — master (#424)
by
unknown
20:12
created

Gzip::compress()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 0
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
1
<?php
2
3
4
namespace Spatie\Backup\Tasks\Backup;
5
6
class Gzip
7
{
8
9
    /** @var string */
10
    public $originalFilePath;
11
12
    /** @var string */
13
    public $filePath;
14
15
    /** @var bool */
16
    public $failed;
17
18
    public function __construct(string $filePath)
19
    {
20
        $this->originalFilePath = $filePath;
21
22
        $this->filePath = $this->compress();
23
24
        $this->failed = $this->originalFilePath == $this->filePath;
25
    }
26
27
    /**
28
     * @return string
29
     */
30
    protected function compress()
31
    {
32
        $gzipPath = $this->originalFilePath.'.gz';
33
34
        if ($gzipOut = gzopen($gzipPath, 'w9')) {
35
            if ($gzipIn = fopen($this->originalFilePath, 'rb')) {
36
                while (! feof($gzipIn)) {
37
                    gzwrite($gzipOut, fread($gzipIn, 1024 * 512));
38
                }
39
                fclose($gzipIn);
40
            } else {
41
                return $this->originalFilePath;
42
            }
43
44
            gzclose($gzipOut);
45
        } else {
46
            return $this->originalFilePath;
47
        }
48
49
        return $gzipPath;
50
    }
51
52
    /**
53
     * @return int
54
     */
55
    public function oldFileSize()
56
    {
57
        return filesize($this->originalFilePath);
58
    }
59
60
    /**
61
     * @return int
62
     */
63
    public function newFileSize()
64
    {
65
        return filesize($this->filePath);
66
    }
67
}
68