Completed
Push — develop ( 98e8af...13893c )
by Abdelrahman
01:15
created

Tenant::boot()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 1
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Tenant::entries() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Tenants\Models;
6
7
use Spatie\Sluggable\SlugOptions;
8
use Rinvex\Support\Traits\HasSlug;
9
use Illuminate\Database\Eloquent\Model;
10
use Rinvex\Cacheable\CacheableEloquent;
11
use Illuminate\Database\Eloquent\Builder;
12
use Rinvex\Support\Traits\HasTranslations;
13
use Rinvex\Support\Traits\ValidatingTrait;
14
use Rinvex\Tenants\Contracts\TenantContract;
15
use Illuminate\Database\Eloquent\Relations\MorphToMany;
16
17
/**
18
 * Rinvex\Tenants\Models\Tenant.
19
 *
20
 * @property int                                                $id
21
 * @property string                                             $slug
22
 * @property array                                              $name
23
 * @property array                                              $description
24
 * @property int                                                $owner_id
25
 * @property string                                             $email
26
 * @property string                                             $phone
27
 * @property string                                             $language_code
28
 * @property string                                             $country_code
29
 * @property string                                             $state
30
 * @property string                                             $city
31
 * @property string                                             $address
32
 * @property string                                             $postal_code
33
 * @property string                                             $launch_date
34
 * @property string                                             $group
35
 * @property bool                                               $is_active
36
 * @property \Carbon\Carbon|null                                $created_at
37
 * @property \Carbon\Carbon|null                                $updated_at
38
 * @property \Carbon\Carbon|null                                $deleted_at
39
 * @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $owner
40
 *
41
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant active()
42
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant inactive()
43
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereAddress($value)
44
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereCity($value)
45
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereCountryCode($value)
46
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereCreatedAt($value)
47
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereDeletedAt($value)
48
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereDescription($value)
49
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereEmail($value)
50
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereGroup($value)
51
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereId($value)
52
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereIsActive($value)
53
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereLanguageCode($value)
54
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereLaunchDate($value)
55
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereName($value)
56
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereOwnerId($value)
57
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant wherePhone($value)
58
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant wherePostalCode($value)
59
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereSlug($value)
60
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereState($value)
61
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereUpdatedAt($value)
62
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant withGroup($group)
63
 * @mixin \Eloquent
64
 */
65
class Tenant extends Model implements TenantContract
66
{
67
    use HasSlug;
68
    use HasTranslations;
69
    use ValidatingTrait;
70
    use CacheableEloquent;
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    protected $fillable = [
76
        'slug',
77
        'name',
78
        'description',
79
        'owner_id',
80
        'email',
81
        'phone',
82
        'language_code',
83
        'country_code',
84
        'state',
85
        'city',
86
        'address',
87
        'postal_code',
88
        'launch_date',
89
        'group',
90
        'is_active',
91
    ];
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    protected $casts = [
97
        'slug' => 'string',
98
        'owner_id' => 'integer',
99
        'email' => 'string',
100
        'phone' => 'string',
101
        'country_code' => 'string',
102
        'language_code' => 'string',
103
        'state' => 'string',
104
        'city' => 'string',
105
        'address' => 'string',
106
        'postal_code' => 'string',
107
        'launch_date' => 'string',
108
        'group' => 'string',
109
        'is_active' => 'boolean',
110
        'deleted_at' => 'datetime',
111
    ];
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    protected $observables = [
117
        'validating',
118
        'validated',
119
    ];
120
121
    /**
122
     * The attributes that are translatable.
123
     *
124
     * @var array
125
     */
126
    public $translatable = [
127
        'name',
128
        'description',
129
    ];
130
131
    /**
132
     * The default rules that the model will validate against.
133
     *
134
     * @var array
135
     */
136
    protected $rules = [];
137
138
    /**
139
     * Whether the model should throw a
140
     * ValidationException if it fails validation.
141
     *
142
     * @var bool
143
     */
144
    protected $throwValidationExceptions = true;
145
146
    /**
147
     * Create a new Eloquent model instance.
148
     *
149
     * @param array $attributes
150
     */
151
    public function __construct(array $attributes = [])
152
    {
153
        parent::__construct($attributes);
154
155
        // Get users model
156
        $userModel = config('auth.providers.'.config('auth.guards.'.config('auth.defaults.guard').'.provider').'.model');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
157
158
        $this->setTable(config('rinvex.tenants.tables.tenants'));
159
        $this->setRules([
160
            'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.tenants.tables.tenants').',slug',
161
            'name' => 'required|string|max:150',
162
            'description' => 'nullable|string|max:10000',
163
            'owner_id' => 'required|integer|exists:'.(new $userModel())->getTable().',id',
164
            'email' => 'required|email|min:3|max:150|unique:'.config('rinvex.tenants.tables.tenants').',email',
165
            'phone' => 'nullable|numeric|min:4',
166
            'country_code' => 'required|alpha|size:2|country',
167
            'language_code' => 'required|alpha|size:2|language',
168
            'state' => 'nullable|string',
169
            'city' => 'nullable|string',
170
            'address' => 'nullable|string',
171
            'postal_code' => 'nullable|string',
172
            'launch_date' => 'nullable|date_format:Y-m-d',
173
            'group' => 'nullable|string|max:150',
174
            'is_active' => 'sometimes|boolean',
175
        ]);
176
    }
