Completed
Push — master ( f9866e...fa91dc )
by Song
02:51
created

src/Auth/Database/Permission.php (7 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Encore\Admin\Auth\Database;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Str;
9
10
class Permission extends Model
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $fillable = ['name', 'slug', 'http_method', 'http_path'];
16
17
    /**
18
     * @var array
19
     */
20
    public static $httpMethods = [
21
        'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD',
22
    ];
23
24
    /**
25
     * Create a new Eloquent model instance.
26
     *
27
     * @param array $attributes
28
     */
29 View Code Duplication
    public function __construct(array $attributes = [])
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
        $connection = config('admin.database.connection') ?: config('database.default');
32
33
        $this->setConnection($connection);
34
35
        $this->setTable(config('admin.database.permissions_table'));
36
37
        parent::__construct($attributes);
38
    }
39
40
    /**
41
     * Permission belongs to many roles.
42
     *
43
     * @return BelongsToMany
44
     */
45 View Code Duplication
    public function roles() : BelongsToMany
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
    {
47
        $pivotTable = config('admin.database.role_permissions_table');
48
49
        $relatedModel = config('admin.database.roles_model');
50
51
        return $this->belongsToMany($relatedModel, $pivotTable, 'permission_id', 'role_id');
52
    }
53
54
    /**
55
     * If request should pass through the current permission.
56
     *
57
     * @param Request $request
58
     *
59
     * @return bool
60
     */
61
    public function shouldPassThrough(Request $request) : bool
62
    {
63
        if (empty($this->http_method) && empty($this->http_path)) {
0 ignored issues
show
The property http_method does not exist on object<Encore\Admin\Auth\Database\Permission>. 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...
The property http_path does not exist on object<Encore\Admin\Auth\Database\Permission>. 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...
64
            return true;
65
        }
66
67
        $method = $this->http_method;
0 ignored issues
show
The property http_method does not exist on object<Encore\Admin\Auth\Database\Permission>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
68
69
        $matches = array_map(function ($path) use ($method) {
70
            $path = trim(config('admin.route.prefix'), '/').$path;
71
72
            if (Str::contains($path, ':')) {
73
                list($method, $path) = explode(':', $path);
74
                $method = explode(',', $method);
0 ignored issues
show
Consider using a different name than the imported variable $method, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
75
            }
76
77
            return compact('method', 'path');
78
        }, explode("\r\n", $this->http_path));
0 ignored issues
show
The property http_path does not exist on object<Encore\Admin\Auth\Database\Permission>. 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...
79
80
        foreach ($matches as $match) {
81
            if ($this->matchRequest($match, $request)) {
82
                return true;
83
            }
84
        }
85
86
        return false;
87
    }
88
89
    /**
90
     * If a request match the specific HTTP method and path.
91
     *
92
     * @param array   $match
93
     * @param Request $request
94
     *
95
     * @return bool
96
     */
97
    protected function matchRequest(array $match, Request $request) : bool
98
    {
99
        if (!$request->is(trim($match['path'], '/'))) {
100
            return false;
101
        }
102
103
        $method = collect($match['method'])->filter()->map(function ($method) {
104
            return strtoupper($method);
105
        });
106
107
        return $method->isEmpty() || $method->contains($request->method());
108
    }
109
110
    /**
111
     * @param $method
112
     */
113
    public function setHttpMethodAttribute($method)
114
    {
115
        if (is_array($method)) {
116
            $this->attributes['http_method'] = implode(',', $method);
117
        }
118
    }
119
120
    /**
121
     * @param $method
122
     *
123
     * @return array
124
     */
125
    public function getHttpMethodAttribute($method)
126
    {
127
        if (is_string($method)) {
128
            return array_filter(explode(',', $method));
129
        }
130
131
        return $method;
132
    }
133
134
    /**
135
     * Detach models from the relationship.
136
     *
137
     * @return void
138
     */
139
    protected static function boot()
140
    {
141
        parent::boot();
142
143
        static::deleting(function ($model) {
144
            $model->roles()->detach();
145
        });
146
    }
147
}
148