Completed
Push — master ( a7700c...c48bfb )
by Abdelrahman
01:33 queued 11s
created

Tenant::isOwner()   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 Rinvex\Support\Traits\HasTranslations;
12
use Rinvex\Support\Traits\ValidatingTrait;
13
use Illuminate\Database\Eloquent\Relations\MorphToMany;
14
15
/**
16
 * Rinvex\Tenants\Models\Tenant.
17
 *
18
 * @property int                                                $id
19
 * @property string                                             $slug
20
 * @property array                                              $title
21
 * @property array                                              $description
22
 * @property string                                             $email
23
 * @property string                                             $website
24
 * @property string                                             $phone
25
 * @property string                                             $language_code
26
 * @property string                                             $country_code
27
 * @property string                                             $state
28
 * @property string                                             $city
29
 * @property string                                             $address
30
 * @property string                                             $postal_code
31
 * @property string                                             $launch_date
32
 * @property string                                             $timezone
33
 * @property string                                             $currency
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 \Rinvex\Country\Country                       $country
40
 * @property-read \Rinvex\Language\Language                     $language
41
 *
42
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereAddress($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereCity($value)
44
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereCountryCode($value)
45
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereCreatedAt($value)
46
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereCurrency($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 whereTimezone($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 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
 * @mixin \Eloquent
63
 */
64
class Tenant extends Model
65
{
66
    use HasSlug;
67
    use HasTranslations;
68
    use ValidatingTrait;
69
    use CacheableEloquent;
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    protected $fillable = [
75
        'slug',
76
        'name',
77
        'description',
78
        'email',
79
        'website',
80
        'phone',
81
        'language_code',
82
        'country_code',
83
        'state',
84
        'city',
85
        'address',
86
        'postal_code',
87
        'launch_date',
88
        'timezone',
89
        'currency',
90
        'is_active',
91
    ];
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    protected $casts = [
97
        'slug' => 'string',
98
        'email' => 'string',
99
        'website' => '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
        'timezone' => 'string',
109
        'currency' => 'string',
110
        'is_active' => 'boolean',
111
        'deleted_at' => 'datetime',
112
    ];
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    protected $observables = [
118
        'validating',
119
        'validated',
120
    ];
121
122
    /**
123
     * The attributes that are translatable.
124
     *
125
     * @var array
126
     */
127
    public $translatable = [
128
        'name',
129
        'description',
130
    ];
131
132
    /**
133
     * The default rules that the model will validate against.
134
     *
135
     * @var array
136
     */
137
    protected $rules = [];
138
139
    /**
140
     * Whether the model should throw a
141
     * ValidationException if it fails validation.
142
     *
143
     * @var bool
144
     */
145
    protected $throwValidationExceptions = true;
146
147
    /**
148
     * Create a new Eloquent model instance.
149
     *
150
     * @param array $attributes
151
     */
152
    public function __construct(array $attributes = [])
153
    {
154
        parent::__construct($attributes);
155
156
        $this->setTable(config('rinvex.tenants.tables.tenants'));
157
        $this->setRules([
158
            'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.tenants.tables.tenants').',slug',
159
            'name' => 'required|string|max:150',
160
            'description' => 'nullable|string|max:10000',
161
            'email' => 'required|email|min:3|max:150|unique:'.config('rinvex.tenants.tables.tenants').',email',
162
            'website' => 'nullable|string|max:150',
163
            'phone' => 'nullable|numeric|phone',
164
            'country_code' => 'required|alpha|size:2|country',
165
            'language_code' => 'required|alpha|size:2|language',
166
            'state' => 'nullable|string',
167
            'city' => 'nullable|string',
168
            'address' => 'nullable|string',
169
            'postal_code' => 'nullable|string',
170
            'launch_date' => 'nullable|date_format:Y-m-d',
171
            'timezone' => 'required|string|timezone',
172
            'currency' => 'required|alpha|size:3',
173
            'is_active' => 'sometimes|boolean',
174
        ]);
175
    }
176
177
    /**
178
     * Get all attached models of the given class to the tenant.
179
     *
180
     * @param string $class
181
     *
182
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
183
     */
184
    public function entries(string $class): MorphToMany
185
    {
186
        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...
187
    }
188
189
    /**
190
     * Get the options for generating the slug.
191
     *
192
     * @return \Spatie\Sluggable\SlugOptions
193
     */
194
    public function getSlugOptions(): SlugOptions
195
    {
196
        return SlugOptions::create()
197
                          ->doNotGenerateSlugsOnUpdate()
198
                          ->generateSlugsFrom('name')
199
                          ->saveSlugsTo('slug');
200
    }
201
202
    /**
203
     * Get the tenant's country.
204
     *
205
     * @return \Rinvex\Country\Country
206
     */
207
    public function getCountryAttribute()
208
    {
209
        return country($this->country_code);
210
    }
211
212
    /**
213
     * Get the tenant's language.
214
     *
215
     * @return \Rinvex\Language\Language
216
     */
217
    public function getLanguageAttribute()
218
    {
219
        return language($this->language_code);
220
    }
221
222
    /**
223
     * Determine if the given model is supermanager of the tenant.
224
     *
225
     * @param \Illuminate\Database\Eloquent\Model $model
226
     *
227
     * @return bool
228
     */
229
    public function isSuperManager(Model $model): bool
230
    {
231
        return $this->isManager($model) && $model->isA('supermanager');
232
    }
233
234
    /**
235
     * Determine if the given model is manager of the tenant.
236
     *
237
     * @param \Illuminate\Database\Eloquent\Model $model
238
     *
239
     * @return bool
240
     */
241
    public function isManager(Model $model): bool
242
    {
243
        return $model->tenants->contains($this);
244
    }
245
246
    /**
247
     * Activate the tenant.
248
     *
249
     * @return $this
250
     */
251
    public function activate()
252
    {
253
        $this->update(['is_active' => true]);
254
255
        return $this;
256
    }
257
258
    /**
259
     * Deactivate the tenant.
260
     *
261
     * @return $this
262
     */
263
    public function deactivate()
264
    {
265
        $this->update(['is_active' => false]);
266
267
        return $this;
268
    }
269
}
270