Passed
Push — 0.1.x ( 81ff64...72f342 )
by f
01:32
created

Gzip::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
namespace wapmorgan\UnifiedArchive\Formats;
3
4
use Exception;
5
6
class Gzip extends OneFileFormat
7
{
8
    const FORMAT_SUFFIX = 'gz';
9
10
    /**
11
     * Gzip constructor.
12
     *
13
     * @param $archiveFileName
14
     *
15
     * @throws \Exception
16
     */
17
    public function __construct($archiveFileName)
18
    {
19
        parent::__construct($archiveFileName);
20
        $stat = gzip_stat($archiveFileName);
21
        if ($stat === false) {
22
            throw new Exception('Could not open Gzip file');
23
        }
24
        $this->uncompressedSize = $stat['size'];
25
        $this->modificationTime = $stat['mtime'];
26
    }
27
28
    /**
29
     * @param string $fileName
30
     *
31
     * @return string|false
32
     */
33
    public function getFileContent($fileName = null)
34
    {
35
        return gzdecode(file_get_contents($this->fileName));
36
    }
37
38
    /**
39
     * @param string $fileName
40
     *
41
     * @return bool|resource|string
42
     */
43
    public function getFileResource($fileName = null)
44
    {
45
        return gzopen($this->fileName, 'rb');
46
    }
47
48
    /**
49
     * @param $data
50
     *
51
     * @return mixed|string
52
     */
53
    protected static function compressData($data)
54
    {
55
        return gzencode($data);
56
    }
57
}