Completed
Push — master ( a8dd31...643241 )
by Song
02:39
created

Permission::getHttpPathAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
    public function __construct(array $attributes = [])
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
Duplication introduced by
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
Documentation introduced by
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...
Documentation introduced by
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
Documentation introduced by
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
Bug introduced by
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("\n", $this->http_path));
0 ignored issues
show
Documentation introduced by
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
     * filter \r
91
     * 
92
     * @param string  $path
93
     * @return mixed
94
     */
95
    public function getHttpPathAttribute($path)
96
    {
97
        return str_replace("\r\n", "\n", $path);
98
    }
99
    
100
    /**
101
     * If a request match the specific HTTP method and path.
102
     *
103
     * @param array   $match
104
     * @param Request $request
105
     *
106
     * @return bool
107
     */
108
    protected function matchRequest(array $match, Request $request) : bool
109
    {
110
        if (!$request->is(trim($match['path'], '/'))) {
111
            return false;
112
        }
113
114
        $method = collect($match['method'])->filter()->map(function ($method) {
115
            return strtoupper($method);
116
        });
117
118
        return $method->isEmpty() || $method->contains($request->method());
119
    }
120
121
    /**
122
     * @param $method
123
     */
124
    public function setHttpMethodAttribute($method)
125
    {
126
        if (is_array($method)) {
127
            $this->attributes['http_method'] = implode(',', $method);
128
        }
129
    }
130
131
    /**
132
     * @param $method
133
     *
134
     * @return array
135
     */
136
    public function getHttpMethodAttribute($method)
137
    {
138
        if (is_string($method)) {
139
            return array_filter(explode(',', $method));
140
        }
141
142
        return $method;
143
    }
144
145
    /**
146
     * Detach models from the relationship.
147
     *
148
     * @return void
149
     */
150
    protected static function boot()
151
    {
152
        parent::boot();
153
154
        static::deleting(function ($model) {
155
            $model->roles()->detach();
156
        });
157
    }
158
}
159