Completed
Pull Request — master (#1)
by ARCANEDEV
03:00
created

PermissionsGroup::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4286
c 1
b 0
f 0
ccs 4
cts 4
cp 1
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php namespace Arcanedev\LaravelAuth\Models;
2
3
use Arcanedev\LaravelAuth\Bases\Model;
4
5
/**
6
 * Class     PermissionsGroup
7
 *
8
 * @package  Arcanedev\LaravelAuth\Models
9
 * @author   ARCANEDEV <[email protected]>
10
 *
11
 * @property  int                                       id
12
 * @property  string                                    name
13
 * @property  string                                    slug
14
 * @property  string                                    description
15
 * @property  \Carbon\Carbon                            created_at
16
 * @property  \Carbon\Carbon                            updated_at
17
 * @property  \Illuminate\Database\Eloquent\Collection  permissions
18
 */
19
class PermissionsGroup extends Model
20
{
21
    /* ------------------------------------------------------------------------------------------------
22
     |  Properties
23
     | ------------------------------------------------------------------------------------------------
24
     */
25
    /**
26
     * The attributes that are mass assignable.
27
     *
28
     * @var array
29
     */
30
    protected $fillable = ['name', 'slug', 'description'];
31
32
    /* ------------------------------------------------------------------------------------------------
33
     |  Constructor
34
     | ------------------------------------------------------------------------------------------------
35
     */
36
    /**
37
     * Create a new Eloquent model instance.
38
     *
39
     * @param  array  $attributes
40
     */
41 28
    public function __construct(array $attributes = [])
42
    {
43 28
        $this->setTable(config('laravel-auth.permissions-group.table', 'permissions_group'));
44
45 28
        parent::__construct($attributes);
46 28
    }
47
48
    /* ------------------------------------------------------------------------------------------------
49
     |  Relationships
50
     | ------------------------------------------------------------------------------------------------
51
     */
52
    /**
53
     * Permissions Groups has many permissions.
54
     *
55
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
56
     */
57 20
    public function permissions()
58
    {
59 20
        return $this->hasMany(Permission::class, 'group_id');
60
    }
61
62
    /* ------------------------------------------------------------------------------------------------
63
     |  CRUD Functions
64
     | ------------------------------------------------------------------------------------------------
65
     */
66
    /**
67
     * Create and attach a permission.
68
     *
69
     * @param  array  $attributes
70
     * @param  bool   $reload
71
     */
72 8
    public function createPermission(array $attributes, $reload = true)
73
    {
74 8
        $this->permissions()->create($attributes);
75
76 8
        if ($reload) {
77 8
            $this->load('permissions');
78 6
        }
79 8
    }
80
81
    /**
82
     * Attach the permission to a group.
83
     *
84
     * @param  \Arcanedev\LaravelAuth\Models\Permission|int  $permission
85
     * @param  bool                                          $reload
86
     */
87 4
    public function attachPermission($permission, $reload = true)
88
    {
89 4
        if ($this->hasPermission($permission)) {
90 4
            return;
91
        }
92
93 4
        $this->permissions()->save($permission);
0 ignored issues
show
Bug introduced by
It seems like $permission defined by parameter $permission on line 87 can also be of type integer; however, Illuminate\Database\Eloq...ns\HasOneOrMany::save() does only seem to accept object<Illuminate\Database\Eloquent\Model>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
94
95 4
        if ($reload) {
96 4
            $this->load('permissions');
97 3
        }
98 4
    }
99
100
    /* ------------------------------------------------------------------------------------------------
101
     |  Check Functions
102
     | ------------------------------------------------------------------------------------------------
103
     */
104
    /**
105
     * Check if role has the given permission (Permission Model or Id).
106
     *
107
     * @param  mixed  $id
108
     *
109
     * @return bool
110
     */
111 4
    public function hasPermission($id)
112
    {
113 4
        if ($id instanceof Permission) {
114 4
            $id = $id->getKey();
115 3
        }
116
117 4
        return $this->permissions->contains($id);
118
    }
119
}
120