177
178
    /**
179
     * Get all attached models of the given class to the tenant.
180
     *
181
     * @param string $class
182
     *
183
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
184
     */
185
    public function entries(string $class): MorphToMany
186
    {
187
        return $this->morphedByMany($class, 'tenantable', config('rinvex.tenants.tables.tenantables'), 'tenant_id', 'tenantable_id');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 133 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
188
    }
189
190
    /**
191
     * Get the options for generating the slug.
192
     *
193
     * @return \Spatie\Sluggable\SlugOptions
194
     */
195
    public function getSlugOptions(): SlugOptions
196
    {
197
        return SlugOptions::create()
198
                          ->doNotGenerateSlugsOnUpdate()
199
                          ->generateSlugsFrom('name')
200
                          ->saveSlugsTo('slug');
201
    }
202
203
    /**
204
     * Get the active tenants.
205
     *
206
     * @param \Illuminate\Database\Eloquent\Builder $builder
207
     *
208
     * @return \Illuminate\Database\Eloquent\Builder
209
     */
210
    public function scopeActive(Builder $builder): Builder
211
    {
212
        return $builder->where('is_active', true);
213
    }
214
215
    /**
216
     * Get the inactive tenants.
217
     *
218
     * @param \Illuminate\Database\Eloquent\Builder $builder
219
     *
220
     * @return \Illuminate\Database\Eloquent\Builder
221
     */
222
    public function scopeInactive(Builder $builder): Builder
223
    {
224
        return $builder->where('is_active', false);
225
    }
226
227
    /**
228
     * Scope tenants by given group.
229
     *
230
     * @param \Illuminate\Database\Eloquent\Builder $builder
231
     * @param string                                $group
232
     *
233
     * @return \Illuminate\Database\Eloquent\Builder
234
     */
235
    public function scopeWithGroup(Builder $builder, string $group): Builder
236
    {
237
        return $builder->where('group', $group);
238
    }
239
240
    /**
241
     * A tenant always belongs to an owner.
242
     *
243
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
244
     */
245
    public function owner()
246
    {
247
        $userModel = config('auth.providers.'.config('auth.guards.'.config('auth.defaults.guard').'.provider').'.model');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
248
249
        return $this->belongsTo($userModel, 'owner_id', 'id');
250
    }
251
252
    /**
253
     * Activate the tenant.
254
     *
255
     * @return $this
256
     */
257
    public function activate()
258
    {
259
        $this->update(['is_active' => true]);
260
261
        return $this;
262
    }
263
264
    /**
265
     * Deactivate the tenant.
266
     *
267
     * @return $this
268
     */
269
    public function deactivate()
270
    {
271
        $this->update(['is_active' => false]);
272
273
        return $this;
274
    }
275
}
276