Completed
Push — master ( 8ec39e...ad5327 )
by Arman
21s queued 14s
created

ZipAdapter::addFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
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\Adapters;
16
17
use Quantum\Libraries\Archive\ArchiveInterface;
18
use ZipArchive;
19
20
/**
21
 * Class ArchiveInterface
22
 * @package Quantum\Libraries\Archive\Adapters
23
 */
24
class ZipAdapter implements ArchiveInterface
25
{
26
    /**
27
     * @var ZipArchive
28
     */
29
    private $zipArchive;
30
31
    /**
32
     * @var string
33
     */
34
    private $archiveName;
35
36
    /**
37
     * Zip constructor
38
     */
39
    public function __construct(string $archiveName)
40
    {
41
        $this->zipArchive = new ZipArchive();
42
        $this->archiveName = $archiveName;
43
    }
44
45
    public function __destruct()
46
    {
47
        if ($this->zipArchive->filename) {
48
            $this->zipArchive->close();
49
        }
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public function offsetExists(string $fileOrDirName): bool
56
    {
57
        
58
        if($this->zipArchive->open($this->archiveName, ZipArchive::CREATE) === TRUE) {
59
            if (strpos($fileOrDirName, '.') === false) {
60
                $fileOrDirName = rtrim($fileOrDirName, '/') . '/';
61
            }
62
            return $this->zipArchive->locateName($fileOrDirName) !== false;
63
        } 
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 58 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...
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69
    public function addEmptyDir(string $newDirectory): bool
70
    {
71
        if ($this->zipArchive->open($this->archiveName, ZipArchive::CREATE) === TRUE) {
72
            if (!$this->offsetExists($newDirectory)) {
73
                return $this->zipArchive->addEmptyDir($newDirectory);
74
            } else {
75
                return false;
76
            }
77
        } else {
78
            return false;
79
        }
80
    }
81
82
    /**
83
     * @inheritDoc
84
     */
85
    public function addFile(string $filePath, string $newFileName = ''): bool
86
    {
87
        if ($this->zipArchive->open($this->archiveName, ZipArchive::CREATE) === TRUE) {
88
            return $this->zipArchive->addFile($filePath, $newFileName);
89
        } else {
90
            return false;
91
        }
92
    }
93
94
    /**
95
     * @inheritDoc
96
     */
97
    public function addFromString(string $newFileName, string $newFileContent): bool
98
    {
99
        if ($this->zipArchive->open($this->archiveName, ZipArchive::CREATE) === TRUE) {
100
            return $this->zipArchive->addFromString($newFileName, $newFileContent);
101
        } else {
102
            return false;
103
        }
104
    }
105
106
    /**
107
     * @inheritDoc
108
     */
109
    public function deleteUsingName(string $fileOrDirName): bool
110
    {
111
        if (
112
            $this->zipArchive->open($this->archiveName) === TRUE
113
            && $this->offsetExists($fileOrDirName)
114
        ) {
115
            return $this->zipArchive->deleteName($fileOrDirName);
116
        } else {
117
            return false;
118
        }
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124
    public function extractTo(string $pathToExtract, $files = ''): bool
125
    {
126
        if ($this->zipArchive->open($this->archiveName) === TRUE) {
127
            return $this->zipArchive->extractTo($pathToExtract);
128
        } else {
129
            return false;
130
        }
131
    }
132
133
    /**
134
     * @inheritDoc
135
     */
136
    public function count(): int
137
    {
138
        return $this->zipArchive->count();
139
    }
140
141
    /**
142
     * @inheritDoc
143
     */
144
    public function addMultipleFiles(array $fileNames): bool
145
    {
146
        if ($this->zipArchive->open($this->archiveName, ZipArchive::CREATE) === TRUE) {
147
            foreach ($fileNames as $fileNmae => $filePath) {
148
                if (!$this->zipArchive->addFile($filePath, $fileNmae)) {
149
                    return false;
150
                }
151
            }
152
            return true;
153
        } else {
154
            return false;
155
        }
156
    }
157
158
    /**
159
     * @inheritDoc
160
     */
161
    public function deleteMultipleFilesUsingName(array $fileNames): bool
162
    {
163
        if ($this->zipArchive->open($this->archiveName) === TRUE) {
164
            foreach ($fileNames as $key => $fileOrDirName) {
165
                $this->zipArchive->deleteName($fileOrDirName);
166
            }
167
            return true;
168
        } else {
169
            return false;
170
        }
171
    }
172
}
173