Completed
Push — develop ( a6a3eb...0c87ce )
by Abdelrahman
01:32
created

Tenant::owner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
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 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                                                                        $timezone
37
 * @property string                                                                        $currency
38
 * @property string                                                                        $social
39
 * @property bool                                                                          $is_active
40
 * @property string                                                                        $style
41
 * @property \Carbon\Carbon|null                                                           $created_at
42
 * @property \Carbon\Carbon|null                                                           $updated_at
43
 * @property \Carbon\Carbon|null                                                           $deleted_at
44
 * @property-read \Illuminate\Database\Eloquent\Collection|\Cortex\Foundation\Models\Log[] $activity
45
 *
46
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereAddress($value)
47
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereCity($value)
48
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereCountryCode($value)
49
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereCreatedAt($value)
50
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereCurrency($value)
51
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereDeletedAt($value)
52
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereDescription($value)
53
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereEmail($value)
54
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereId($value)
55
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereIsActive($value)
56
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereLanguageCode($value)
57
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereLaunchDate($value)
58
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereName($value)
59
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant wherePhone($value)
60
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant wherePostalCode($value)
61
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereSlug($value)
62
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereSocial($value)
63
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereState($value)
64
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereTimezone($value)
65
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereUpdatedAt($value)
66
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereStyle($value)
67
 * @mixin \Eloquent
68
 */
69
class Tenant extends BaseTenant implements HasMedia
70
{
71
    use Taggable;
72
    use Auditable;
73
    use LogsActivity;
74
    use HashidsTrait;
75
    use HasMediaTrait;
76
    use HasSocialAttributes;
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    protected $fillable = [
82
        'slug',
83
        'name',
84
        'description',
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
        'social',
98
        'style',
99
        'is_active',
100
    ];
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    protected $casts = [
106
        'slug' => '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
        'social' => 'array',
120
        'style' => 'string',
121
        'is_active' => 'boolean',
122
        'deleted_at' => 'datetime',
123
    ];
124
125
    /**
126
     * Indicates whether to log only dirty attributes or all.
127
     *
128
     * @var bool
129
     */
130
    protected static $logOnlyDirty = true;
131
132
    /**
133
     * The attributes that are logged on change.
134
     *
135
     * @var array
136
     */
137
    protected static $logFillable = true;
138
139
    /**
140
     * The attributes that are ignored on change.
141
     *
142
     * @var array
143
     */
144
    protected static $ignoreChangedAttributes = [
145
        'created_at',
146
        'updated_at',
147
        'deleted_at',
148
    ];
149
150
    /**
151
     * Create a new Eloquent model instance.
152
     *
153
     * @param array $attributes
154
     */
155
    public function __construct(array $attributes = [])
156
    {
157
        parent::__construct($attributes);
158
159
        $this->setTable(config('rinvex.tenants.tables.tenants'));
160
        $this->setRules([
161
            'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.tenants.tables.tenants').',slug',
162
            'name' => 'required|string|max:150',
163
            'description' => 'nullable|string|max:10000',
164
            'email' => 'required|email|min:3|max:150|unique:'.config('rinvex.tenants.tables.tenants').',email',
165
            'website' => 'nullable|string|max:150',
166
            'phone' => 'required|phone:AUTO',
167
            'country_code' => 'required|alpha|size:2|country',
168
            'language_code' => 'required|alpha|size:2|language',
169
            'state' => 'nullable|string',
170
            'city' => 'nullable|string',
171
            'address' => 'nullable|string',
172
            'postal_code' => 'nullable|string',
173
            'launch_date' => 'nullable|date_format:Y-m-d',
174
            'timezone' => 'required|string|timezone',
175
            'currency' => 'required|string|size:3',
176
            'social' => 'nullable',
177
            'style' => 'nullable|string|max:150',
178
            'is_active' => 'sometimes|boolean',
179
            'tags' => 'nullable|array',
180
        ]);
181
    }
182
183
    /**
184
     * Register media collections.
185
     *
186
     * @return void
187
     */
188
    public function registerMediaCollections(): void
189
    {
190
        $this->addMediaCollection('profile_picture')->singleFile();
191
        $this->addMediaCollection('cover_photo')->singleFile();
192
    }
193
194
    /**
195
     * Get all attached managers to tenant.
196
     *
197
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
198
     */
199
    public function managers(): MorphToMany
200
    {
201
        return $this->entries(config('cortex.auth.models.manager'));
202
    }
203
204
    /**
205
     * Get the route key for the model.
206
     *
207
     * @return string
208
     */
209
    public function getRouteKeyName()
210
    {
211
        return 'slug';
212
    }
213
}
214