Completed
Push — master ( dfc3e0...e152d1 )
by Freek
01:40
created

ensureAuthorizedToDownload()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 4
nc 5
nop 1
1
<?php
2
3
namespace Spatie\PersonalDataDownload\Http\Controllers;
4
5
use Illuminate\Support\Facades\Storage;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\HttpFoundation\StreamedResponse;
8
9
class PersonalDataDownloadController
10
{
11
    public function __invoke(string $zipFilename): StreamedResponse
12
    {
13
        $this->ensureAuthorizedToDownload($zipFilename);
14
15
        return $this->responseStream($zipFilename);
16
    }
17
18
    protected function ensureAuthorizedToDownload(string $zipFilename)
19
    {
20
        if (! config('personal-data-download.authentication_required')) {
21
            return;
22
        }
23
24
        if (! auth()->check()) {
0 ignored issues
show
Bug introduced by
The method check 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...
25
            abort(Response::HTTP_FORBIDDEN);
26
        }
27
28
        [$owningUserId] = explode('-', $zipFilename);
0 ignored issues
show
Bug introduced by
The variable $owningUserId does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
29
30
        if ($owningUserId != auth()->user()->id) {
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...
31
            abort(Response::HTTP_UNAUTHORIZED);
32
        }
33
    }
34
35
    protected function responseStream(string $filename): StreamedResponse
36
    {
37
        $disk = Storage::disk(config('personal-data-download.disk'));
38
39
        if (! $disk->exists($filename)) {
40
            abort(404);
41
        }
42
43
        $downloadHeaders = [
44
            'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
45
            'Content-Type' => 'application/zip',
46
            'Content-Length' => $disk->size($filename),
47
            'Content-Disposition' => 'attachment; filename="'.$filename.'"',
48
            'Pragma' => 'public',
49
        ];
50
51
        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...
52
            $stream = $disk->readStream($filename);
53
54
            fpassthru($stream);
55
56
            if (is_resource($stream)) {
57
                fclose($stream);
58
            }
59
        }, 200, $downloadHeaders);
60
    }
61
}
62