Completed
Push — develop ( c9be70...5aaa2f )
by Franck
9s
created

ZipArchiveAdapter::addFromString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2.0625
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 3
    public function open($filename)
20
    {
21 3
        $this->filename = $filename;
22 3
        $this->oZipArchive = new ZipArchive();
23
24 3
        if ($this->oZipArchive->open($this->filename, ZipArchive::OVERWRITE) === true) {
25 3
            return $this;
26
        }
27
        if ($this->oZipArchive->open($this->filename, ZipArchive::CREATE) === true) {
28
            return $this;
29
        }
30
        throw new \Exception("Could not open $this->filename for writing.");
31
    }
32
33 2
    public function close()
34
    {
35 2
        if ($this->oZipArchive->close() === false) {
36
            throw new \Exception("Could not close zip file $this->filename.");
37
        }
38 2
        return $this;
39
    }
40
41 1
    public function addFromString($localname, $contents)
42
    {
43 1
        if ($this->oZipArchive->addFromString($localname, $contents) === false) {
44
            throw new \Exception("Error zipping files : " . $localname);
45
        }
46
47 1
        return $this;
48
    }
49
}
50