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

ZipArchiveAdapter::open()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 2
Metric Value
c 2
b 1
f 2
dl 0
loc 13
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
namespace PhpOffice\Common\Adapter\Zip;
4
5
use ZipArchive;
6
7
class ZipArchiveAdapter implements ZipInterface
8
{
9
    /**
10
     * @var ZipArchive
11
     */
12
    protected $oZipArchive;
13
14
    /**
15
     * @var string
16
     */
17
    protected $filename;
18
19
    /**
20
     * @param string $filename
21
     * @throws \Exception Could not open $this->filename for writing.
22
     * @return mixed
23
     */
24
    public function open($filename)
25
    {
26
        $this->filename = $filename;
27
        $this->oZipArchive = new ZipArchive();
28
29
        if ($this->oZipArchive->open($this->filename, ZipArchive::OVERWRITE) === true) {
30
            return $this;
31
        }
32
        if ($this->oZipArchive->open($this->filename, ZipArchive::CREATE) === true) {
33
            return $this;
34
        }
35
        throw new \Exception("Could not open $this->filename for writing.");
36
    }
37
38
    /**
39
     * @return $this
40
     * @throws \Exception Could not close zip file $this->filename.
41
     */
42
    public function close()
43
    {
44
        if ($this->oZipArchive->close() === false) {
45
            throw new \Exception("Could not close zip file $this->filename.");
46
        }
47
        return $this;
48
    }
49
50
    /**
51
     * @param $localname
52
     * @param $contents
53
     * @return bool
54
     */
55
    public function addFromString($localname, $contents)
56
    {
57
        return $this->oZipArchive->addFromString($localname, $contents);
58
    }
59
}
60