Completed
Push — develop ( 710708...ec2257 )
by Abdelrahman
05:08
created

Tenant::resolveRouteBinding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Tenants\Models;
6
7
use Rinvex\Tags\Traits\Taggable;
8
use Vinkla\Hashids\Facades\Hashids;
9
use Cortex\Foundation\Traits\Auditable;
10
use Spatie\MediaLibrary\HasMedia\HasMedia;
11
use Spatie\Activitylog\Traits\LogsActivity;
12
use Rinvex\Tenants\Models\Tenant as BaseTenant;
13
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
14
use Illuminate\Database\Eloquent\Relations\MorphToMany;
15
16
/**
17
 * Cortex\Tenants\Models\Tenant.
18
 *
19
 * @property int                                                                           $id
20
 * @property string                                                                        $name
21
 * @property array                                                                         $title
22
 * @property array                                                                         $description
23
 * @property int                                                                           $owner_id
24
 * @property string                                                                        $owner_type
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                                                                        $group
36
 * @property bool                                                                          $is_active
37
 * @property string                                                                        $style
38
 * @property \Carbon\Carbon|null                                                           $created_at
39
 * @property \Carbon\Carbon|null                                                           $updated_at
40
 * @property \Carbon\Carbon|null                                                           $deleted_at
41
 * @property-read \Illuminate\Database\Eloquent\Collection|\Cortex\Foundation\Models\Log[] $activity
42
 * @property-read \Illuminate\Database\Eloquent\Model|\Eloquent                            $owner
43
 *
44
 * @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...
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 whereDeletedAt($value)
50
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereDescription($value)
51
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereEmail($value)
52
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereGroup($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 whereTitle($value)
58
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereOwnerId($value)
59
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant whereOwnerType($value)
60
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant wherePhone($value)
61
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Tenants\Models\Tenant wherePostalCode($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 whereState($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 HasMediaTrait;
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    protected $fillable = [
79
        'name',
80
        'title',
81
        'description',
82
        'owner_id',
83
        'owner_type',
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
        'group',
95
        'style',
96
        'is_active',
97
    ];
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    protected $casts = [
103
        'name' => 'string',
104
        'owner_id' => 'integer',
105
        'owner_type' => '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
        'group' => 'string',
117
        'style' => 'string',
118
        'is_active' => 'boolean',
119
        'deleted_at' => 'datetime',
120
    ];
121
122
    /**
123
     * Indicates whether to log only dirty attributes or all.
124
     *
125
     * @var bool
126
     */
127
    protected static $logOnlyDirty = true;
128
129
    /**
130
     * The attributes that are logged on change.
131
     *
132
     * @var array
133
     */
134
    protected static $logFillable = true;
135
136
    /**
137
     * The attributes that are ignored on change.
138
     *
139
     * @var array
140
     */
141
    protected static $ignoreChangedAttributes = [
142
        'created_at',
143
        'updated_at',
144
        'deleted_at',
145
    ];
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
            'name' => 'required|alpha_dash|max:150|unique:'.config('rinvex.tenants.tables.tenants').',name',
159
            'title' => 'required|string|max:150',
160
            'description' => 'nullable|string|max:10000',
161
            'owner_id' => 'required|integer',
162
            'owner_type' => 'required|string',
163
            'email' => 'required|email|min:3|max:150|unique:'.config('rinvex.tenants.tables.tenants').',email',
164
            'website' => 'nullable|string|max:150',
165
            'phone' => 'nullable|numeric|phone',
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
            'group' => 'nullable|string|max:150',
174
            'style' => 'nullable|string|max:150',
175
            'is_active' => 'sometimes|boolean',
176
            'tags' => 'nullable|array',
177
        ]);
178
    }
179
180
    /**
181
     * Get the value of the model's route key.
182
     *
183
     * @return mixed
184
     */
185
    public function getRouteKey()
186
    {
187
        return Hashids::encode($this->getAttribute($this->getRouteKeyName()));
188
    }
189
190
    /**
191
     * Retrieve the model for a bound value.
192
     *
193
     * @param  mixed  $value
194
     * @return \Illuminate\Database\Eloquent\Model|null
195
     */
196
    public function resolveRouteBinding($value)
197
    {
198
        $value = Hashids::decode($value)[0];
199
200
        return $this->where($this->getRouteKeyName(), $value)->first();
201
    }
202
203
    /**
204
     * Register media collections.
205
     *
206
     * @return void
207
     */
208
    public function registerMediaCollections(): void
209
    {
210
        $this->addMediaCollection('profile_picture')->singleFile();
211
        $this->addMediaCollection('cover_photo')->singleFile();
212
    }
213
214
    /**
215
     * Get all attached managers to tenant.
216
     *
217
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
218
     */
219
    public function managers(): MorphToMany
220
    {
221
        return $this->entries(config('cortex.auth.models.manager'));
222
    }
223
}
224