Completed
Push — master ( 9c1429...8b1ed7 )
by Abdelrahman
06:12 queued 05:02
created

Tenant::entries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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 Illuminate\Database\Eloquent\Relations\MorphToMany;
15
16
/**
17
 * Rinvex\Tenants\Models\Tenant.
18
 *
19
 * @property int                                                $id
20
 * @property string                                             $slug
21
 * @property array                                              $title
22
 * @property array                                              $description
23
 * @property int                                                $owner_id
24
 * @property string                                             $owner_type
25
 * @property string                                             $email
26
 * @property string                                             $website
27
 * @property string                                             $phone
28
 * @property string                                             $language_code
29
 * @property string                                             $country_code
30
 * @property string                                             $state
31
 * @property string                                             $city
32
 * @property string                                             $address
33
 * @property string                                             $postal_code
34
 * @property string                                             $launch_date
35
 * @property string                                             $timezone
36
 * @property string                                             $currency
37
 * @property string                                             $group
38
 * @property bool                                               $is_active
39
 * @property \Carbon\Carbon|null                                $created_at
40
 * @property \Carbon\Carbon|null                                $updated_at
41
 * @property \Carbon\Carbon|null                                $deleted_at
42
 * @property-read \Rinvex\Country\Country                       $country
43
 * @property-read \Rinvex\Language\Language                     $language
44
 * @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $owner
45
 *
46
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant ofOwner(\Illuminate\Database\Eloquent\Model $owner)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 137 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...
47
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereAddress($value)
48
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereCity($value)
49
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereCountryCode($value)
50
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereCreatedAt($value)
51
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereCurrency($value)
52
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereDeletedAt($value)
53
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereDescription($value)
54
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereEmail($value)
55
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereGroup($value)
56
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereId($value)
57
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereIsActive($value)
58
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereLanguageCode($value)
59
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereLaunchDate($value)
60
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereTimezone($value)
61
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereTitle($value)
62
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereOwnerId($value)
63
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereOwnerType($value)
64
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant wherePhone($value)
65
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant wherePostalCode($value)
66
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereSlug($value)
67
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereState($value)
68
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereUpdatedAt($value)
69
 * @mixin \Eloquent
70
 */
71
class Tenant extends Model
72
{
73
    use HasSlug;
74
    use HasTranslations;
75
    use ValidatingTrait;
76
    use CacheableEloquent;
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    protected $fillable = [
82
        'slug',
83
        'name',
84
        'description',
85
        'owner_id',
86
        'owner_type',
87
        'email',
88
        'website',
89
        'phone',
90
        'language_code',
91
        'country_code',
92
        'state',
93
        'city',
94
        'address',
95
        'postal_code',
96
        'launch_date',
97
        'timezone',
98
        'currency',
99
        'is_active',
100
    ];
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    protected $casts = [
106
        'slug' => 'string',
107
        'owner_id' => 'integer',
108
        'owner_type' => 'string',
109
        'email' => 'string',
110
        'website' => 'string',
111
        'phone' => 'string',
112
        'country_code' => 'string',
113
        'language_code' => 'string',
114
        'state' => 'string',
115
        'city' => 'string',
116
        'address' => 'string',
117
        'postal_code' => 'string',
118
        'launch_date' => 'string',
119
        'timezone' => 'string',
120
        'currency' => 'string',
121
        'is_active' => 'boolean',
122
        'deleted_at' => 'datetime',
123
    ];
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    protected $observables = [
129
        'validating',
130
        'validated',
131
    ];
132
133
    /**
134
     * The attributes that are translatable.
135
     *
136
     * @var array
137
     */
138
    public $translatable = [
139
        'name',
140
        'description',
141
    ];
142
143
    /**
144
     * The default rules that the model will validate against.
145
     *
146
     * @var array
147
     */
148
    protected $rules = [];
149
150
    /**
151
     * Whether the model should throw a
152
     * ValidationException if it fails validation.
153
     *
154
     * @var bool
155
     */
156
    protected $throwValidationExceptions = true;
157
158
    /**
159
     * Create a new Eloquent model instance.
160
     *
161
     * @param array $attributes
162
     */
163
    public function __construct(array $attributes = [])
164
    {
165
        parent::__construct($attributes);
166
167
        $this->setTable(config('rinvex.tenants.tables.tenants'));
168
        $this->setRules([
169
            'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.tenants.tables.tenants').',slug',
170
            'name' => 'required|string|max:150',
171
            'description' => 'nullable|string|max:10000',
172
            'owner_id' => 'required|integer',
173
            'owner_type' => 'required|string',
174
            'email' => 'required|email|min:3|max:150|unique:'.config('rinvex.tenants.tables.tenants').',email',
175
            'website' => 'nullable|string|max:150',
176
            'phone' => 'nullable|numeric|phone',
177
            'country_code' => 'required|alpha|size:2|country',
178
            'language_code' => 'required|alpha|size:2|language',
179
            'state' => 'nullable|string',
180
            'city' => 'nullable|string',
181
            'address' => 'nullable|string',
182
            'postal_code' => 'nullable|string',
183
            'launch_date' => 'nullable|date_format:Y-m-d',
184
            'timezone' => 'required|string|timezone',
185
            'currency' => 'required|alpha|size:3',
186
            'is_active' => 'sometimes|boolean',
187
        ]);
188
    }
