Completed
Push — master ( 53e6da...20c72d )
by Freek
02:09
created

Role::permissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Spatie\Permission\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Spatie\Permission\Contracts\Role as RoleContract;
7
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
8
use Spatie\Permission\Exceptions\RoleDoesNotExist;
9
use Spatie\Permission\Traits\HasPermissions;
10
use Spatie\Permission\Traits\RefreshesPermissionCache;
11
12
class Role extends Model implements RoleContract
13
{
14
    use HasPermissions;
15
    use RefreshesPermissionCache;
16
17
    /**
18
     * The attributes that aren't mass assignable.
19
     *
20
     * @var array
21
     */
22
    public $guarded = ['id'];
23
24
    /**
25
     * Create a new Eloquent model instance.
26
     *
27
     * @param array $attributes
28
     */
29
    public function __construct(array $attributes = [])
30
    {
31
        parent::__construct($attributes);
32
33
        $this->setTable(config('laravel-permission.table_names.roles'));
34
    }
35
36
    /**
37
     * A role may be given various permissions.
38
     *
39
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
40
     */
41
    public function permissions()
42
    {
43
        return $this->belongsToMany(
44
            config('laravel-permission.models.permission'),
45
            config('laravel-permission.table_names.role_has_permissions')
46
        );
47
    }
48
49
    /**
50
     * A role may be assigned to various users.
51
     *
52
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
53
     */
54
    public function users()
55
    {
56
        return $this->belongsToMany(
57
            config('auth.model') ?: config('auth.providers.users.model'),
58
            config('laravel-permission.table_names.user_has_roles')
59
        );
60
    }
61
62
    /**
63
     * Find a role by its name.
64
     *
65
     * @param string $name
66
     *
67
     * @return Role
68
     *
69
     * @throws RoleDoesNotExist
70
     */
71
    public static function findByName($name)
72
    {
73
        $role = static::where('name', $name)->first();
74
75
        if (!$role) {
76
            throw new RoleDoesNotExist();
77
        }
78
79
        return $role;
80
    }
81
82
    /**
83
     * Determine if the user may perform the given permission.
84
     *
85
     * @param string|Permission $permission
86
     *
87
     * @return bool
88
     */
89 View Code Duplication
    public function hasPermissionTo($permission)
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...
90
    {
91
        if (is_string($permission)) {
92
            try {
93
                $permission = app(Permission::class)->findByName($permission);
94
            } catch (PermissionDoesNotExist $e) {
95
                return false;
96
            }
97
        }
98
99
        return $this->permissions->contains('id', $permission->id);
0 ignored issues
show
Documentation introduced by
The property permissions does not exist on object<Spatie\Permission\Models\Role>. 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...
100
    }
101
}
102