Passed
Push — master ( 19fdc3...a3afa9 )
by Ben
07:24
created

ZipAction::handle()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 18
rs 10
cc 2
nc 1
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Thinktomorrow\Chief\MediaGallery\Application;
5
6
use ZipStream\ZipStream;
7
use Illuminate\Http\Request;
8
use ZipStream\Option\Archive;
9
use Thinktomorrow\AssetLibrary\Asset;
10
11
final class ZipAction
12
{
13
    public function __construct()
14
    {
15
16
    }
17
18
    public function handle(string $filename, Request $request)
19
    {
20
        $assets = Asset::whereIn('id', (array) $request->input('asset_ids', []))->get();
21
22
        // enable output of HTTP headers TODO: this should be moved to the controller instead...
23
        $options = new Archive();
24
        $options->setSendHttpHeaders(true);
25
26
        $zip = new ZipStream($filename, $options);
27
28
        $assets->each(function(Asset $asset) use($zip){
29
            if(file_exists($asset->getFirstMediaPath())) {
30
                $zip->addFileFromPath($asset->filename(), $asset->getFirstMediaPath());
31
            }
32
        });
33
34
        // Output the zip as download
35
        $zip->finish();
36
    }
37
}
38