Completed
Pull Request — master (#1)
by ARCANEDEV
04:05
created

PermissionsGroup::permissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
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 44
    public function __construct(array $attributes = [])
42
    {
43 44
        $this->setTable(config('laravel-auth.permissions-group.table', 'permissions_group'));
44
45 44
        parent::__construct($attributes);
46 44
    }
47
48
    /* ------------------------------------------------------------------------------------------------
49
     |  Relationships
50
     | ------------------------------------------------------------------------------------------------
51
     */
52
    /**
53
     * Permissions Groups has many permissions.
54
     *
55
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
56
     */
57 36
    public function permissions()
58
    {
59 36
        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  $permission
85
     * @param  bool                                      $reload
86
     */
87 20
    public function attachPermission(&$permission, $reload = true)
88
    {
89 20
        if ($this->hasPermission($permission)) {
90 4
            return;
91
        }
92
93 20
        $permission = $this->permissions()->save($permission);
94
95 20
        if ($reload) {
96 20
            $this->load('permissions');
97 15
        }
98 20
    }
99
100
    /**
101
     * Attach the permission by id to a group.
102
     *
103
     * @param  int   $id
104
     * @param  bool  $reload
105
     *
106
     * @return \Arcanedev\LaravelAuth\Models\Permission
107
     */
108 4
    public function attachPermissionById($id, $reload = true)
109
    {
110 4
        $permission = $this->getPermissionById($id);
111
112 4
        $this->attachPermission($permission, $reload);
0 ignored issues
show
Bug introduced by
It seems like $permission defined by $this->getPermissionById($id) on line 110 can be null; however, Arcanedev\LaravelAuth\Mo...oup::attachPermission() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
113
114 4
        return $permission;
115
    }
116
117
    /**
118
     * Attach the permission from a group.
119
     *
120
     * @param  \Arcanedev\LaravelAuth\Models\Permission  $permission
121
     * @param  bool                                      $reload
122
     */
123 12
    public function detachPermission(&$permission, $reload = true)
124
    {
125 12
        if ( ! $this->hasPermission($permission)) {
126 8
            return;
127
        }
128
129 12
        $permission = $this->getPermissionFromGroup($permission);
130
131 12
        $permission->update([
132 12
            'group_id' => 0,
133 9
        ]);
134
135 12
        if ($reload) {
136 12
            $this->load('permissions');
137 9
        }
138 12
    }
139
140
    /**
141
     * Attach the permission by id to a group.
142
     *
143
     * @param  int   $id
144
     * @param  bool  $reload
145
     *
146
     * @return \Arcanedev\LaravelAuth\Models\Permission
147
     */
148 4
    public function detachPermissionById($id, $reload = true)
149
    {
150 4
        $permission = $this->getPermissionById($id);
151
152 4
        $this->detachPermission($permission, $reload);
0 ignored issues
show
Bug introduced by
It seems like $permission defined by $this->getPermissionById($id) on line 150 can be null; however, Arcanedev\LaravelAuth\Mo...oup::detachPermission() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
153
154 4
        return $permission;
155
    }
156
157
    /* ------------------------------------------------------------------------------------------------
158
     |  Check Functions
159
     | ------------------------------------------------------------------------------------------------
160
     */
161
    /**
162
     * Check if role has the given permission (Permission Model or Id).
163
     *
164
     * @param  \Arcanedev\LaravelAuth\Models\Permission|int  $id
165
     *
166
     * @return bool
167
     */
168 20
    public function hasPermission($id)
169
    {
170 20
        if ($id instanceof Permission) {
171 20
            $id = $id->getKey();
172 15
        }
173
174 20
        return ! is_null($this->getPermissionFromGroup($id));
175
    }
176
177
    /**
178
     * Get a permission from the group.
179
     *
180
     * @param  \Arcanedev\LaravelAuth\Models\Permission|int  $id
181
     *
182
     * @return \Arcanedev\LaravelAuth\Models\Permission|null
183
     */
184 20
    private function getPermissionFromGroup($id)
185
    {
186 20
        if ($id instanceof Permission) {
187 12
            $id = $id->getKey();
188 9
        }
189
190 20
        $this->load('permissions');
191
192 20
        return $this->permissions->filter(function (Permission $permission) use ($id) {
193 20
            return $permission->id == $id;
194 20
        })->first();
195
    }
196
197
    /**
198
     * Get a permission by id.
199
     *
200
     * @param  int  $id
201
     *
202
     * @return \Arcanedev\LaravelAuth\Models\Permission|null
203
     */
204 8
    private function getPermissionById($id)
205
    {
206 8
        return $this->permissions()
207 8
            ->getRelated()
208 8
            ->where('id', $id)
209 8
            ->first();
210
    }
211
}
212