Contact   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

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

11 Methods

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