Completed
Push — develop ( 903485...edab0c )
by Abdelrahman
01:15
created

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