Test Setup Failed
Push — master ( 7f1ec0...39e837 )
by Phan
04:24
created

SongZipArchive::__construct()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 5
nop 1
1
<?php
2
3
namespace App\Models;
4
5
use App\Facades\Download;
6
use Exception;
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Facades\Log;
9
use ZipArchive;
10
11
class SongZipArchive
12
{
13
    /**
14
     * @var ZipArchive
15
     */
16
    protected $archive;
17
18
    /**
19
     * Path to the zip archive.
20
     *
21
     * @var string
22
     */
23
    protected $path;
24
25
    /**
26
     * Names of the files in the archive
27
     * Format: [file-name.mp3' => currentFileIndex].
28
     *
29
     * @var array
30
     */
31
    protected $fileNames = [];
32
33
    /**
34
     * @param string $path
35
     *
36
     * @throws Exception
37
     */
38
    public function __construct($path = '')
39
    {
40
        if (!class_exists('ZipArchive')) {
41
            throw new Exception('Downloading multiple files requires ZipArchive module.');
42
        }
43
44
        // We use system's temp dir instead of storage_path() here, so that the generated files
45
        // can be cleaned up automatically after server reboot.
46
        $this->path = $path ?: $path = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'koel-download-'.uniqid().'.zip';
47
48
        $this->archive = new ZipArchive();
49
50
        if ($this->archive->open($this->path, ZipArchive::CREATE) !== true) {
51
            throw new Exception('Cannot create zip file.');
52
        }
53
    }
54
55
    /**
56
     * Add multiple songs into the archive.
57
     *
58
     * @param Collection $songs
59
     *
60
     * @return $this
61
     */
62
    public function addSongs(Collection $songs)
63
    {
64
        $songs->each(function ($song) {
65
            $this->addSong($song);
66
        });
67
68
        return $this;
69
    }
70
71
    /**
72
     * Add a single song into the archive.
73
     *
74
     * @param Song $song
75
     *
76
     * @return $this
77
     */
78
    public function addSong(Song $song)
79
    {
80
        try {
81
            $path = Download::fromSong($song);
82
83
            // We add all files into the zip archive as a flat structure.
84
            // As a result, there can be duplicate file names.
85
            // The following several lines are to make sure each file name is unique.
86
            $name = basename($path);
87
            if (array_key_exists($name, $this->fileNames)) {
88
                ++$this->fileNames[$name];
89
                $parts = explode('.', $name);
90
                $ext = $parts[count($parts) - 1];
91
                $parts[count($parts) - 1] = $this->fileNames[$name].".$ext";
92
                $name = implode('.', $parts);
93
            } else {
94
                $this->fileNames[$name] = 1;
95
            }
96
97
            $this->archive->addFile($path, $name);
98
        } catch (Exception $e) {
99
            Log::error($e);
100
        }
101
102
        return $this;
103
    }
104
105
    /**
106
     * Finish (close) the archive.
107
     *
108
     * @return $this
109
     */
110
    public function finish()
111
    {
112
        $this->archive->close();
113
114
        return $this;
115
    }
116
117
    /**
118
     * Get the path to the archive.
119
     *
120
     * @return string
121
     */
122
    public function getPath()
123
    {
124
        return $this->path;
125
    }
126
127
    /**
128
     * @return ZipArchive
129
     */
130
    public function getArchive()
131
    {
132
        return $this->archive;
133
    }
134
}
135