Passed
Push — master ( 3b7697...25d5dd )
by Alexey
03:48 queued 29s
created

ZipCompressionMethod   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 47
c 1
b 0
f 1
dl 0
loc 89
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCompressionMethodName() 0 5 2
A checkSupport() 0 9 2
A getSupportMethods() 0 16 3
1
<?php
2
3
namespace PhpZip\Constants;
4
5
use PhpZip\Exception\ZipUnsupportMethodException;
6
7
/**
8
 * Class ZipCompressionMethod.
9
 */
10
final class ZipCompressionMethod
11
{
12
    /** @var int Compression method Store */
13
    const STORED = 0;
14
15
    /** @var int Compression method Deflate */
16
    const DEFLATED = 8;
17
18
    /** @var int Compression method Bzip2 */
19
    const BZIP2 = 12;
20
21
    /** @var int Compression method AES-Encryption */
22
    const WINZIP_AES = 99;
23
24
    /** @var array Compression Methods */
25
    private static $ZIP_COMPRESSION_METHODS = [
26
        self::STORED => 'Stored',
27
        1 => 'Shrunk',
28
        2 => 'Reduced compression factor 1',
29
        3 => 'Reduced compression factor 2',
30
        4 => 'Reduced compression factor 3',
31
        5 => 'Reduced compression factor 4',
32
        6 => 'Imploded',
33
        7 => 'Reserved for Tokenizing compression algorithm',
34
        self::DEFLATED => 'Deflated',
35
        9 => 'Enhanced Deflating using Deflate64(tm)',
36
        10 => 'PKWARE Data Compression Library Imploding',
37
        11 => 'Reserved by PKWARE',
38
        self::BZIP2 => 'BZIP2',
39
        13 => 'Reserved by PKWARE',
40
        14 => 'LZMA',
41
        15 => 'Reserved by PKWARE',
42
        16 => 'Reserved by PKWARE',
43
        17 => 'Reserved by PKWARE',
44
        18 => 'File is compressed using IBM TERSE (new)',
45
        19 => 'IBM LZ77 z Architecture (PFS)',
46
        96 => 'WinZip JPEG Compression',
47
        97 => 'WavPack compressed data',
48
        98 => 'PPMd version I, Rev 1',
49
        self::WINZIP_AES => 'AES Encryption',
50
    ];
51
52
    /**
53
     * @param int $value
54
     *
55
     * @return string
56
     */
57
    public static function getCompressionMethodName($value)
58
    {
59
        return isset(self::$ZIP_COMPRESSION_METHODS[$value]) ?
60
            self::$ZIP_COMPRESSION_METHODS[$value] :
61
            'Unknown Method';
62
    }
63
64
    /**
65
     * @return int[]
66
     */
67
    public static function getSupportMethods()
68
    {
69
        static $methods;
70
71
        if ($methods === null) {
72
            $methods = [
73
                self::STORED,
74
                self::DEFLATED,
75
            ];
76
77
            if (\extension_loaded('bz2')) {
78
                $methods[] = self::BZIP2;
79
            }
80
        }
81
82
        return $methods;
83
    }
84
85
    /**
86
     * @param int $compressionMethod
87
     *
88
     * @throws ZipUnsupportMethodException
89
     */
90
    public static function checkSupport($compressionMethod)
91
    {
92
        $compressionMethod = (int) $compressionMethod;
93
94
        if (!\in_array($compressionMethod, self::getSupportMethods(), true)) {
95
            throw new ZipUnsupportMethodException(sprintf(
96
                'Compression method %d (%s) is not supported.',
97
                $compressionMethod,
98
                self::getCompressionMethodName($compressionMethod)
99
            ));
100
        }
101
    }
102
}
103