Test Failed
Push — master ( e18acf...2713a2 )
by Marcio
12:48
created

HZip::create()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 17
rs 9.6111
cc 5
nc 8
nop 3
1
<?php
2
/**
3
 * KNUT7 K7F (https://marciozebedeu.com/)
4
 * KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/).
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @see      https://github.com/knut7/framework/ for the canonical source repository
11
 *
12
 * @copyright (c) 2015.  KNUT7  Software Technologies AO Inc. (https://marciozebedeu.com/)
13
 * @license   https://marciozebedeu.com/license/new-bsd New BSD License
14
 * @author    Marcio Zebedeu - [email protected]
15
 *
16
 * @version   1.0.2
17
 */
18
19
namespace Ballybran\Helpers\Zip;
20
use Ballybran\Exception\Exception;
21
22
class HZip extends ZipStatus
23
{
24
    private $zip;
25
26
    public function __construct($objec = null)
27
    {
28
        if (is_object($objec)) {
29
            $this->zip = $objec;
30
        } else {
31
            $this->zip = new \ZipArchive();
32
        }
33
    }
34
35
    /**
36
     * _rglobRead.
37
     *
38
     * @param mixed $source
39
     * @param mixed $array
40
     */
41
    private function _rglobRead($source, &$array = array())
42
    {
43
        if (!$source || trim($source) == '') {
44
            $source = '.';
45
        }
46
47
        foreach ((array)glob($source . '/*/') as $key => $value) {
48
            $this->_rglobRead(str_replace('//', '/', $value), $array);
49
        }
50
51
        foreach ((array)glob($source . '*.*') as $key => $value) {
52
            $array[] = str_replace('//', '/', $value);
53
        }
54
    }
55
56
    /**
57
     * _zip.
58
     *
59
     * @param mixed $array
60
     * @param mixed $part
61
     * @param mixed $destination
62
     */
63
    private function _zip($array, $part, $destination)
64
    {
65
        @mkdir($destination, 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

65
        /** @scrutinizer ignore-unhandled */ @mkdir($destination, 0777, true);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
66
67
        if ($this->zip->open(str_replace('//', '/', "{$destination}/partz{$part}.zip"), \ZipArchive::CREATE)) {
68
            foreach ((array)$array as $key => $value) {
69
                $this->zip->addFile($value, str_replace(array('../', './'), null, $value));
70
            }
71
            $this->ZipStatusString($this->zip->status);
72
            $this->zip->close();
73
        }
74
    }
75
76
    /**
77
     * create.
78
     *
79
     * @param mixed $limit
80
     * @param mixed $source
81
     * @param mixed $destination
82
     */
83
    public function create($limit = 500, $source = null, $destination = './')
84
    {
85
        if (!$destination || trim($destination) == '') {
86
            $destination = './';
87
        }
88
89
        $this->_rglobRead($source, $input);
90
        $maxinput = count($input);
91
        $splitinto = (($maxinput / $limit) > round($maxinput / $limit, 0)) ? round($maxinput / $limit, 0) + 1 : round($maxinput / $limit, 0);
92
93
        for ($i = 0; $i < $splitinto; ++$i) {
94
            $this->_zip(array_slice($input, ($i * $limit), $limit, true), $i, $destination);
95
        }
96
97
        unset($input);
98
99
        return;
100
    }
101
102
    /**
103
     * unzip.
104
     *
105
     * @param mixed $source
106
     * @param mixed $destination
107
     */
108
    public function unzip($source, $destination)
109
    {
110
        @mkdir($destination, 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

110
        /** @scrutinizer ignore-unhandled */ @mkdir($destination, 0777, true);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
111
112
        foreach ((array)glob($source . '/*.zip') as $key => $value) {
113
            if ($this->zip->open(str_replace('//', '/', $value)) === true) {
114
                $this->zip->extractTo($destination);
115
                echo $this->ZipStatusString($this->zip);
116
                $this->zip->close();
117
            }
118
        }
119
    }
120
121
    /**
122
     * deleteZip.
123
     *
124
     * @param mixed $zipname
125
     */
126
    public function deleteZip($zipname)
127
    {
128
        if (!file_exists($zipname)) {
129
            try {
130
                throw new Exception('no file zip');
131
            } catch (\Exception $e) {
132
                $e->getMessage();
133
            }
134
        }
135
        @chmod($zipname, 0777);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for chmod(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

135
        /** @scrutinizer ignore-unhandled */ @chmod($zipname, 0777);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
136
137
        if (!$this->zip->open($zipname)) {
138
        }
139
140
        $loop = $this->zip->numFiles;
141
142
        if (!$loop) {
143
            try {
144
                throw new Exception('no file zip');
145
            } catch (\Exception $e) {
146
                $e->getMessage();
147
            }
148
        }
149
        for ($i = 0; $i < $loop; ++$i) {
150
            $this->zip->deleteIndex($i);
151
        }
152
    }
153
}
154