This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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
|
|||
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 |
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.