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