Completed
Push — master ( 28c984...b2bcde )
by Troy
01:35
created

BaseTenant   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 2
dl 0
loc 71
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A setFillable() 0 10 1
A owner() 0 4 1
A features() 0 4 1
A users() 0 4 1
A assignFeature() 0 4 1
A hasFeature() 0 4 1
1
<?php
2
3
namespace MultiTenantLaravel\App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
abstract class BaseTenant extends Model
8
{
9
    /**
10
     * Construct the model with the provided config settings
11
     * or use our fallbacks if none are provided
12
     */
13
    public function __construct(array $attributes = [])
14
    {
15
        $this->setTable(config('multi-tenant.table_name'));
16
17
        $this->setFillable();
18
19
        // parent construct must go below property customizations
20
        parent::__construct($attributes);
21
    }
22
23
    /**
24
     *  Merge the fillable fields with any provided in the extending class
25
     */
26
    public function setFillable()
27
    {
28
        $fillable = [
29
            'owner_id',
30
            'slug',
31
            'name'
32
        ];
33
34
        $this->fillable(array_merge($this->fillable, $fillable));
35
    }
36
37
    /**
38
     *  Merge the fillable fields with any provided in the extending class
39
     */
40
    public function owner()
41
    {
42
        return $this->belongsTo(config('multi-tenant.user_class'), 'owner_id');
43
    }
44
45
    /**
46
     *  The features relationship
47
     */
48
    public function features()
49
    {
50
        return $this->belongsToMany(config('multi-tenant.feature_class'));
51
    }
52
53
    /**
54
     *  The users relationship
55
     */
56
    public function users()
57
    {
58
        return $this->belongsToMany(config('multi-tenant.user_class'));
59
    }
60
61
62
    /**
63
     *  The features relationship
64
     */
65
    public function assignFeature(BaseFeature $feature)
66
    {
67
        return $this->features()->syncWithoutDetaching($feature);
68
    }
69
70
    /**
71
     *  The features relationship
72
     */
73
    public function hasFeature(BaseFeature $feature)
74
    {
75
        return $this->features()->where('feature_id', $feature->id)->exists();
76
    }
77
}
78