Contact   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 231
Duplicated Lines 5.19 %

Coupling/Cohesion

Components 4
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 25
lcom 4
cbo 1
dl 12
loc 231
ccs 41
cts 41
cp 1
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A user() 0 4 1
A businesses() 0 4 1
A appointments() 0 4 1
A hasAppointment() 0 4 1
A appointmentsCount() 0 7 1
A getAppointmentsCountAttribute() 12 12 3
A getPresenterClass() 0 4 1
A setMobileAttribute() 0 4 2
A setMobileCountryAttribute() 0 4 2
A setBirthdateAttribute() 0 8 2
A setEmailAttribute() 0 4 2
A setNinAttribute() 0 4 2
A getEmailAttribute() 0 12 3
A isSubscribedTo() 0 4 1
A isProfileOf() 0 4 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Timegridio\Concierge\Models;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Model as EloquentModel;
7
use McCool\LaravelAutoPresenter\HasPresenter;
8
use Timegridio\Concierge\Presenters\ContactPresenter;
9
10
/**
11
 * @property int $id
12
 * @property string $firstname
13
 * @property string $lastname
14
 * @property string $nin
15
 * @property string $email
16
 * @property \Carbon\Carbon $birthdate
17
 * @property string $mobile
18
 * @property string $mobile_country
19
 * @property string $gender
20
 * @property string $occupation
21
 * @property string $martial_status
22
 * @property string $postal_address
23
 * @property Illuminate\Support\Collection $businesses
24
 * @property Illuminate\Support\Collection $appointments
25
 * @property mixed $user
26
 * @property int $appointmentsCount
27
 */
