Passed
Push — develop ( 493714...dd5809 )
by nguereza
01:37
created

Zip::archiveFolder()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 26
rs 9.6666
cc 3
nc 3
nop 2
1
<?php
2
3
/**
4
 * Platine Docx template
5
 *
6
 * Platine Docx template is the lightweight library to manipulate the content of .docx files
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Docx template
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file Zip.php
33
 *
34
 *  The Zip Archive manipulation class
35
 *
36
 *  @package    Platine\DocxTemplate\Archive
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   http://www.iacademy.cf
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
45
declare(strict_types=1);
46
47
namespace Platine\DocxTemplate\Archive;
48
49
use FilesystemIterator;
50
use Platine\DocxTemplate\DocxExtractorInterface;
51
use Platine\DocxTemplate\Exception\DocxArchiveException;
52
use RecursiveDirectoryIterator;
53
use RecursiveIteratorIterator;
54
use ZipArchive;
55
56
/**
57
 * @class Zip
58
 * @package Platine\DocxTemplate\Archive
59
 */
60
class Zip implements DocxExtractorInterface
61
{
62
    /**
63
     * The zip archive instance
64
     * @var ZipArchive
65
     */
66
    protected ZipArchive $zip;
67
68
    /**
69
     * Create new instance
70
     * @param ZipArchive|null $zip
71
     */
72
    public function __construct(?ZipArchive $zip = null)
73
    {
74
        $this->zip = $zip ?? new ZipArchive();
75
    }
76
77
    /**
78
     * {@inheritodc}
79
     */
80
    public function archiveFolder(string $folder, string $filename): void
81
    {
82
        $create = $this->zip->open(
83
            $filename,
84
            ZipArchive::CREATE | ZipArchive::OVERWRITE
85
        );
86
        if ($create === true) {
87
             // Create recursive directory iterator
88
            $files = new RecursiveIteratorIterator(
89
                new RecursiveDirectoryIterator(
90
                    $folder,
91
                    FilesystemIterator::SKIP_DOTS
92
                ),
93
                RecursiveIteratorIterator::LEAVES_ONLY
94
            );
95
96
            foreach ($files as $name => $file) {
97
                $name = substr($name, strlen($folder . '/'));
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type null and true; however, parameter $string of substr() 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

97
                $name = substr(/** @scrutinizer ignore-type */ $name, strlen($folder . '/'));
Loading history...
98
                $this->zip->addFile($file->getRealPath(), $name);
99
            }
100
            $this->zip->close();
101
        } else {
102
            throw new DocxArchiveException(sprintf(
103
                'Can not open file [%s], error code [%s]',
104
                $filename,
105
                $create
106
            ));
107
        }
108
    }
109
110
    /**
111
     * {@inheritodc}
112
     */
113
    public function extract(string $archive, string $destination = '.'): void
114
    {
115
        $open = $this->zip->open($archive);
116
        if ($open === true) {
117
            if (!$this->zip->extractTo($destination)) {
118
                throw new DocxArchiveException(sprintf(
119
                    'Can not extract file [%s]',
120
                    $archive
121
                ));
122
            }
123
        } else {
124
            throw new DocxArchiveException(sprintf(
125
                'Can not open file [%s], error code [%s]',
126
                $archive,
127
                $open
128
            ));
129
        }
130
    }
131
132
    /**
133
     * {@inheritodc}
134
     */
135
    public function totalFiles(): int
136
    {
137
        return $this->zip->numFiles;
138
    }
139
140
    /**
141
     * {@inheritodc}
142
     */
143
    public function close(): void
144
    {
145
        $this->zip->close();
146
    }
147
148
    /**
149
     * {@inheritodc}
150
     */
151
    public function getInfo(int $index): ArchiveInfo
152
    {
153
        $stat = $this->zip->statIndex($index);
154
        if ($stat === false) {
155
            throw new DocxArchiveException(sprintf('Can not stat the archive file at index [%d]', $index));
156
        }
157
158
        return new ArchiveInfo(
159
            $stat['name'],
160
            $stat['index'],
161
            $stat['size'],
162
            $stat['mtime'],
163
            $stat['crc']
164
        );
165
    }
166
}
167