Completed
Pull Request — master (#424)
by
unknown
35:24 queued 19:17
created

Gzip::newFileSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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