28
class Contact extends EloquentModel implements HasPresenter
29
{
30
    /**
31
     * The attributes that are mass assignable.
32
     *
33
     * @var array
34
     */
35
    protected $fillable = [
36
        'firstname',
37
        'lastname',
38
        'nin',
39
        'email',
40
        'birthdate',
41
        'mobile',
42
        'mobile_country',
43
        'gender',
44
        'occupation',
45
        'martial_status',
46
        'postal_address',
47
        ];
48
49
    /**
50
     * The attributes that should be mutated to dates.
51
     *
52
     * @var array
53
     */
54
    protected $dates = ['birthdate'];
55
56
    //////////////////
57
    // Relationship //
58
    //////////////////
59
60
    /**
61
     * is profile of User.
62
     *
63
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo Relationship Contact belongs to User query
64
     */
65 12
    public function user()
66
    {
67 12
        return $this->belongsTo(config('auth.providers.users.model'));
68
    }
69
70
    /**
71
     * belongs to Business.
72
     *
73
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany Relationship Contact is part of Businesses addressbooks query
74
     */
75 7
    public function businesses()
76
    {
77 7
        return $this->belongsToMany(Business::class);
78
    }
79
80
    /**
81
     * has Appointments.
82
     *
83
     * @return \Illuminate\Database\Eloquent\Relations\HasMany Relationship Contact has booked Appointments query
84
     */
85 3
    public function appointments()
86
    {
87 3
        return $this->hasMany(Appointment::class);
88
    }
89
90
    /////////////////////
91
    // Soft Attributes //
92
    /////////////////////
93
94
    /**
95
     * has Appointment.
96
     *
97
     * @return bool Check if Contact has at least one Appointment booked
98
     */
99 1
    public function hasAppointment()
100
    {
101 1
        return $this->appointmentsCount > 0;
102
    }
103
104
    /**
105
     * Appointments Count.
106
     *
107
     * This method is used to optimize the relationship counting performance
108
     *
109
     * @return Illuminate\Database\Query Relationship Contact x Appointment count(*) query
110
     */
111 1
    public function appointmentsCount()
112
    {
113
        return $this
114 1
            ->hasMany(Appointment::class)
115 1
            ->selectRaw('contact_id, count(*) as aggregate')
116 1
            ->groupBy('contact_id');
117
    }
118
119
    /**
120
     * get AppointmentsCount.
121
     *
122
     * @return int Count of Appointments held by this Contact
123
     */
124 1 View Code Duplication
    public function getAppointmentsCountAttribute()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
    {
126
        // If relation is not loaded already, let's do it first
127 1
        if (!array_key_exists('appointmentsCount', $this->relations)) {
128 1
            $this->load('appointmentsCount');
129
        }
130
131 1
        $related = $this->getRelation('appointmentsCount');
132
133
        // Return the count directly
134 1
        return ($related->count() > 0) ? (int) $related->first()->aggregate : 0;
135
    }
136
137
    ///////////////
138
    // Presenter //
139
    ///////////////
140
141
    /**
142
     * get presenter.
143
     *
144
     * @return ContactPresenter Presenter class
145
     */
146 2
    public function getPresenterClass()
147
    {
148 2
        return ContactPresenter::class;
149
    }
150
151
    //////////////
152
    // Mutators //
153
    //////////////
154
155
    /**
156
     * set Mobile.
157
     *
158
     * Expected phone number is international format numeric only
159
     *
160
     * @param string $mobile Mobile phone number
161
     */
162 77
    public function setMobileAttribute($mobile)
163
    {
164 77
        return $this->attributes['mobile'] = trim($mobile) ?: null;
165
    }
166
167
    /**
168
     * set Mobile Country.
169
     *
170
     * @param string $country Country ISO Code ALPHA-2
171
     */
172 77
    public function setMobileCountryAttribute($country)
173
    {
174 77
        return $this->attributes['mobile_country'] = trim($country) ?: null;
175
    }
176
177
    /**
178
     * set Birthdate.
179
     *
180
     * @param Carbon $birthdate Carbon parseable birth date
181
     */
182 77
    public function setBirthdateAttribute(Carbon $birthdate = null)
183
    {
184 77
        if ($birthdate === null) {
185 2
            return $this->attributes['birthdate'] = null;
186
        }
187
188 77
        return $this->attributes['birthdate'] = $birthdate;
189
    }
190
191
    /**
192
     * set Email.
193
     *
194
     * @param string $email Valid email address
195
     */
196 77
    public function setEmailAttribute($email)
197
    {
198 77
        return $this->attributes['email'] = empty(trim($email)) ? null : $email;
199
    }
200
201
    /**
202
     * TODO: Check if possible to handle in a more structured way
203
     *       NIN record is currently too flexible.
204
     *
205
     * set NIN: National Identity Number
206
     *
207
     * @param string $nin The national identification number in any format
208
     */
209 77
    public function setNinAttribute($nin)
210
    {
211 77
        return $this->attributes['nin'] = empty(trim($nin)) ? null : $nin;
212
    }
213
214
    ///////////////
215
    // ACCESSORS //
216
    ///////////////
217
218 7
    public function getEmailAttribute()
219
    {
220 7
        if ($email = array_get($this->attributes, 'email')) {
221 6
            return $email;
222
        }
223
224 2
        if ($this->user) {
225 1
            return $this->user->email;
226
        }
227
228 1
        return;
229
    }
230
231
    /////////////////////
232
    // Soft Attributes //
233
    /////////////////////
234
235
    /**
236
     * is Subscribed To Business.
237
     *
238
     * @param int $businessId Business of inquiry
239
     *
240
     * @return bool The Contact belongs to the inquired Business' addressbook
241
     */
242 1
    public function isSubscribedTo($businessId)
243
    {
244 1
        return $this->businesses->contains($businessId);
245
    }
246
247
    /**
248
     * is Profile of User.
249
     *
250
     * @param int $userId User of inquiry
251
     *
252
     * @return bool The Contact belongs to the inquired User
253
     */
254 1
    public function isProfileOf($userId)
255
    {
256 1
        return $this->user ? $this->user->id == $userId : false;
257
    }
258
}
259