Completed
Push — develop ( 74ebca...62f4c1 )
by Abdelrahman
01:26
created

Tenant::scopeOfUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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