Completed
Push — develop ( 7c2b6f...49ae55 )
by Abdelrahman
08:15
created

Tenant::isSuperManager()   A

Complexity

Conditions 2
Paths 2

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 2
nc 2
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 string                                             $email
24
 * @property string                                             $website
25
 * @property string                                             $phone
26
 * @property string                                             $language_code
27
 * @property string                                             $country_code
28
 * @property string                                             $state
29
 * @property string                                             $city
30
 * @property string                                             $address
31
 * @property string                                             $postal_code
32
 * @property string                                             $launch_date
33
 * @property string                                             $timezone
34
 * @property string                                             $currency
35
 * @property string                                             $group
36
 * @property bool                                               $is_active
37
 * @property \Carbon\Carbon|null                                $created_at
38
 * @property \Carbon\Carbon|null                                $updated_at
39
 * @property \Carbon\Carbon|null                                $deleted_at
40
 * @property-read \Rinvex\Country\Country                       $country
41
 * @property-read \Rinvex\Language\Language                     $language
42
 *
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 whereCurrency($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 whereTimezone($value)
57
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereTitle($value)
58
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant wherePhone($value)
59
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant wherePostalCode($value)
60
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereSlug($value)
61
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereState($value)
62
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereUpdatedAt($value)
63
 * @mixin \Eloquent
64
 */
65
class Tenant extends Model
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
        'email',
80
        'website',
81
        'phone',
82
        'language_code',
83
        'country_code',
84
        'state',
85
        'city',
86
        'address',
87
        'postal_code',
88
        'launch_date',
89
        'timezone',
90
        'currency',
91
        'is_active',
92
    ];
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    protected $casts = [
98
        'slug' => 'string',
99
        'email' => 'string',
100
        'website' => 'string',
101
        'phone' => 'string',
102
        'country_code' => 'string',
103
        'language_code' => 'string',
104
        'state' => 'string',
105
        'city' => 'string',
106
        'address' => 'string',
107
        'postal_code' => 'string',
108
        'launch_date' => 'string',
109
        'timezone' => 'string',
110
        'currency' => 'string',
111
        'is_active' => 'boolean',
112
        'deleted_at' => 'datetime',
113
    ];
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    protected $observables = [
119
        'validating',
120
        'validated',
121
    ];
122
123
    /**
124
     * The attributes that are translatable.
125
     *
126
     * @var array
127
     */
128
    public $translatable = [
129
        'name',
130
        'description',
131
    ];
132
133
    /**
134
     * The default rules that the model will validate against.
135
     *
136
     * @var array
137
     */
138
    protected $rules = [];
139
140
    /**
141
     * Whether the model should throw a
142
     * ValidationException if it fails validation.
143
     *
144
     * @var bool
145
     */
146
    protected $throwValidationExceptions = true;
147
148
    /**
149
     * Create a new Eloquent model instance.
150
     *
151
     * @param array $attributes
152
     */
153
    public function __construct(array $attributes = [])
154
    {
155
        parent::__construct($attributes);
156
157
        $this->setTable(config('rinvex.tenants.tables.tenants'));
158
        $this->setRules([
159
            'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.tenants.tables.tenants').',slug',
160
            'name' => 'required|string|max:150',
161
            'description' => 'nullable|string|max:10000',
162
            'email' => 'required|email|min:3|max:150|unique:'.config('rinvex.tenants.tables.tenants').',email',
163
            'website' => 'nullable|string|max:150',
164
            'phone' => 'nullable|numeric|phone',
165
            'country_code' => 'required|alpha|size:2|country',
166
            'language_code' => 'required|alpha|size:2|language',
167
            'state' => 'nullable|string',
168
            'city' => 'nullable|string',
169
            'address' => 'nullable|string',
170
            'postal_code' => 'nullable|string',
171
            'launch_date' => 'nullable|date_format:Y-m-d',
172
            'timezone' => 'required|string|timezone',
173
            'currency' => 'required|alpha|size:3',
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 tenant's country.
205
     *
206
     * @return \Rinvex\Country\Country
207
     */
208
    public function getCountryAttribute()
209
    {
210
        return country($this->country_code);
211
    }
212
213
    /**
214
     * Get the tenant's language.
215
     *
216
     * @return \Rinvex\Language\Language
217
     */
218
    public function getLanguageAttribute()
219
    {
220
        return language($this->language_code);
221
    }
222
223
    /**
224
     * Determine if the given model is supermanager of the tenant.
225
     *
226
     * @param \Illuminate\Database\Eloquent\Model $model
227
     *
228
     * @return bool
229
     */
230
    public function isSuperManager(Model $model): bool
231
    {
232
        return $this->isManager($model) && $model->isA('supermanager');
233
    }
234
235
    /**
236
     * Determine if the given model is manager of the tenant.
237
     *
238
     * @param \Illuminate\Database\Eloquent\Model $model
239
     *
240
     * @return bool
241
     */
242
    public function isManager(Model $model): bool
243
    {
244
        return $model->tenants->contains($this);
245
    }
246
247
    /**
248
     * Activate the tenant.
249
     *
250
     * @return $this
251
     */
252
    public function activate()
253
    {
254
        $this->update(['is_active' => true]);
255
256
        return $this;
257
    }
258
259
    /**
260
     * Deactivate the tenant.
261
     *
262
     * @return $this
263
     */
264
    public function deactivate()
265
    {
266
        $this->update(['is_active' => false]);
267
268
        return $this;
269
    }
270
}
271