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

Zip::addEmptyDir()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
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
    /**
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 offsetExists(string $fileOrDirName): bool
55
    {
56
        
57
        if($this->zipArchive->open($this->archiveName, ZipArchive::CREATE) === TRUE) {
58
            if (strpos($fileOrDirName, '.') === false) {
59
                $fileOrDirName = rtrim($fileOrDirName, '/') . '/';
60
            }
61
            return $this->zipArchive->locateName($fileOrDirName) !== false;
62
        } 
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 57 is false. This is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function addEmptyDir(string $newDirectory): bool
69
    {
70
        if ($this->zipArchive->open($this->archiveName, ZipArchive::CREATE) === TRUE) {
71
            if (!$this->offsetExists($newDirectory)) {
72
                return $this->zipArchive->addEmptyDir($newDirectory);
73
            } else {
74
                return false;
75
            }
76
        } else {
77
            return false;
78
        }
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84
    public function addFile(string $filePath, string $newFileName = ''): bool
85
    {
86
        if ($this->zipArchive->open($this->archiveName, ZipArchive::CREATE) === TRUE) {
87
            return $this->zipArchive->addFile($filePath, $newFileName);
88
        } else {
89
            return false;
90
        }
91
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96
    public function addFromString(string $newFileName, string $newFileContent): bool
97
    {
98
        if ($this->zipArchive->open($this->archiveName, ZipArchive::CREATE) === TRUE) {
99
            return $this->zipArchive->addFromString($newFileName, $newFileContent);
100
        } else {
101
            return false;
102
        }
103
    }
104
105
    /**
106
     * @inheritDoc
107
     */
108
    public function deleteUsingName(string $fileOrDirName): bool
109
    {
110
        if (
111
            $this->zipArchive->open($this->archiveName) === TRUE
112
            && $this->offsetExists($fileOrDirName)
113
        ) {
114
            return $this->zipArchive->deleteName($fileOrDirName);
115
        } else {
116
            return false;
117
        }
118
    }
119
120
    /**
121
     * @inheritDoc
122
     */
123
    public function extractTo(string $pathToExtract, $files = ''): bool
124
    {
125
        if ($this->zipArchive->open($this->archiveName) === TRUE) {
126
            return $this->zipArchive->extractTo($pathToExtract);
127
        } else {
128
            return false;
129
        }
130
    }
131
132
    /**
133
     * @inheritDoc
134
     */
135
    public function count(): int
136
    {
137
        return $this->zipArchive->count();
138
    }
139
140
    /**
141
     * @inheritDoc
142
     */
143
    public function addMultipleFiles(array $fileNames): bool
144
    {
145
        if ($this->zipArchive->open($this->archiveName, ZipArchive::CREATE) === TRUE) {
146
            foreach ($fileNames as $fileNmae => $filePath) {
147
                if (!$this->zipArchive->addFile($filePath, $fileNmae)) {
148
                    return false;
149
                }
150
            }
151
            return true;
152
        } else {
153
            return false;
154
        }
155
    }
156
157
    /**
158
     * @inheritDoc
159
     */
160
    public function deleteMultipleFilesUsingName(array $fileNames): bool
161
    {
162
        if ($this->zipArchive->open($this->archiveName) === TRUE) {
163
            foreach ($fileNames as $key => $fileOrDirName) {
164
                $this->zipArchive->deleteName($fileOrDirName);
165
            }
166
            return true;
167
        } else {
168
            return false;
169
        }
170
    }
171
}
172