Completed
Push — master ( a57b2e...8b5ec4 )
by Brent
11:54
created

PersonalDataExportController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\PersonalDataExport\Http\Controllers;
4
5
use Illuminate\Routing\Controller;
6
use Illuminate\Support\Facades\Storage;
7
use Spatie\PersonalDataExport\Http\Middleware\EnsureAuthorizedToDownload;
8
use Spatie\PersonalDataExport\Http\Middleware\FiresPersonalDataExportDownloadedEvent;
9
use Symfony\Component\HttpFoundation\StreamedResponse;
10
11
class PersonalDataExportController extends Controller
12
{
13
    public function __construct()
14
    {
15
        $this->middleware(EnsureAuthorizedToDownload::class);
16
        $this->middleware(FiresPersonalDataExportDownloadedEvent::class);
17
    }
18
19
    public function __invoke(string $zipFilename): StreamedResponse
20
    {
21
        return $this->responseStream($zipFilename);
22
    }
23
24
    protected function responseStream(string $filename): StreamedResponse
25
    {
26
        $disk = Storage::disk(config('personal-data-export.disk'));
27
28
        if (! $disk->exists($filename)) {
29
            abort(404);
30
        }
31
32
        $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...
33
            ? auth()->user()->personalDataExportName()
34
            : $filename;
35
36
        $downloadHeaders = [
37
            'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
38
            'Content-Type' => 'application/zip',
39
            'Content-Length' => $disk->size($filename),
40
            'Content-Disposition' => 'attachment; filename="' . $downloadFilename . '"',
41
            'Pragma' => 'public',
42
        ];
43
44
        return response()->stream(function () use ($filename, $disk) {
0 ignored issues
show
Bug introduced by
The method stream does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

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...
45
            $stream = $disk->readStream($filename);
46
47
            fpassthru($stream);
48
49
            if (is_resource($stream)) {
50
                fclose($stream);
51
            }
52
        }, 200, $downloadHeaders);
53
    }
54
}
55