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

Zip::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
    /**
26
     * @var ZipArchive
27
     */
28
    private $zipArchive;
29
30
    /**
31
     * @var string
32
     */
33
    private $archiveName;
34
35
    /**
36
     * Zip constructor
37
     */
38
    public function __construct(string $archiveName)
39
    {
40
        $this->zipArchive = new ZipArchive();
41
        $this->archiveName = $archiveName;
42
    }
43
44
    public function __destruct()
45
    {
46
        if ($this->zipArchive->filename) {
47
            $this->zipArchive->close();
48
        }
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54
    public function addEmptyDir(string $newDirectory): bool
55
    {
56
        if ($this->zipArchive->open($this->archiveName, ZipArchive::CREATE) === TRUE) {
57
            $newDirectory = rtrim($newDirectory, '/') . '/';
58
            if ($this->zipArchive->locateName($newDirectory) !== false) {
59
                return true;
60
            };
61
            return $this->zipArchive->addEmptyDir($newDirectory);
62
        } else {
63
            return false;
64
        }
65
    }
66
67
    /**
68
     * @inheritDoc
69
     */
70
    public function addFile(string $filePath, string $newFileName = ''): bool
71
    {
72
        if ($this->zipArchive->open($this->archiveName, ZipArchive::CREATE) === TRUE) {
73
            return $this->zipArchive->addFile($filePath, $newFileName);
74
        } else {
75
            return false;
76
        }
77
    }
78
79
    /**
80
     * @inheritDoc
81
     */
82
    public function addFromString(string $newFileName, string $newFileContent): bool
83
    {
84
        if ($this->zipArchive->open($this->archiveName, ZipArchive::CREATE) === TRUE) {
85
            return $this->zipArchive->addFromString($newFileName, $newFileContent);
86
        } else {
87
            return false;
88
        }
89
    }
90
91
    /**
92
     * @inheritDoc
93
     */
94
    public function deleteUsingName(string $fileOrDirName): bool
95
    {
96
        if (
97
            $this->zipArchive->open($this->archiveName) === TRUE
98
            && $this->zipArchive->locateName($fileOrDirName) !== false
99
        ) {
100
            return $this->zipArchive->deleteName($fileOrDirName);
101
        } else {
102
            return false;
103
        }
104
    }
105
106
    /**
107
     * @inheritDoc
108
     */
109
    public function extractTo(string $pathToExtract, $files = ''): bool
110
    {
111
        if ($this->zipArchive->open($this->archiveName) === TRUE) {
112
            return $this->zipArchive->extractTo($pathToExtract);
113
        } else {
114
            return false;
115
        }
116
    }
117
118
    /**
119
     * @inheritDoc
120
     */
121
    public function count(): int
122
    {
123
        return $this->zipArchive->count();
124
    }
125
126
    /**
127
     * @inheritDoc
128
     */
129
    public function renameUsingName(string $currentName, string $newName): bool
130
    {
131
        if ($this->zipArchive->open($this->archiveName) === TRUE) {
132
            return $this->zipArchive->renameName($currentName, $newName);
133
        } else {
134
            return false;
135
        }
136
    }
137
138
    /**
139
     * @inheritDoc
140
     */
141
    public function addMultipleFiles(array $fileNames): bool
142
    {
143
        if ($this->zipArchive->open($this->archiveName, ZipArchive::CREATE) === TRUE) {
144
            foreach ($fileNames as $fileNmae => $filePath) {
145
                if (!$this->zipArchive->addFile($filePath, $fileNmae)) {
146
                    return false;
147
                }
148
            }
149
            return true;
150
        } else {
151
            return false;
152
        }
153
    }
154
155
    /**
156
     * @inheritDoc
157
     */
158
    public function deleteMultipleFilesUsingName(array $fileNames): bool
159
    {
160
        if ($this->zipArchive->open($this->archiveName) === TRUE) {
161
            foreach ($fileNames as $key => $fileOrDirName) {
162
                $this->zipArchive->deleteName($fileOrDirName);
163
            }
164
            return true;
165
        } else {
166
            return false;
167
        }
168
    }
169
}
170