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

Password::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
nc 3
nop 2
dl 0
loc 21
rs 9.9332
c 3
b 0
f 0
1
<?php
2
3
namespace Olssonm\BackupShield\Factories;
4
5
use Illuminate\Support\Collection;
6
7
use Olssonm\BackupShield\Encryption;
8
use PhpZip\ZipFile;
9
use \ZipArchive;
10
11
class Password
12
{
13
    /**
14
     * Path to .zip-file
15
     *
16
     * @var string
17
     */
18
    public $path;
19
20
    /**
21
     * The chosen password
22
     *
23
     * @var string
24
     */
25
    protected $password;
26
27
    /**
28
     * Read the .zip, apply password and encryption, then rewrite the file
29
     * @param string $path the path to the .zip-file
30
     */
31
    function __construct(Encryption $encryption, string $path)
32
    {
33
        $this->password = config('backup-shield.password');
34
35
        if (!$this->password) {
36
            return $this->path = $path;
37
        }
38
39
        // If ZipArchive is enabled
40
        if (class_exists('ZipArchive') && in_array('setEncryptionIndex', get_class_methods('ZipArchive'))) {
41
            consoleOutput()->info('Applying password and encryption to zip using ZipArchive...');
42
            $this->makeZipArchive($encryption, $path);
43
        }
44
45
        // Fall back on PHP-driven ZipFile
46
        else {
47
            consoleOutput()->info('Applying password and encryption to zip using ZipFile...');
48
            $this->makeZipFile($encryption, $path);
49
        }
50
51
        consoleOutput()->info('Successfully applied password and encryption to zip.');
52
    }
53
54
    /**
55
     * Use native PHP ZipArchive
56
     *
57
     * @param   Encryption $encryption
58
     * @return  void
59
     */
60
    protected function makeZipArchive(Encryption $encryption, string $path) : void
61
    {
62
        $encryptionConstant = $encryption->getEncryptionConstant(
63
            config('backup-shield.encryption'),
64
            'ZipArchive'
65
        );
66
67
        $zipArchive = new ZipArchive;
68
69
        $zipArchive->open($path, ZipArchive::OVERWRITE);
70
        $zipArchive->addFile($path, 'backup.zip');
71
        $zipArchive->setPassword($this->password);
72
        Collection::times($zipArchive->numFiles, function ($i) use ($zipArchive, $encryptionConstant) {
73
            $zipArchive->setEncryptionIndex($i - 1, $encryptionConstant);
74
        });
75
        $zipArchive->close();
76
77
        $this->path = $path;
78
    }
79
80
    /**
81
     * Use PhpZip\ZipFile-package to create the zip
82
     *
83
     * @param   Encryption $encryption
84
     * @return  void
85
     */
86
    protected function makeZipFile(Encryption $encryption, string $path) : void
87
    {
88
        $encryptionConstant = $encryption->getEncryptionConstant(
89
            config('backup-shield.encryption'),
90
            'ZipFile'
91
        );
92
93
        $zipFile = new ZipFile();
94
        $zipFile->addFile($path, 'backup.zip', ZipFile::METHOD_DEFLATED);
95
        $zipFile->setPassword($this->password, $encryptionConstant);
96
        $zipFile->saveAsFile($path);
97
        $zipFile->close();
98
99
        $this->path = $path;
100
    }
101
}
102