Passed
Pull Request — master (#131)
by
unknown
02:51
created

Zip::__destruct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.0
13
 */
14
15
namespace Quantum\Libraries\Archive;
16
17
use ZipArchive;
18
19
/**
20
 * Class ArchiveInterface
21
 * @package Quantum\Libraries\Archive
22
 */
23
class Zip implements ArchiveInterface
24
{
25
    private $zipArchive;
26
27
    /**
28
     * Zip constructor
29
     */
30
    public function __construct()
31
    {
32
        $this->zipArchive = new ZipArchive();
33
    }
34
35
    public function __destruct()
36
    {
37
        if ($this->zipArchive->filename) {
38
            $this->zipArchive->close();
39
        }
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45
    public function addEmptyDir(string $archiveName, string $newDirectory): bool
46
    {
47
        if ($this->zipArchive->open($archiveName, ZipArchive::CREATE) === TRUE) {
48
            return $this->zipArchive->addEmptyDir($newDirectory);
49
        } else {
50
            return false;
51
        }
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function addFile(string $archiveName, string $filePath, string $newFileName = ''): bool
58
    {
59
        if ($this->zipArchive->open($archiveName, ZipArchive::CREATE) === TRUE) {
60
            return $this->zipArchive->addFile($filePath, $newFileName);
61
        } else {
62
            return false;
63
        }
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69
    public function addFromString(string $archiveName, string $newFileName, string $newFileContent): bool
70
    {
71
        if ($this->zipArchive->open($archiveName, ZipArchive::CREATE) === TRUE) {
72
            return $this->zipArchive->addFromString($newFileName, $newFileContent);
73
        } else {
74
            return false;
75
        }
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function deleteUsingName(string $archiveName, string $fileOrDirName): bool
82
    {
83
        if ($this->zipArchive->open($archiveName) === TRUE && $this->zipArchive->locateName($fileOrDirName)) {
84
            return $this->zipArchive->deleteName($fileOrDirName);
85
        } else {
86
            return false;
87
        }
88
    }
89
90
    /**
91
     * @inheritDoc
92
     */
93
    public function extractTo(string $archiveName, string $pathToExtract): bool
94
    {
95
        if ($this->zipArchive->open($archiveName) === TRUE) {
96
            return $this->zipArchive->extractTo($pathToExtract);
97
        } else {
98
            return false;
99
        }
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105
    public function renameUsingName(string $archiveName, string $currentName, string $newName): bool
106
    {
107
        if ($this->zipArchive->open($archiveName) === TRUE) {
108
            return $this->zipArchive->renameName($currentName, $newName);
109
        } else {
110
            return false;
111
        }
112
    }
113
114
    /**
115
     * @inheritDoc
116
     */
117
    public function addMultipleFiles(string $archiveName, array $fileNames): bool
118
    {
119
        if ($this->zipArchive->open($archiveName) === TRUE) {
120
            foreach ($fileNames as $fileNmae => $filePath) {
121
                if (!$this->zipArchive->addFile($filePath, $fileNmae)) {
122
                    return false;
123
                }
124
            }
125
            return true;
126
        } else {
127
            return false;
128
        }
129
    }
130
131
    /**
132
     * @inheritDoc
133
     */
134
    public function deleteMultipleFilesUsingName(string $archiveName, array $fileNames): bool
135
    {
136
        if ($this->zipArchive->open($archiveName) === TRUE) {
137
            foreach ($fileNames as $fileOrDirName => $filePath) {
138
                $this->zipArchive->deleteName($fileOrDirName);
139
            }
140
            return true;
141
        } else {
142
            return false;
143
        }
144
    }
145
}
146