Completed
Push — master ( 65c4cc...0d5186 )
by Abdelrahman
03:21 queued 02:04
created

Tenant::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 29
rs 9.456
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Tenants\Models;
6
7
use Cortex\Auth\Models\Manager;
8
use Rinvex\Tags\Traits\Taggable;
9
use Cortex\Foundation\Traits\Auditable;
10
use Rinvex\Support\Traits\HashidsTrait;
11
use Spatie\MediaLibrary\HasMedia\HasMedia;
12
use Spatie\Activitylog\Traits\LogsActivity;
13
use Rinvex\Support\Traits\HasSocialAttributes;
14
use Cortex\Foundation\Relations\BelongsToMorph;
15
use Rinvex\Tenants\Models\Tenant as BaseTenant;
16
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
17
use Illuminate\Database\Eloquent\Relations\MorphToMany;
18
19
/**
20
 * Cortex\Tenants\Models\Tenant.
21
 *
22
 * @property int                                                                           $id
23
 * @property string                                                                        $slug
24
 * @property array                                                                         $name
25
 * @property array                                                                         $description
26
 * @property int                                                                           $owner_id
27
 * @property string                                                                        $owner_type
28
 * @property string                                                                        $email
29
 * @property string                                                                        $website
30
 * @property string                                                                        $phone
31
 * @property string                                                                        $language_code
32
 * @property string                                                                        $country_code
33
 * @property string                                                                        $state
34
 * @property string                                                                        $city
35
 * @property string                                                                        $address
36
 * @property string                                                                        $postal_code
37
 * @property string                                                                        $launch_date
38
 * @property string                                                                        $timezone
39
 * @property string                                                                        $currency
40
 * @property string                                                                        $social
41
 * @property bool                                                                          $is_active
42
 * @property string                                                                        $style
43
 * @property \Carbon\Carbon|null                                                           $created_at
44
 * @property \Carbon\Carbon|null                                                           $updated_at
45
 * @property \Carbon\Carbon|null                                                           $deleted_at
46
 * @property-read \Illuminate\Database\Eloquent\Collection|\Cortex\Foundation\Models\Log[] $activity
47
 * @property-read \Cortex\Auth\Models\User|\Illuminate\Database\Eloquent\Model|\Eloquent   $owner
48
 *
49
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\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...
50
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereAddress($value)
51
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereCity($value)
52
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereCountryCode($value)
53
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereCreatedAt($value)
54
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereCurrency($value)
55
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereDeletedAt($value)
56
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereDescription($value)
57
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereEmail($value)
58
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereId($value)
59
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereIsActive($value)
60
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereLanguageCode($value)
61
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereLaunchDate($value)
62
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereName($value)
63
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereOwnerId($value)
64
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereOwnerType($value)
65
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant wherePhone($value)
66
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant wherePostalCode($value)
67
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereSlug($value)
68
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereSocial($value)
69
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereState($value)
70
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereTimezone($value)
71
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereUpdatedAt($value)
72
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereStyle($value)
73
 * @mixin \Eloquent
74
 */
75
class Tenant extends BaseTenant implements HasMedia
76
{
77
    use Taggable;
78
    use Auditable;
79
    use LogsActivity;
80
    use HashidsTrait;
81
    use HasMediaTrait;
82
    use HasSocialAttributes;
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    protected $fillable = [
88
        'slug',
89
        'name',
90
        'description',
91
        'owner_id',
92
        'owner_type',
93
        'email',
94
        'website',
95
        'phone',
96
        'language_code',
97
        'country_code',
98
        'state',
99
        'city',
100
        'address',
101
        'postal_code',
102
        'launch_date',
103
        'timezone',
104
        'currency',
105
        'social',
106
        'style',
107
        'is_active',
108
    ];
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    protected $casts = [
114
        'slug' => 'string',
115
        'owner_id' => 'integer',
116
        'owner_type' => 'string',
117
        'email' => 'string',
118
        'website' => 'string',
119
        'phone' => 'string',
120
        'country_code' => 'string',
121
        'language_code' => 'string',
122
        'state' => 'string',
123
        'city' => 'string',
124
        'address' => 'string',
125
        'postal_code' => 'string',
126
        'launch_date' => 'string',
127
        'timezone' => 'string',
128
        'currency' => 'string',
129
        'social' => 'array',
130
        'style' => 'string',
131
        'is_active' => 'boolean',
132
        'deleted_at' => 'datetime',
133
    ];
134
135
    /**
136
     * Indicates whether to log only dirty attributes or all.
137
     *
138
     * @var bool
139
     */
140
    protected static $logOnlyDirty = true;
141
142
    /**
143
     * The attributes that are logged on change.
144
     *
145
     * @var array
146
     */
147
    protected static $logFillable = true;
148
149
    /**
150
     * The attributes that are ignored on change.
151
     *
152
     * @var array
153
     */
154
    protected static $ignoreChangedAttributes = [
155
        'created_at',
156
        'updated_at',
157
        'deleted_at',
158
    ];
159
160
    /**
161
     * Create a new Eloquent model instance.
162
     *
163
     * @param array $attributes
164
     */
165
    public function __construct(array $attributes = [])
166
    {
167
        parent::__construct($attributes);
168
169
        $this->setTable(config('rinvex.tenants.tables.tenants'));
170
        $this->setRules([
171
            'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.tenants.tables.tenants').',slug',
172
            'name' => 'required|string|max:150',
173
            'description' => 'nullable|string|max:10000',
174
            'owner_id' => 'required|integer',
175
            'owner_type' => 'required|string',
176
            'email' => 'required|email|min:3|max:150|unique:'.config('rinvex.tenants.tables.tenants').',email',
177
            'website' => 'nullable|string|max:150',
178
            'phone' => 'required|phone:AUTO',
179
            'country_code' => 'required|alpha|size:2|country',
180
            'language_code' => 'required|alpha|size:2|language',
181
            'state' => 'nullable|string',
182
            'city' => 'nullable|string',
183
            'address' => 'nullable|string',
184
            'postal_code' => 'nullable|string',
185
            'launch_date' => 'nullable|date_format:Y-m-d',
186
            'timezone' => 'required|string|timezone',
187
            'currency' => 'required|string|size:3',
188
            'social' => 'nullable',
189
            'style' => 'nullable|string|max:150',
190
            'is_active' => 'sometimes|boolean',
191
            'tags' => 'nullable|array',
192
        ]);
193
    }
194
195
    /**
196
     * Register media collections.
197
     *
198
     * @return void
199
     */
200
    public function registerMediaCollections(): void
201
    {
202
        $this->addMediaCollection('profile_picture')->singleFile();
203
        $this->addMediaCollection('cover_photo')->singleFile();
204
    }
205
206
    /**
207
     * Get all attached managers to tenant.
208
     *
209
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
210
     */
211
    public function managers(): MorphToMany
212
    {
213
        return $this->entries(config('cortex.auth.models.manager'));
214
    }
215
216
    /**
217
     * {@inheritdoc}
218
     */
219
    public function owner()
220
    {
221
        return BelongsToMorph::build($this, Manager::class, 'owner');
222
    }
223
224
    /**
225
     * Get the route key for the model.
226
     *
227
     * @return string
228
     */
229
    public function getRouteKeyName()
230
    {
231
        return 'slug';
232
    }
233
}
234