Completed
Push — develop ( 522e3f...10c5c8 )
by Franck
04:13
created

PclZipAdapter::close()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace PhpOffice\Common\Adapter\Zip;
3
4
use PclZip;
5
6
class PclZipAdapter implements ZipInterface
7
{
8
    /**
9
     * @var PclZip
10
     */
11
    protected $oPclZip;
12
13
    /**
14
     * @var string
15
     */
16
    protected $tmpDir;
17
18 2
    public function open($filename)
19
    {
20 2
        $this->oPclZip = new PclZip($filename);
21 2
        $this->tmpDir = sys_get_temp_dir();
22 2
        return $this;
23
    }
24
25 1
    public function close()
26
    {
27 1
        return $this;
28
    }
29
30
    public function addFromString($localname, $contents)
31
    {
32
        $pathData = pathinfo($localname);
33
34
        $hFile = fopen($this->tmpDir.'/'.$pathData['basename'], "wb");
35
        fwrite($hFile, $contents);
36
        fclose($hFile);
37
38
        $res = $this->oPclZip->add($this->tmpDir.'/'.$pathData['basename'], PCLZIP_OPT_REMOVE_PATH, $this->tmpDir, PCLZIP_OPT_ADD_PATH, $pathData['dirname']);
39
        if ($res == 0) {
40
            throw new \Exception("Error zipping files : " . $this->oPclZip->errorInfo(true));
41
        }
42
        unlink($this->tmpDir.'/'.$pathData['basename']);
43
        return $this;
44
    }
45
}
46