Completed
Pull Request — master (#161)
by Sebastian
03:49
created

Zip   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 97
rs 10
wmc 11
lcom 1
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 8 1
A __construct() 0 8 1
A getPath() 0 4 1
A getSize() 0 4 1
A open() 0 4 1
A close() 0 4 1
A add() 0 21 4
A count() 0 4 1
1
<?php
2
3
namespace Spatie\Backup\Tasks\Backup;
4
5
use ZipArchive;
6
7
class Zip
8
{
9
    /** @var \ZipArchive */
10
    protected $zipFile;
11
12
    /** @var int */
13
    protected $fileCount = 0;
14
15
    /** @var string */
16
    protected $pathToZip;
17
18
    /**
19
     * @param string       $pathToZip
20
     * @param string|array $files
21
     *
22
     * @return \Spatie\Backup\Tasks\Backup\Zip
23
     */
24
    public static function create($pathToZip, $files = [])
25
    {
26
        $zip = new static($pathToZip);
27
28
        $zip->add($files);
29
30
        return $zip;
31
    }
32
33
    /**
34
     * @param string $pathToZip
35
     */
36
    public function __construct($pathToZip)
37
    {
38
        $this->zipFile = new ZipArchive();
39
40
        $this->pathToZip = $pathToZip;
41
42
        $this->open($pathToZip);
0 ignored issues
show
Unused Code introduced by
The call to Zip::open() has too many arguments starting with $pathToZip.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function getPath()
49
    {
50
        return $this->pathToZip;
51
    }
52
53
    /**
54
     * @return int
55
     */
56
    public function getSize()
57
    {
58
        return filesize($this->pathToZip);
59
    }
60
61
    protected function open()
62
    {
63
        $this->zipFile->open($this->pathToZip, ZipArchive::CREATE);
64
    }
65
66
    protected function close()
67
    {
68
        $this->zipFile->close();
69
    }
70
71
    /**
72
     * @param string|array $files
73
     * @param string       $nameInZip
74
     *
75
     * @return \Spatie\Backup\Tasks\Backup\Zip
76
     */
77
    public function add($files, $nameInZip = null)
78
    {
79
        if (is_array($files)) {
80
            $nameInZip = null;
81
        }
82
83
        if (is_string($files)) {
84
            $files = [$files];
85
        }
86
87
        $this->open();
88
89
        foreach ($files as $file) {
90
            $this->zipFile->addFile($file, $nameInZip);
91
            $this->fileCount++;
92
        }
93
94
        $this->close();
95
96
        return $this;
97
    }
98
99
    public function count()
100
    {
101
        return $this->fileCount;
102
    }
103
}
104