Completed
Push — master ( b739db...92d607 )
by Abdelrahman
02:42
created

Role::isProtected()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Fort Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Fort Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Fort\Models;
17
18
use Illuminate\Database\Eloquent\Model;
19
use Illuminate\Database\Eloquent\SoftDeletes;
20
21
class Role extends Model
22
{
23
    use SoftDeletes;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected $dates = ['deleted_at'];
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected $fillable = [
34
        'slug',
35
        'title',
36
        'description',
37
    ];
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected $with = ['abilities'];
43
44
    /**
45
     * Create a new Eloquent model instance.
46
     *
47
     * @param array $attributes
48
     *
49
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
50
     */
51
    public function __construct(array $attributes = [])
52
    {
53
        parent::__construct($attributes);
54
55
        $this->setTable(config('rinvex.fort.tables.roles'));
56
    }
57
58
    /**
59
     * A role may be given various abilities.
60
     *
61
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
62
     */
63
    public function abilities()
64
    {
65
        return $this->belongsToMany(config('rinvex.fort.models.ability'), config('rinvex.fort.tables.ability_role'))
66
                    ->withTimestamps();
67
    }
68
69
    /**
70
     * A role may be assigned to various users.
71
     *
72
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
73
     */
74
    public function users()
75
    {
76
        return $this->belongsToMany(config('rinvex.fort.models.user'), config('rinvex.fort.tables.role_user'))
77
                    ->withTimestamps();
78
    }
79
    /**
80
     * Determine if the role is super admin.
81
     *
82
     * @return bool
83
     */
84
    public function isSuperadmin()
85
    {
86
        return $this->abilities->where('policy', null)->contains('action', 'superadmin');
0 ignored issues
show
Documentation introduced by
The property abilities does not exist on object<Rinvex\Fort\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...
87
    }
88
89
    /**
90
     * Determine if the role is protected.
91
     *
92
     * @return bool
93
     */
94
    public function isProtected()
95
    {
96
        return in_array($this->id, config('rinvex.fort.protected.roles'));
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Rinvex\Fort\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...
97
    }
98
}
99