BaseTenant   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A setFillable() 0 14 2
A owner() 0 4 1
A features() 0 9 1
A users() 0 9 1
A assignUser() 0 4 1
A hasUser() 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
        foreach(config('multi-tenant.additional_tenant_columns') as $key => $column) {
35
            $fillable[] = $key;
36
        }
37
38
        $this->fillable(array_merge($this->fillable, $fillable));
39
    }
40
41
    /**
42
     *  Merge the fillable fields with any provided in the extending class
43
     */
44
    public function owner()
45
    {
46
        return $this->belongsTo(config('multi-tenant.user_class'), 'owner_id');
47
    }
48
49
    /**
50
     *  The features relationship
51
     */
52
    public function features()
53
    {
54
        return $this->belongsToMany(
55
            config('multi-tenant.feature_class'),
56
            'feature_' . str_singular(config('multi-tenant.table_name')),
57
            str_singular(config('multi-tenant.table_name')) . '_id',
58
            'feature_id'
59
        );
60
    }
61
62
    /**
63
     *  The users relationship
64
     */
65
    public function users()
66
    {
67
        return $this->belongsToMany(
68
            config('multi-tenant.user_class'),
69
            str_singular(config('multi-tenant.table_name')) . '_user',
70
            str_singular(config('multi-tenant.table_name')) . '_id',
71
            'user_id'
72
        );
73
    }
74
75
    /**
76
     * Assign the given user to the teannt
77
     *
78
     * @param BaseUser $user
79
     *
80
     * @return void
81
     */
82
    public function assignUser(BaseUser $user)
83
    {
84
        return $this->users()->syncWithoutDetaching($user);
85
    }
86
87
    /**
88
     * Test that user has access to the this tenant
89
     *
90
     * @param BaseUser $user
91
     *
92
     * @return boolean
93
     */
94
    public function hasUser(BaseUser $user)
95
    {
96
        return $this->users()->where('user_id', $user->id)->exists();
97
    }
98
99
    /**
100
     * Assign the given feature to the teannt
101
     *
102
     * @param BaseFeature $feature
103
     *
104
     * @return void
105
     */
106
    public function assignFeature(BaseFeature $feature)
107
    {
108
        return $this->features()->syncWithoutDetaching($feature);
109
    }
110
111
    /**
112
     * Test that the tenant has access to a given feature
113
     *
114
     * @param BaseFeature $feature
115
     *
116
     * @return boolean
117
     */
118
    public function hasFeature(BaseFeature $feature)
119
    {
120
        return $this->features()->where('feature_id', $feature->id)->exists();
121
    }
122
}
123