Passed
Push — master ( 76ff82...59d73f )
by Sérgio Danilo
01:35
created

Zipper::setFileName()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 15
rs 9.6111
1
<?php
2
3
4
namespace SergioDaniloJr\Zipper;
5
6
7
/**
8
 * Class Zipper
9
 * @package SergioDaniloJr\Zipper
10
 */
11
class Zipper
12
{
13
    /**
14
     * @var \ZipArchive
15
     */
16
    private $zip;
17
18
    /**
19
     * @var string
20
     */
21
    private $file;
22
23
    /**
24
     * @var string
25
     */
26
    private $path;
27
28
    /**
29
     * @var string
30
     */
31
    private $message;
32
33
    /**
34
     *Default Extension
35
     */
36
    const DEFAULT_EXTENSION = "zip";
37
38
    /**
39
     * Zipper constructor.
40
     * @param string|null $pathSave
41
     */
42
    public function __construct(string $pathSave = null)
43
    {
44
        $this->zip = new \ZipArchive();
45
        $this->path = $pathSave;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function message(): string
52
    {
53
        return $this->message;
54
    }
55
56
    /**
57
     * @return mixed
58
     */
59
    public function file()
60
    {
61
        return $this->file;
62
    }
63
64
    /**
65
     * @param string $file
66
     * @return string|null
67
     */
68
    public function zipFile(string $file): ?string
69
    {
70
        if (!is_null($dataFile = $this->dataFile($file))) {
71
72
            $path = $this->destiny($file);
73
74
            if ($this->zip->open($path, $this->zip::CREATE) === true) {
75
                $bool = $this->zip->addFile($file, $dataFile->basename);
76
                $this->zip->close();
77
            }
78
79
            if ($bool) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $bool does not seem to be defined for all execution paths leading up to this point.
Loading history...
80
                $this->file = $path;
81
                return $this->file();
82
            }
83
        }
84
        return null;
85
    }
86
87
88
    /**
89
     * @param array $files
90
     * @param string $nameFile
91
     * @param string|null $path
92
     * @return string|null
93
     */
94
    public function zipFiles(array $files, string $nameFile, string $path = null): ?string
95
    {
96
        $path = (!is_null($this->path) ? $this->path : $path);
0 ignored issues
show
introduced by
The condition is_null($this->path) is always false.
Loading history...
97
        if (!$path) {
98
            $this->message = "Forneça um Caminho para Salvar o Arquivo!";
99
            return null;
100
        }
101
102
        $nameFile = $nameFile . "." . self::DEFAULT_EXTENSION;
103
        $pathSave = $this->checkDir($path) . "/" . $nameFile;
104
105
        if (!empty($files) && is_array($files) && $this->zip->open($pathSave, $this->zip::CREATE) === true) {
106
            foreach ($files as $file) {
107
                if ($dataFile = $this->dataFile($file)) {
108
                    $this->zip->addFile($file, $dataFile->basename);
109
                } else {
110
                    return null;
111
                }
112
            }
113
            $this->zip->close();
114
        }
115
116
        $this->file = $pathSave;
117
        return $this->file();
118
    }
119
120
    /**
121
     * @param string $file
122
     * @param string|null $destiny
123
     * @return string|null
124
     */
125
    public function extract(string $file, string $destiny = null): ?string
126
    {
127
        if (!is_null($dataFile = $this->dataFile($file))) {
128
129
            $destiny = (!is_null($destiny) ? $destiny : $dataFile->dirname);
130
131
            if ($this->zip->open($file) === TRUE) {
132
                $this->zip->extractTo($destiny);
133
                $this->zip->close();
134
            }
135
            $saved = $destiny . "/" . $dataFile->basename;
136
            return $this->checkDir($saved);
137
        }
138
139
        return null;
140
    }
141
142
    /**
143
     * @param $path
144
     * @return bool
145
     */
146
    public function download($path): bool
147
    {
148
        $fileZip = $this->zipFile($path);
149
        if (file_exists($fileZip) && !is_dir($fileZip)) {
150
            if (!is_null($dataFile = $this->dataFile($fileZip))) {
0 ignored issues
show
Bug introduced by
It seems like $fileZip can also be of type null; however, parameter $path of SergioDaniloJr\Zipper\Zipper::dataFile() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

150
            if (!is_null($dataFile = $this->dataFile(/** @scrutinizer ignore-type */ $fileZip))) {
Loading history...
151
                header("Content-Type: application/zip");
152
                header("Content-Lenght: " . filesize($fileZip));
153
                header("Content-Disposition: attachment; filename=" . $dataFile->filename);
154
                readfile($fileZip);
155
                unlink($fileZip);
156
                return true;
157
            }
158
        }
159
        return false;
160
    }
161
162
163
    /**
164
     * @param string $path
165
     * @return object|null
166
     */
167
    private function dataFile(string $path): ?object
168
    {
169
        if (!is_file($path)) {
170
            $this->message = "Forneça um Arquivo Válido!";
171
            return null;
172
        } else {
173
            $pathinfo = pathinfo($path);
174
            $dataFile = new \stdClass();
175
            $dataFile->dirname = $pathinfo["dirname"];
176
            $dataFile->basename = $pathinfo["basename"];
177
            $dataFile->extension = $pathinfo["extension"];
178
            $dataFile->filename = $pathinfo["filename"];
179
180
            return $dataFile;
181
        }
182
    }
183
184
185
    /**
186
     * @param string $file
187
     * @return string
188
     */
189
    private function destiny(string $file): string
190
    {
191
        if (!is_null($dataFile = $this->dataFile($file)) && !is_null($this->path)) {
192
            $pathSave = $this->checkDir($this->path) . "/" . $this->setFileName($file) . "." . self::DEFAULT_EXTENSION;
193
        } else {
194
            $pathSave = $dataFile->dirname . "/" . $this->setFileName($file) . "." . self::DEFAULT_EXTENSION;
195
        }
196
        $this->path = $pathSave;
197
        return $this->path;
198
    }
199
200
    /**
201
     * @param $file
202
     * @return string
203
     */
204
    public function setFileName($file): string
205
    {
206
        $dataFile = $this->dataFile($file);
207
        $path = (!empty($this->path) ? $this->path : $dataFile->dirname);
208
209
        $directory = scandir($path);
210
        $filename = $this->dataFile($file)->filename;
211
        if (!empty($directory)) {
212
            foreach ($directory as $item) {
213
                if (pathinfo($item)["filename"] == pathinfo($file)["filename"]) {
214
                    $filename = $this->dataFile($file)->filename . "-" . (string)time();
215
                }
216
            }
217
        }
218
        return $filename;
219
    }
220
221
    /**
222
     * @param string $path
223
     * @return string|null
224
     */
225
    private function checkDir(string $path): ?string
226
    {
227
        if (is_dir($path)) {
228
            $this->path = $path;
229
            return $this->path;
230
        }
231
        //Caso a Pasta não exista, tem que criar um método para criar a pasta solicitada
232
        return $this->setDir($path);
233
    }
234
235
236
    /**
237
     * @param string $path
238
     * @return string
239
     */
240
    private function setDir(string $path): string
241
    {
242
        if (!is_dir($path) && !is_file($path)) {
243
            mkdir($path, 777, true);
244
        }
245
        $this->path = $path;
246
        return $this->path;
247
    }
248
}