ZipEncryptionMethod   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 28
dl 0
loc 80
ccs 19
cts 19
cp 1
rs 10
c 1
b 0
f 1
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getEncryptionMethodName() 0 7 2
A hasEncryptionMethod() 0 3 1
A isWinZipAesMethod() 0 10 1
A checkSupport() 0 8 2
1
<?php
2
3
namespace PhpZip\Constants;
4
5
use PhpZip\Exception\InvalidArgumentException;
6
7
/**
8
 * Class ZipEncryptionMethod.
9
 */
10
final class ZipEncryptionMethod
11
{
12
    const NONE = -1;
13
14
    /** @var int Traditional PKWARE encryption. */
15
    const PKWARE = 0;
16
17
    /** @var int WinZip AES-256 */
18
    const WINZIP_AES_256 = 1;
19
20
    /** @var int WinZip AES-128 */
21
    const WINZIP_AES_128 = 2;
22
23
    /** @var int WinZip AES-192 */
24
    const WINZIP_AES_192 = 3;
25
26
    /** @var array<int, string> */
27
    private static $ENCRYPTION_METHODS = [
28
        self::NONE => 'no encryption',
29
        self::PKWARE => 'Traditional PKWARE encryption',
30
        self::WINZIP_AES_128 => 'WinZip AES-128',
31
        self::WINZIP_AES_192 => 'WinZip AES-192',
32
        self::WINZIP_AES_256 => 'WinZip AES-256',
33
    ];
34
35
    /**
36
     * @param int $value
37
     *
38
     * @return string
39
     */
40 8
    public static function getEncryptionMethodName($value)
41
    {
42 8
        $value = (int) $value;
43
44 8
        return isset(self::$ENCRYPTION_METHODS[$value]) ?
45 8
            self::$ENCRYPTION_METHODS[$value] :
46 8
            'Unknown Encryption Method';
47
    }
48
49
    /**
50
     * @param int $encryptionMethod
51
     *
52
     * @return bool
53
     */
54 33
    public static function hasEncryptionMethod($encryptionMethod)
55
    {
56 33
        return isset(self::$ENCRYPTION_METHODS[$encryptionMethod]);
57
    }
58
59
    /**
60
     * @param int $encryptionMethod
61
     *
62
     * @return bool
63
     */
64 144
    public static function isWinZipAesMethod($encryptionMethod)
65
    {
66 144
        return \in_array(
67 144
            (int) $encryptionMethod,
68
            [
69 144
                self::WINZIP_AES_256,
70 144
                self::WINZIP_AES_192,
71 144
                self::WINZIP_AES_128,
72
            ],
73 144
            true
74
        );
75
    }
76
77
    /**
78
     * @param int $encryptionMethod
79
     *
80
     * @throws InvalidArgumentException
81
     */
82 33
    public static function checkSupport($encryptionMethod)
83
    {
84 33
        $encryptionMethod = (int) $encryptionMethod;
85
86 33
        if (!self::hasEncryptionMethod($encryptionMethod)) {
87 5
            throw new InvalidArgumentException(sprintf(
88 5
                'Encryption method %d is not supported.',
89
                $encryptionMethod
90
            ));
91
        }
92 29
    }
93
}
94