Completed
Push — master ( 1a7394...cd7830 )
by Abdelrahman
01:17
created

Contact   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 244
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 6
dl 0
loc 244
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getNameAttribute() 0 10 1
A entity() 0 4 1
A setSourceAttribute() 0 4 1
A setMethodAttribute() 0 4 1
A scopeCountry() 0 4 1
A scopeLanguage() 0 4 1
A scopeSource() 0 4 1
A scopeMethod() 0 4 1
A relatives() 0 5 1
A backRelatives() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Contacts\Models;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Rinvex\Cacheable\CacheableEloquent;
9
use Illuminate\Database\Eloquent\Builder;
10
use Rinvex\Support\Traits\ValidatingTrait;
11
use Illuminate\Database\Eloquent\Relations\MorphTo;
12
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
13
14
/**
15
 * Rinvex\Contacts\Models\Contact.
16
 *
17
 * @property int                                                                             $id
18
 * @property int                                                                             $entity_id
19
 * @property string                                                                          $entity_type
20
 * @property string                                                                          $source
21
 * @property string                                                                          $method
22
 * @property string                                                                          $name_prefix
23
 * @property string                                                                          $first_name
24
 * @property string                                                                          $middle_name
25
 * @property string                                                                          $last_name
26
 * @property string                                                                          $name_suffix
27
 * @property string                                                                          $title
28
 * @property string                                                                          $email
29
 * @property string                                                                          $phone
30
 * @property string                                                                          $fax
31
 * @property string                                                                          $skype
32
 * @property string                                                                          $twitter
33
 * @property string                                                                          $facebook
34
 * @property string                                                                          $google_plus
35
 * @property string                                                                          $linkedin
36
 * @property string                                                                          $country_code
37
 * @property string                                                                          $language_code
38
 * @property string                                                                          $birthday
39
 * @property string                                                                          $gender
40
 * @property \Carbon\Carbon|null                                                             $created_at
41
 * @property \Carbon\Carbon|null                                                             $updated_at
42
 * @property-read \Illuminate\Database\Eloquent\Collection|\Rinvex\Contacts\Models\Contact[] $backRelatives
43
 * @property-read \Illuminate\Database\Eloquent\Model|\Eloquent                              $entity
44
 * @property-read string                                                                     $name
45
 * @property-read \Illuminate\Database\Eloquent\Collection|\Rinvex\Contacts\Models\Contact[] $relatives
46
 *
47
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact country($countryCode)
48
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact language($languageCode)
49
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact method($method)
50
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact source($source)
51
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereBirthday($value)
52
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereCountryCode($value)
53
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereCreatedAt($value)
54
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereEmail($value)
55
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereEntityId($value)
56
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereEntityType($value)
57
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereFacebook($value)
58
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereFax($value)
59
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereFirstName($value)
60
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereGender($value)
61
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereGooglePlus($value)
62
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereId($value)
63
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereLanguageCode($value)
64
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereLastName($value)
65
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereLinkedin($value)
66
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereMethod($value)
67
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereMiddleName($value)
68
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereNamePrefix($value)
69
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereNameSuffix($value)
70
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact wherePhone($value)
71
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereSkype($value)
72
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereSource($value)
73
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereTitle($value)
74
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereTwitter($value)
75
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereUpdatedAt($value)
76
 * @mixin \Eloquent
77
 */
