Completed
Push — master ( c1e6c3...800341 )
by Stephen
04:07 queued 02:05
created

Level::__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 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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
     * The table associated with the model.
29
     *
30
     * @var string
31
     */
32
    protected $table;
33
34
    /**
35
     * LEVEL
36
     * A Level has many users
37
     *
38
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
39
     */
40 2
    public function users()
41
    {
42 2
        return $this->hasMany(config('laravel-acl.user'));
43
    }
44
45
    /**
46
     * LEVEL
47
     * A level has many permissions
48
     *
49
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
50
     */
51 14
    public function permissions()
52
    {
53 14
        $model = config('laravel-acl.permission', Permission::class);
54
55 14
        return $this->hasMany($model);
56
    }
57
58
59
    /* ------------------------------------------------------------------------------------------------
60
     |  Other Functions
61
     | ------------------------------------------------------------------------------------------------
62
     */
63
    /**
64
     * Level constructor.
65
     * Sets the table name from the config
66
     *
67
     * @param array $attributes
68
     */
69 53
    public function __construct(array $attributes = [])
70
    {
71 53
        parent::__construct($attributes);
72
73 53
        $this->table = config('laravel-acl.tables.level');
74 53
    }
75
76
    /**
77
     * Handle model events
78
     */
79 53
    public static function boot()
80
    {
81 53
        parent::boot();
82
83 53
        static::deleting(function ($level) {
84 1
            $level->permissions()->where('level_id', $level->id)->update(['level_id' => null]);
85 1
            foreach ($level->users as $user) {
86
                $user->level()->dissociate()->save();
87
            }
88 53
        });
89 53
    }
90
}
91