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

PclZipAdapter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 37.5%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 40
ccs 6
cts 16
cp 0.375
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 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