Passed
Push — master ( f82ba1...247912 )
by Ben
08:55
created

ZipAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 9
dl 0
loc 25
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 18 2
A __construct() 0 2 1
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