189
190
    /**
191
     * Get all attached models of the given class to the tenant.
192
     *
193
     * @param string $class
194
     *
195
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
196
     */
197
    public function entries(string $class): MorphToMany
198
    {
199
        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...
200
    }
201
202
    /**
203
     * Get the options for generating the slug.
204
     *
205
     * @return \Spatie\Sluggable\SlugOptions
206
     */
207
    public function getSlugOptions(): SlugOptions
208
    {
209
        return SlugOptions::create()
210
                          ->doNotGenerateSlugsOnUpdate()
211
                          ->generateSlugsFrom('name')
212
                          ->saveSlugsTo('slug');
213
    }
214
215
    /**
216
     * Get the tenant's country.
217
     *
218
     * @return \Rinvex\Country\Country
219
     */
220
    public function getCountryAttribute()
221
    {
222
        return country($this->country_code);
223
    }
224
225
    /**
226
     * Get the tenant's language.
227
     *
228
     * @return \Rinvex\Language\Language
229
     */
230
    public function getLanguageAttribute()
231
    {
232
        return language($this->language_code);
233
    }
234
235
    /**
236
     * Get the tenant owner.
237
     *
238
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
239
     */
240
    public function owner()
241
    {
242
        return $this->morphTo('owner', 'owner_type', 'owner_id');
243
    }
244
245
    /**
246
     * Determine if the given model is owner of tenant.
247
     *
248
     * @param \Illuminate\Database\Eloquent\Model $model
249
     *
250
     * @return bool
251
     */
252
    public function isOwner(Model $model): bool
253
    {
254
        return $model->getKey() === $this->owner->getKey();
255
    }
256
257
    /**
258
     * Determine if the given model is staff of tenant.
259
     *
260
     * @param \Illuminate\Database\Eloquent\Model $model
261
     *
262
     * @return bool
263
     */
264
    public function isStaff(Model $model): bool
265
    {
266
        return $model->tenants->contains($this);
267
    }
268
269
    /**
270
     * Get tenants of the given owner.
271
     *
272
     * @param \Illuminate\Database\Eloquent\Builder $builder
273
     * @param \Illuminate\Database\Eloquent\Model   $owner
274
     *
275
     * @return \Illuminate\Database\Eloquent\Builder
276
     */
277
    public function scopeOfOwner(Builder $builder, Model $owner): Builder
278
    {
279
        return $builder->where('owner_type', $owner->getMorphClass())->where('owner_id', $owner->getKey());
280
    }
281
282
    /**
283
     * Activate the tenant.
284
     *
285
     * @return $this
286
     */
287
    public function activate()
288
    {
289
        $this->update(['is_active' => true]);
290
291
        return $this;
292
    }
293
294
    /**
295
     * Deactivate the tenant.
296
     *
297
     * @return $this
298
     */
299
    public function deactivate()
300
    {
301
        $this->update(['is_active' => false]);
302
303
        return $this;
304
    }
305
}
306