Completed
Push — master ( e3b615...7e1512 )
by Marcus
05:48 queued 10s
created

Encryption::getEncryptionConstant()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 3
nop 2
dl 0
loc 8
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Olssonm\BackupShield;
4
5
use PhpZip\ZipFile;
6
7
use \ZipArchive;
8
9
class Encryption
10
{
11
    /**
12
     * Default encryption contants
13
     *
14
     * @var string
15
     */
16
    const ENCRYPTION_DEFAULT = 'default';
17
18
    /**
19
     * AES-128 encryption contants
20
     *
21
     * @var string
22
     */
23
    const ENCRYPTION_WINZIP_AES_128 = 'aes_128';
24
25
    /**
26
     * AES-192 encryption contants
27
     *
28
     * @var string
29
     */
30
    const ENCRYPTION_WINZIP_AES_192 = 'aes_192';
31
32
    /**
33
     * AES-256 encryption contants
34
     *
35
     * @var string
36
     */
37
    const ENCRYPTION_WINZIP_AES_256 = 'aes_256';
38
39
    /**
40
     * ZipArchive encryption constants; stores as simple string for PHP < 7.2
41
     * backwards compatability
42
     *
43
     * @var array
44
     */
45
    private $zipArchiveOptions = [
46
        self::ENCRYPTION_DEFAULT => '257',
47
        self::ENCRYPTION_WINZIP_AES_128 => '257',
48
        self::ENCRYPTION_WINZIP_AES_192 => '258',
49
        self::ENCRYPTION_WINZIP_AES_256 => '259',
50
    ];
51
52
    /**
53
     * ZipFile encryption constants
54
     *
55
     * @var array
56
     */
57
    private $zipFileOptions = [
58
        self::ENCRYPTION_DEFAULT => ZipFile::ENCRYPTION_METHOD_TRADITIONAL,
59
        self::ENCRYPTION_WINZIP_AES_128 => ZipFile::ENCRYPTION_METHOD_WINZIP_AES_128,
60
        self::ENCRYPTION_WINZIP_AES_192 => ZipFile::ENCRYPTION_METHOD_WINZIP_AES_192,
61
        self::ENCRYPTION_WINZIP_AES_256 => ZipFile::ENCRYPTION_METHOD_WINZIP_AES_256,
62
    ];
63
64
    /**
65
     * Retrive appropriate encryption constant
66
     *
67
     * @param  string $type
68
     * @param  string $engine
69
     * @return mixed
70
     */
71
    public function getEncryptionConstant($type, $engine)
72
    {
73
        if ($engine == 'ZipArchive' && isset($this->zipArchiveOptions[$type])) {
74
            return $this->zipArchiveOptions[$type];
75
        } elseif ($engine == 'ZipFile' && isset($this->zipFileOptions[$type])) {
76
            return $this->zipFileOptions[$type];
77
        } else {
78
            throw new \Exception("Encryption key not set or invalid value", 1);
79
        }
80
    }
81
}
82