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

EnsureAuthorizedToDownload   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 23
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 20 4
1
<?php
2
3
namespace Spatie\PersonalDataExport\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\Response;
8
9
class EnsureAuthorizedToDownload
10
{
11
    public function handle(Request $request, Closure $next)
12
    {
13
        if (! config('personal-data-export.authentication_required')) {
14
            return $next($request);
15
        }
16
17
        if (! 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...
18
            abort(Response::HTTP_FORBIDDEN);
19
        }
20
21
        $zipFilename = $request->route('zipFilename');
22
23
        [$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...
24
25
        if ($owningUserId != auth()->user()->id) {
26
            abort(Response::HTTP_UNAUTHORIZED);
27
        }
28
29
        return $next($request);
30
    }
31
}
32