Completed
Push — master ( 490194...2ac138 )
by Freek
01:25
created

Zip::add()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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