Completed
Push — master ( 326b3e...061c53 )
by Freek
09:13
created

Zip::open()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Backup\Tasks\Backup;
4
5
use Spatie\Backup\Helpers\Format;
6
use ZipArchive;
7
8
class Zip
9
{
10
    /** @var \ZipArchive */
11
    protected $zipFile;
12
13
    /** @var int */
14
    protected $fileCount = 0;
15
16
    /** @var string */
17
    protected $pathToZip;
18
19
    public static function createForManifest(Manifest $manifest, string $pathToZip): Zip
20
    {
21
        $zip = new static($pathToZip);
22
23
        foreach ($manifest->files() as $file) {
24
            $zip->add($file, self::determineNameOfFileInZip($file, $pathToZip));
25
        }
26
27
        return $zip;
28
    }
29
30
    protected static function determineNameOfFileInZip(string $pathToFile, string $pathToZip)
31
    {
32
        $zipDirectory = pathinfo($pathToZip, PATHINFO_DIRNAME);
33
34
        $fileDirectory = pathinfo($pathToFile, PATHINFO_DIRNAME);
35
36
        if (starts_with($fileDirectory, $zipDirectory)) {
37
            return str_replace($zipDirectory, '', $pathToFile);
38
        }
39
40
        return $pathToFile;
41
    }
42
43
    public function __construct(string $pathToZip)
44
    {
45
        $this->zipFile = new ZipArchive();
46
47
        $this->pathToZip = $pathToZip;
48
49
        $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...
50
    }
51
52
    public function path(): string
53
    {
54
        return $this->pathToZip;
55
    }
56
57
    public function size(): int
58
    {
59
        if ($this->fileCount === 0) {
60
            return 0;
61
        }
62
63
        return filesize($this->pathToZip);
64
    }
65
66
    public function humanReadableSize(): string
67
    {
68
        return Format::humanReadableSize($this->size());
69
    }
70
71
    protected function open()
72
    {
73
        $this->zipFile->open($this->pathToZip, ZipArchive::CREATE);
74
    }
75
76
    protected function close()
77
    {
78
        $this->zipFile->close();
79
    }
80
81
    /**
82
     * @param string|array $files
83
     * @param string $nameInZip
84
     *
85
     * @return \Spatie\Backup\Tasks\Backup\Zip
86
     */
87
    public function add($files, string $nameInZip = null): Zip
88
    {
89
        if (is_array($files)) {
90
            $nameInZip = null;
91
        }
92
93
        if (is_string($files)) {
94
            $files = [$files];
95
        }
96
97
        $this->open();
98
99
        foreach ($files as $file) {
100
            if (file_exists($file)) {
101
                $this->zipFile->addFile($file, $nameInZip).PHP_EOL;
102
            }
103
            $this->fileCount++;
104
        }
105
106
        $this->close();
107
108
        return $this;
109
    }
110
111
    public function count(): int
112
    {
113
        return $this->fileCount;
114
    }
115
}
116