PclZipAdapter::open()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 3
    public function open($filename)
19
    {
20 3
        $this->oPclZip = new PclZip($filename);
21 3
        $this->tmpDir = sys_get_temp_dir();
22 3
        return $this;
23
    }
24
25 2
    public function close()
26
    {
27 2
        return $this;
28
    }
29
30 1
    public function addFromString($localname, $contents)
31
    {
32 1
        $pathData = pathinfo($localname);
33
34 1
        $hFile = fopen($this->tmpDir.'/'.$pathData['basename'], "wb");
35 1
        fwrite($hFile, $contents);
36 1
        fclose($hFile);
37
38 1
        $res = $this->oPclZip->add($this->tmpDir.'/'.$pathData['basename'], PCLZIP_OPT_REMOVE_PATH, $this->tmpDir, PCLZIP_OPT_ADD_PATH, $pathData['dirname']);
39 1
        if ($res == 0) {
40
            throw new \Exception("Error zipping files : " . $this->oPclZip->errorInfo(true));
41
        }
42 1
        unlink($this->tmpDir.'/'.$pathData['basename']);
43 1
        return $this;
44
    }
45
}
46