Passed
Push — master ( 308831...125b29 )
by f
13:31
created

Bzip::checkFormatSupport()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 9
ccs 0
cts 4
cp 0
crap 12
rs 10
c 0
b 0
f 0
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
     * @inheritDoc
26
     */
27
    public static function getDescription()
28
    {
29
        return 'adapter for ext-bzip2';
30
    }
31
32
    /**
33
     * @inheritDoc
34
     */
35
    public function __construct($archiveFileName, $format, $password = null)
36
    {
37
        parent::__construct($archiveFileName, $password);
38
        $this->modificationTime = filemtime($this->fileName);
39
    }
40
41
    /**
42
     * @param string $fileName
43
     *
44
     * @return string|false
45
     */
46
    public function getFileContent($fileName = null)
47
    {
48
        return bzdecompress(file_get_contents($this->fileName));
49
    }
50
51
    /**
52
     * @param string $fileName
53
     *
54
     * @return bool|resource|string
55
     */
56
    public function getFileStream($fileName = null)
57
    {
58
        return bzopen($this->fileName, 'r');
59
    }
60
61
    /**
62
     * @param string $data
63
     * @param int $compressionLevel
64
     * @return mixed|string
65
     */
66
    protected static function compressData($data, $compressionLevel)
67
    {
68
        static $compressionLevelMap = [
69
            self::COMPRESSION_NONE => 1,
70
            self::COMPRESSION_WEAK => 2,
71
            self::COMPRESSION_AVERAGE => 4,
72
            self::COMPRESSION_STRONG => 7,
73
            self::COMPRESSION_MAXIMUM => 9,
74
        ];
75
        static $work_factor_multiplier = 27;
76
77
        // it seems not working at all
78
        $work_factor = ($compressionLevelMap[$compressionLevel] * $work_factor_multiplier);
79
        return bzcompress($data, $compressionLevelMap[$compressionLevel], $work_factor);
80
    }
81
}