PersonalDataDownloadController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 2
dl 0
loc 59
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 6 1
A ensureAuthorizedToDownload() 0 16 4
A responseStream() 0 32 5
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
        if ($user = 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...
44
            if (method_exists($user, 'getPersonalDataDownloadName')) {
45
                $filename = $user->getPersonalDataDownloadName($filename);
46
            }
47
        }
48
49
        $downloadHeaders = [
50
            'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
51
            'Content-Type' => 'application/zip',
52
            'Content-Length' => $disk->size($filename),
53
            'Content-Disposition' => 'attachment; filename="'.$filename.'"',
54
            'Pragma' => 'public',
55
        ];
56
57
        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...
58
            $stream = $disk->readStream($filename);
59
60
            fpassthru($stream);
61
62
            if (is_resource($stream)) {
63
                fclose($stream);
64
            }
65
        }, 200, $downloadHeaders);
66
    }
67
}
68