Completed
Push — master ( c436ad...d0a6e2 )
by Stephen
02:15
created

Level::users()   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 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace z1haze\Acl\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use z1haze\Acl\Traits\UserAndLevel;
7
8
class Level extends Model
9
{
10
    protected $casts = ['id' => 'integer', 'rank' => 'integer'];
11
    protected $guarded = ['id', 'created_id', 'updated_at'];
12
    protected $table;
13
14
    use UserAndLevel;
15
16
    /**
17
     * LEVEL
18
     * A Level has many users
19
     *
20
     * @return mixed
21
     */
22 2
    public function users()
23
    {
24 2
        return $this->hasMany(config('laravel-acl.user'));
25
    }
26
27
    /**
28
     * LEVEL
29
     * A level has many permissions
30
     *
31
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
32
     */
33 14
    public function permissions()
34
    {
35 14
        $model = config('laravel-acl.permission', Permission::class);
36
37 14
        return $this->hasMany($model);
38
    }
39
40
41
    /* ------------------------------------------------------------------------------------------------
42
     |  Other Functions
43
     | ------------------------------------------------------------------------------------------------
44
     */
45
    /**
46
     * Handle model events
47
     */
48 53
    public static function boot()
49
    {
50 53
        parent::boot();
51
52 53
        static::deleting(function ($level) {
53 1
            $level->permissions()->where('level_id', $level->id)->update(['level_id' => null]);
54 1
            foreach ($level->users as $user) {
55
                $user->level()->dissociate()->save();
56
            }
57 53
        });
58 53
    }
59
60 53
    public function __construct(array $attributes = [])
61
    {
62 53
        parent::__construct($attributes);
63
64 53
        $this->table = config('laravel-acl.tables.level');
65 53
    }
66
}
67