78
class Contact extends Model
79
{
80
    use ValidatingTrait;
81
    use CacheableEloquent;
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    protected $fillable = [
87
        'entity_id',
88
        'entity_type',
89
        'source',
90
        'method',
91
        'name_prefix',
92
        'first_name',
93
        'middle_name',
94
        'last_name',
95
        'name_suffix',
96
        'title',
97
        'email',
98
        'phone',
99
        'fax',
100
        'skype',
101
        'twitter',
102
        'facebook',
103
        'google_plus',
104
        'linkedin',
105
        'country_code',
106
        'language_code',
107
        'birthday',
108
        'gender',
109
    ];
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    protected $casts = [
115
        'entity_id' => 'integer',
116
        'entity_type' => 'string',
117
        'source' => 'string',
118
        'method' => 'string',
119
        'name_prefix' => 'string',
120
        'first_name' => 'string',
121
        'middle_name' => 'string',
122
        'last_name' => 'string',
123
        'name_suffix' => 'string',
124
        'title' => 'string',
125
        'email' => 'string',
126
        'phone' => 'string',
127
        'fax' => 'string',
128
        'skype' => 'string',
129
        'twitter' => 'string',
130
        'facebook' => 'string',
131
        'google_plus' => 'string',
132
        'linkedin' => 'string',
133
        'country_code' => 'string',
134
        'language_code' => 'string',
135
        'birthday' => 'string',
136
        'gender' => 'string',
137
        'deleted_at' => 'datetime',
138
    ];
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    protected $observables = [
144
        'validating',
145
        'validated',
146
    ];
147
148
    /**
149
     * The default rules that the model will validate against.
150
     *
151
     * @var array
152
     */
153
    protected $rules = [
154
        'entity_id' => 'required|integer',
155
        'entity_type' => 'required|string|max:150',
156
        'source' => 'required|string|max:150',
157
        'method' => 'nullable|string|max:150',
158
        'name_prefix' => 'nullable|string|max:150',
159
        'first_name' => 'required|string|max:150',
160
        'middle_name' => 'nullable|string|max:150',
161
        'last_name' => 'nullable|string|max:150',
162
        'name_suffix' => 'nullable|string|max:150',
163
        'title' => 'nullable|string|max:150',
164
        'email' => 'nullable|email|min:3|max:150',
165
        'phone' => 'nullable|numeric|min:4',
166
        'fax' => 'nullable|string|max:150',
167
        'skype' => 'nullable|string|max:150',
168
        'twitter' => 'nullable|string|max:150',
169
        'facebook' => 'nullable|string|max:150',
170
        'google_plus' => 'nullable|string|max:150',
171
        'linkedin' => 'nullable|string|max:150',
172
        'country_code' => 'nullable|alpha|size:2|country',
173
        'language_code' => 'nullable|alpha|size:2|language',
174
        'birthday' => 'nullable|date_format:Y-m-d',
175
        'gender' => 'nullable|string|in:male,female',
176
    ];
177
178
    /**
179
     * Whether the model should throw a
180
     * ValidationException if it fails validation.
181
     *
182
     * @var bool
183
     */
184
    protected $throwValidationExceptions = true;
185
186
    /**
187
     * Create a new Eloquent model instance.
188
     *
189
     * @param array $attributes
190
     */
191
    public function __construct(array $attributes = [])
192
    {
193
        parent::__construct($attributes);
194
195
        $this->setTable(config('rinvex.contacts.tables.contacts'));
196
    }
197
198
    /**
199
     * Get the contact name.
200
     *
201
     * @return string
202
     */
203
    public function getNameAttribute(): string
204
    {
205
        return trim(collect([
206
            $this->name_prefix,
207
            $this->first_name,
208
            $this->middle_name,
209
            $this->last_name,
210
            $this->name_suffix,
211
        ])->implode(' '));
212
    }
213
214
    /**
215
     * Get the owner model of the contact.
216
     *
217
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
218
     */
219
    public function entity(): MorphTo
220
    {
221
        return $this->morphTo();
222
    }
223
224
    /**
225
     * Enforce clean sources.
226
     *
227
     * @param string $value
228
     *
229
     * @return void
230
     */
231
    public function setSourceAttribute($value): void
232
    {
233
        $this->attributes['source'] = str_slug($value);
234
    }
235
236
    /**
237
     * Enforce clean methods.
238
     *
239
     * @param string $value
240
     *
241
     * @return void
242
     */
243
    public function setMethodAttribute($value): void
244
    {
245
        $this->attributes['method'] = str_slug($value);
246
    }
247
248
    /**
249
     * Scope contacts by the given country.
250
     *
251
     * @param \Illuminate\Database\Eloquent\Builder $builder
252
     * @param string                                $countryCode
253
     *
254
     * @return \Illuminate\Database\Eloquent\Builder
255
     */
256
    public function scopeCountry(Builder $builder, string $countryCode): Builder
257
    {
258
        return $builder->where('country_code', $countryCode);
259
    }
260
261
    /**
262
     * Scope contacts by the given language.
263
     *
264
     * @param \Illuminate\Database\Eloquent\Builder $builder
265
     * @param string                                $languageCode
266
     *
267
     * @return \Illuminate\Database\Eloquent\Builder
268
     */
269
    public function scopeLanguage(Builder $builder, string $languageCode): Builder
270
    {
271
        return $builder->where('language_code', $languageCode);
272
    }
273
274
    /**
275
     * Scope contacts by the given source.
276
     *
277
     * @param \Illuminate\Database\Eloquent\Builder $builder
278
     * @param string                                $source
279
     *
280
     * @return \Illuminate\Database\Eloquent\Builder
281
     */
282
    public function scopeSource(Builder $builder, string $source): Builder
283
    {
284
        return $builder->where('source', $source);
285
    }
286
287
    /**
288
     * Scope contacts by the given method.
289
     *
290
     * @param \Illuminate\Database\Eloquent\Builder $builder
291
     * @param string                                $method
292
     *
293
     * @return \Illuminate\Database\Eloquent\Builder
294
     */
295
    public function scopeMethod(Builder $builder, string $method): Builder
296
    {
297
        return $builder->where('method', $method);
298
    }
299
300
    /**
301
     * A contact may have multiple related contacts.
302
     *
303
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
304
     */
305
    public function relatives(): BelongsToMany
306
    {
307
        return $this->belongsToMany(self::class, config('rinvex.contacts.tables.contact_relations'), 'contact_id', 'related_id')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 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...
308
                    ->withPivot('relation')->withTimestamps();
309
    }
310
311
    /**
312
     * A contact may be related to multiple contacts.
313
     *
314
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
315
     */
316
    public function backRelatives(): BelongsToMany
317
    {
318
        return $this->belongsToMany(self::class, config('rinvex.contacts.tables.contact_relations'), 'related_id', 'contact_id')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 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...
319
                    ->withPivot('relation')->withTimestamps();
320
    }
321
}
322