CanHandleActionMiddleware::getRouteParameter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use App\Http\Requests\Request;
6
use App\Lot;
7
use Auth;
8
9
class CanHandleActionMiddleware
10
{
11
    /**
12
     * @var Request
13
     */
14
    private $request;
15
16
    /**
17
     * Handle an incoming request.
18
     *
19
     * @param  \Illuminate\Http\Request $request
20
     * @param  \Closure $next
21
     * @return mixed
22
     */
23
    public function handle($request, \Closure $next, $parameter)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
24
    {
25
        $this->request = $request;
0 ignored issues
show
Documentation Bug introduced by
$request is of type object<Illuminate\Http\Request>, but the property $request was declared to be of type object<App\Http\Requests\Request>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
26
27
        $user_holder = $this->getObjectHolder(
28
            $this->getRouteParameter($parameter)
29
        );
30
31
        if ($user_holder && Auth::id() == $user_holder->id)
32
            return $next($request);
33
34
        $this->abort();
35
    }
36
37
    /**
38
     * Get model from parameter from route.
39
     *
40
     * @param $parameter
41
     * @return \Illuminate\Routing\Route|object|string
42
     */
43
    private function getRouteParameter($parameter)
44
    {
45
        return $this->request->route($parameter);
46
    }
47
48
    /**
49
     * Get user of this object.
50
     *
51
     * @param $model
52
     * @return mixed
53
     */
54
    private function getObjectHolder($model)
55
    {
56
        if($model && $model instanceof Lot)
57
            return $model->vendor->user;
0 ignored issues
show
Documentation introduced by
The property vendor does not exist on object<App\Lot>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
58
59
        if($model)
60
            return $model->user()->first();
61
62
        $this->abort();
63
    }
64
65
    /**
66
     * Abort connection.
67
     */
68
    private function abort()
69
    {
70
        return abort('404');
71
    }
72
}