Passed
Push — master ( e9611b...a3681d )
by f
13:07
created

Bzip::getInstallationInstruction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 6
rs 10
1
<?php
2
namespace wapmorgan\UnifiedArchive\Drivers\OneFile;
3
4
use wapmorgan\UnifiedArchive\Drivers\BasicDriver;
5
use wapmorgan\UnifiedArchive\Formats;
6
use wapmorgan\UnifiedArchive\Drivers\OneFile\OneFileDriver;
7
8
class Bzip extends OneFileDriver
9
{
10
    const FORMAT_SUFFIX =  'bz2';
11
    const PHP_EXTENSION = 'bz2';
12
    const FORMAT = Formats::BZIP;
13
14 1
    /**
15
     * @return array
16
     */
17 1
    public static function getSupportedFormats()
18
    {
19
        return [
20
            Formats::BZIP,
21
        ];
22
    }
23
24
    /**
25
     * @param $format
26
     * @return array
27
     */
28
    public static function checkFormatSupport($format)
29
    {
30
        if (!static::isInstalled()) {
31
            return [];
32
        }
33
34
        switch ($format) {
35
            case Formats::BZIP:
36
                return [BasicDriver::OPEN, BasicDriver::EXTRACT_CONTENT, BasicDriver::STREAM_CONTENT, BasicDriver::CREATE];
37
        }
38
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43
    public static function getDescription()
44
    {
45
        return 'adapter for ext-bzip2';
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51
    public function __construct($archiveFileName, $format, $password = null)
52
    {
53
        parent::__construct($archiveFileName, $password);
54
        $this->modificationTime = filemtime($this->fileName);
55
    }
56
57
    /**
58
     * @param string $fileName
59
     *
60
     * @return string|false
61
     */
62
    public function getFileContent($fileName = null)
63
    {
64
        return bzdecompress(file_get_contents($this->fileName));
65
    }
66
67
    /**
68
     * @param string $fileName
69
     *
70
     * @return bool|resource|string
71
     */
72
    public function getFileStream($fileName = null)
73
    {
74
        return bzopen($this->fileName, 'r');
75
    }
76
77
    /**
78
     * @param string $data
79
     * @param int $compressionLevel
80
     * @return mixed|string
81
     */
82
    protected static function compressData($data, $compressionLevel)
83
    {
84
        static $compressionLevelMap = [
85
            self::COMPRESSION_NONE => 1,
86
            self::COMPRESSION_WEAK => 2,
87
            self::COMPRESSION_AVERAGE => 4,
88
            self::COMPRESSION_STRONG => 7,
89
            self::COMPRESSION_MAXIMUM => 9,
90
        ];
91
        static $work_factor_multiplier = 27;
92
93
        // it seems not working at all
94
        $work_factor = ($compressionLevelMap[$compressionLevel] * $work_factor_multiplier);
95
        return bzcompress($data, $compressionLevelMap[$compressionLevel], $work_factor);
96
    }
97
}