Completed
Push — master ( 8b5ec4...bccd56 )
by Brent
11:18 queued 09:11
created

ZipDownloadResponse::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 9.44
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace Spatie\PersonalDataExport\Http\Responses;
4
5
use Illuminate\Http\Response;
6
use Illuminate\Support\Facades\Storage;
7
use Symfony\Component\HttpFoundation\StreamedResponse;
8
9
class ZipDownloadResponse extends StreamedResponse
10
{
11
    public function __construct(string $filename)
12
    {
13
        $disk = Storage::disk(config('personal-data-export.disk'));
14
15
        if (! $disk->exists($filename)) {
16
            abort(404);
17
        }
18
19
        $downloadFilename = auth()->user()
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
20
            ? auth()->user()->personalDataExportName()
21
            : $filename;
22
23
        $downloadHeaders = [
24
            'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
25
            'Content-Type' => 'application/zip',
26
            'Content-Length' => $disk->size($filename),
27
            'Content-Disposition' => 'attachment; filename="' . $downloadFilename . '"',
28
            'Pragma' => 'public',
29
        ];
30
31
        parent::__construct(function () use ($filename, $disk) {
32
            $stream = $disk->readStream($filename);
33
34
            fpassthru($stream);
35
36
            if (is_resource($stream)) {
37
                fclose($stream);
38
            }
39
        }, Response::HTTP_OK, $downloadHeaders);
40
    }
41
}
42