Passed
Push — master ( a3cc07...eb52df )
by Nashwan
03:50
created

Donor::getGenderTitle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Donor entity class file
4
 *
5
 * @package    EBloodBank
6
 * @subpackage Models
7
 * @since      1.0
8
 */
9
namespace EBloodBank\Models;
10
11
use DateTime;
12
use DateTimeZone;
13
use InvalidArgumentException;
14
use EBloodBank as EBB;
15
use EBloodBank\Traits\EntityMeta;
16
17
/**
18
 * Donor entity class
19
 *
20
 * @since 1.0
21
 *
22
 * @Entity(repositoryClass="EBloodBank\Models\DonorRepository")
23
 * @Table(name="donor")
24
 * @HasLifecycleCallbacks
25
 */
26
class Donor extends Entity
27
{
28
    use EntityMeta;
29
30
    /**
31
     * Donor ID
32
     * 
33
     * @var   int
34
     * @since 1.0
35
     *
36
     * @Id
37
     * @GeneratedValue
38
     * @Column(type="integer", name="donor_id")
39
     */
40
    protected $id = 0;
41
42
    /**
43
     * Donor name
44
     * 
45
     * @var   string
46
     * @since 1.0
47
     *
48
     * @Column(type="string", name="donor_name")
49
     */
50
    protected $name;
51
52
    /**
53
     * Donor gender
54
     * 
55
     * @var   string
56
     * @since 1.0
57
     *
58
     * @Column(type="string", name="donor_gender")
59
     */
60
    protected $gender;
61
62
    /**
63
     * Donor birthdate
64
     * 
65
     * @var   string
66
     * @since 1.0
67
     *
68
     * @Column(type="string", name="donor_birthdate")
69
     */
70
    protected $birthdate;
71
72
    /**
73
     * Donor blood group
74
     * 
75
     * @var   string
76
     * @since 1.0
77
     *
78
     * @Column(type="string", name="donor_blood_group")
79
     */
80
    protected $blood_group;
81
82
    /**
83
     * Donor district
84
     * 
85
     * @var   District
86
     * @since 1.0
87
     *
88
     * @ManyToOne(targetEntity="EBloodBank\Models\District", inversedBy="donors")
89
     * @JoinColumn(name="donor_district_id", referencedColumnName="district_id")
90
     */
91
    protected $district;
92
93
    /**
94
     * Donor creation datetime
95
     * 
96
     * @var   string
97
     * @since 1.0
98
     *
99
     * @Column(type="datetime", name="donor_created_at")
100
     */
101
    protected $created_at;
102
103
    /**
104
     * Donor created by
105
     * 
106
     * @var   User
107
     * @since 1.0
108
     *
109
     * @ManyToOne(targetEntity="EBloodBank\Models\User")
110
     * @JoinColumn(name="donor_created_by", referencedColumnName="user_id")
111
     */
112
    protected $created_by;
113
114
    /**
115
     * Donor status
116
     * 
117
     * @var   string
118
     * @since 1.0
119
     *
120
     * @Column(type="string", name="donor_status")
121
     */
122
    protected $status;
123
124
    /**
125
     * Donor meta
126
     * 
127
     * @var   array
128
     * @since 1.0
129
     *
130
     * @Column(type="json", name="donor_meta")
131
     */
132
    protected $meta = [];
133
134
    /**
135
     * @return bool
136
     * @since 1.0
137
     */
138
    public function isExists()
139
    {
140
        $id = (int) $this->get('id');
141
        return ! empty($id);
142
    }
143
144
    /**
145
     * @return bool
146
     * @since 1.0
147
     */
148
    public function isPending()
149
    {
150
        return 'pending' === $this->get('status');
151
    }
152
153
    /**
154
     * @return bool
155
     * @since 1.0
156
     */
157
    public function isApproved()
158
    {
159
        return 'approved' === $this->get('status');
160
    }
161
162
    /**
163
     * @return string
164
     * @since 1.0
165
     */
166
    public function getGenderTitle()
167
    {
168
        $genders = EBB\getGenders();
0 ignored issues
show
Bug introduced by
The function getGenders was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

168
        $genders = /** @scrutinizer ignore-call */ EBB\getGenders();
Loading history...
169
        $gender = $this->get('gender');
170
        if (isset($genders[$gender])) {
171
            $gender = $genders[$gender];
172
        }
173
        return $gender;
174
    }
175
176
    /**
177
     * @return string
178
     * @since 1.0
179
     */
180
    public function getAge($format = '%y')
181
    {
182
        $currentDate = new DateTime(date('Y-m-d'));
183
        $birthdate = new DateTime($this->get('birthdate'));
184
185
        if ($birthdate > $currentDate) {
186
            return 0;
187
        }
188
189
        return $currentDate->diff($birthdate)->format($format);
190
    }
191
192
    /**
193
     * @return void
194
     * @since  1.6
195
     * 
196
     * @PrePersist
197
     */
198
    public function doActionOnPrePersist()
199
    {
200
        $this->set('created_at', new DateTime('now', new DateTimeZone('UTC')));
201
    }
202
203
    /**
204
     * @return mixed
205
     * @since 1.0
206
     * @static
207
     */
208
    public static function sanitize($key, $value)
209
    {
210
        switch ($key) {
211
            case 'id':
212
                $value = EBB\sanitizeInteger($value);
0 ignored issues
show
Bug introduced by
The function sanitizeInteger was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

212
                $value = /** @scrutinizer ignore-call */ EBB\sanitizeInteger($value);
Loading history...
213
                break;
214
            case 'name':
215
                $value = EBB\sanitizeTitle($value);
0 ignored issues
show
Bug introduced by
The function sanitizeTitle was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

215
                $value = /** @scrutinizer ignore-call */ EBB\sanitizeTitle($value);
Loading history...
216
                break;
217
            case 'gender':
218
            case 'status':
219
            case 'birthdate':
220
            case 'blood_group':
221
                $value = EBB\sanitizeSlug($value);
0 ignored issues
show
Bug introduced by
The function sanitizeSlug was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

221
                $value = /** @scrutinizer ignore-call */ EBB\sanitizeSlug($value);
Loading history...
222
                break;
223
        }
224
225
        return $value;
226
    }
227
228
    /**
229
     * @throws \InvalidArgumentException
230
     * @return bool
231
     * @since 1.0
232
     * @static
233
     */
234
    public static function validate($key, $value)
235
    {
236
        switch ($key) {
237
            case 'id':
238
                if (! EBB\isValidID($value)) {
0 ignored issues
show
Bug introduced by
The function isValidID was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

238
                if (! /** @scrutinizer ignore-call */ EBB\isValidID($value)) {
Loading history...
239
                    throw new InvalidArgumentException(__('Invalid donor ID.'));
240
                }
241
                break;
242
            case 'name':
243
                if (! is_string($value) || empty($value)) {
244
                    throw new InvalidArgumentException(__('Invalid donor name.'));
245
                }
246
                break;
247
            case 'gender':
248
                if (! array_key_exists($value, EBB\getGenders())) {
0 ignored issues
show
Bug introduced by
The function getGenders was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

248
                if (! array_key_exists($value, /** @scrutinizer ignore-call */ EBB\getGenders())) {
Loading history...
249
                    throw new InvalidArgumentException(__('Invalid donor gender.'));
250
                }
251
                break;
252
            case 'blood_group':
253
                if (! in_array($value, EBB\getBloodGroups(), true)) {
0 ignored issues
show
Bug introduced by
The function getBloodGroups was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

253
                if (! in_array($value, /** @scrutinizer ignore-call */ EBB\getBloodGroups(), true)) {
Loading history...
254
                    throw new InvalidArgumentException(__('Invalid donor blood group.'));
255
                }
256
                break;
257
            case 'district':
258
                if (! $value instanceof District || ! $value->isExists()) {
259
                    throw new InvalidArgumentException(__('Invalid donor district.'));
260
                }
261
                break;
262
            case 'created_by':
263
                if (! $value instanceof User || ! $value->isExists()) {
264
                    throw new InvalidArgumentException(__('Invalid donor originator.'));
265
                }
266
                break;
267
            case 'status':
268
                if (! is_string($value) || empty($value)) {
269
                    throw new InvalidArgumentException(__('Invalid donor status.'));
270
                }
271
                break;
272
        }
273
274
        return true;
275
    }
276
277
    /**
278
     * @return mixed
279
     * @since 1.0
280
     * @static
281
     */
282
    public static function sanitizeMeta(string $key, $value)
283
    {
284
        switch ($key) {
285
            case 'weight':
286
                $value = EBB\sanitizeFloat($value);
0 ignored issues
show
Bug introduced by
The function sanitizeFloat was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

286
                $value = /** @scrutinizer ignore-call */ EBB\sanitizeFloat($value);
Loading history...
287
                break;
288
            case 'email':
289
                $value = EBB\sanitizeEmail($value);
0 ignored issues
show
Bug introduced by
The function sanitizeEmail was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

289
                $value = /** @scrutinizer ignore-call */ EBB\sanitizeEmail($value);
Loading history...
290
                break;
291
            case 'phone':
292
                $value = EBB\sanitizeInteger($value);
0 ignored issues
show
Bug introduced by
The function sanitizeInteger was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

292
                $value = /** @scrutinizer ignore-call */ EBB\sanitizeInteger($value);
Loading history...
293
                break;
294
            case 'address':
295
                $value = EBB\sanitizeTitle($value);
0 ignored issues
show
Bug introduced by
The function sanitizeTitle was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

295
                $value = /** @scrutinizer ignore-call */ EBB\sanitizeTitle($value);
Loading history...
296
                break;
297
        }
298
299
        return $value;
300
    }
301
302
    /**
303
     * @throws \InvalidArgumentException
304
     * @return bool
305
     * @since 1.0
306
     * @static
307
     */
308
    public static function validateMeta(string $key, $value)
309
    {
310
        switch ($key) {
311
            case 'weight':
312
                if (! empty($value) && ! EBB\isValidFloat($value)) {
0 ignored issues
show
Bug introduced by
The function isValidFloat was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

312
                if (! empty($value) && ! /** @scrutinizer ignore-call */ EBB\isValidFloat($value)) {
Loading history...
313
                    // TODO: Check Min and Max weight.
314
                    throw new InvalidArgumentException(__('Invalid donor weight.'));
315
                }
316
                break;
317
            case 'email':
318
                if (! empty($value) && ! EBB\isValidEmail($value)) {
0 ignored issues
show
Bug introduced by
The function isValidEmail was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

318
                if (! empty($value) && ! /** @scrutinizer ignore-call */ EBB\isValidEmail($value)) {
Loading history...
319
                    throw new InvalidArgumentException(__('Invalid donor e-mail address.'));
320
                }
321
                break;
322
            case 'email_visibility':
323
                if (! empty($value) && ! array_key_exists($value, EBB\getVisibilities())) {
0 ignored issues
show
Bug introduced by
The function getVisibilities was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

323
                if (! empty($value) && ! array_key_exists($value, /** @scrutinizer ignore-call */ EBB\getVisibilities())) {
Loading history...
324
                    throw new InvalidArgumentException(__('Invalid donor e-mail address visibility.'));
325
                }
326
                break;
327
            case 'phone':
328
                if (! empty($value) && ! ctype_digit($value)) {
329
                    throw new InvalidArgumentException(__('Invalid donor phone number.'));
330
                }
331
                break;
332
            case 'phone_visibility':
333
                if (! empty($value) && ! array_key_exists($value, EBB\getVisibilities())) {
334
                    throw new InvalidArgumentException(__('Invalid donor phone number visibility.'));
335
                }
336
                break;
337
        }
338
339
        return true;
340
    }
341
}
342