Completed
Push — develop ( 72e4e7...fc5243 )
by Franck
28:42 queued 10:24
created

PclZipAdapter::addFromString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
rs 9.4285
cc 2
eloc 10
nc 2
nop 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
    /**
19
     * @param $filename
20
     * @return $this
21
     */
22
    public function open($filename)
23
    {
24
        $this->oPclZip = new PclZip($filename);
25
        $this->tmpDir = sys_get_temp_dir();
26
        return $this;
27
    }
28
29
    /**
30
     * @return $this
31
     */
32
    public function close()
33
    {
34
        return $this;
35
    }
36
37
    /**
38
     * @param $localname
39
     * @param $contents
40
     * @return $this
41
     * @throws \Exception
42
     */
43
    public function addFromString($localname, $contents)
44
    {
45
        $pathData = pathinfo($localname);
46
47
        $hFile = fopen($this->tmpDir.'/'.$pathData['basename'], "wb");
48
        fwrite($hFile, $contents);
49
        fclose($hFile);
50
51
        $res = $this->oPclZip->add($this->tmpDir.'/'.$pathData['basename'], PCLZIP_OPT_REMOVE_PATH, $this->tmpDir, PCLZIP_OPT_ADD_PATH, $pathData['dirname']);
52
        if ($res == 0) {
53
            throw new \Exception("Error zipping files : " . $this->oPclZip->errorInfo(true));
54
        }
55
        unlink($this->tmpDir.'/'.$pathData['basename']);
56
        return $this;
57
    }
58
}
59