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

ZipArchiveAdapter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 68.75%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 43
ccs 11
cts 16
cp 0.6875
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A open() 0 13 3
A close() 0 7 2
A addFromString() 0 8 2
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