Level::users()   A
last analyzed

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\Contracts\AclLevelInterface;
7
use z1haze\Acl\Traits\UserAndLevel;
8
9
class Level extends Model implements AclLevelInterface
10
{
11
    use UserAndLevel;
12
13
    /**
14
     * The attributes that should be cast to native types.
15
     *
16
     * @var array
17
     */
18
    protected $casts = ['id' => 'integer', 'rank' => 'integer'];
19
20
    /**
21
     * The attributes that aren't mass assignable.
22
     *
23
     * @var array
24
     */
25
    protected $guarded = ['id', 'created_id', 'updated_at'];
26
27
    /**
28
     * LEVEL
29
     * A Level has many users
30
     *
31
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
32
     */
33 2
    public function users()
34
    {
35 2
        return $this->hasMany(config('laravel-acl.user'));
36
    }
37
38
    /**
39
     * LEVEL
40
     * A level has many permissions
41
     *
42
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
43
     */
44 15
    public function permissions()
45
    {
46 15
        $model = config('laravel-acl.permission', Permission::class);
47
48 15
        return $this->hasMany($model);
49
    }
50
51
52
    /* ------------------------------------------------------------------------------------------------
53
     |  Other Functions
54
     | ------------------------------------------------------------------------------------------------
55
     */
56
    /**
57
     * Level constructor.
58
     * Sets the table name from the config
59
     *
60
     * @param array $attributes
61
     */
62 55
    public function __construct(array $attributes = [])
63
    {
64 55
        parent::__construct($attributes);
65
66 55
        $this->table = config('laravel-acl.tables.level');
67 55
    }
68
69
    /**
70
     * Handle model events
71
     */
72 55
    public static function boot()
73
    {
74 55
        parent::boot();
75
76 55
        static::deleting(function($level) {
77 1
            $level->permissions()->where('level_id', $level->id)->update(['level_id' => null]);
78 1
            foreach ($level->users as $user) {
79
                $user->level()->dissociate()->save();
80
            }
81 55
        });
82 55
    }
83
}
84