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

Gzip   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 0
dl 0
loc 61
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A compress() 0 21 4
A oldFileSize() 0 4 1
A newFileSize() 0 4 1
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