Completed
Push — develop ( 10c5c8...2af103 )
by Franck
11s
created

PclZipAdapter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 40
ccs 15
cts 16
cp 0.9375
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A open() 0 6 1
A close() 0 4 1
A addFromString() 0 15 2
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