Passed
Push — master ( 1c63e7...8d8414 )
by Adrien
17:41
created

User   F

Complexity

Total Complexity 92

Size/Duplication

Total Lines 788
Duplicated Lines 0 %

Test Coverage

Coverage 76.63%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 92
eloc 170
c 3
b 0
f 0
dl 0
loc 788
ccs 141
cts 184
cp 0.7663
rs 2

68 Methods

Rating   Name   Duplication   Size   Complexity  
A getWelcomeSessionDate() 0 3 1
A isTermsAgreement() 0 3 1
A getCurrent() 0 3 1
A setCurrent() 0 11 1
A setRole() 0 9 3
A setWelcomeSessionDate() 0 3 1
A licenseRemoved() 0 3 1
A setFirstName() 0 3 1
A setEmail() 0 3 1
A setSwissWindsurfType() 0 3 1
A getBookings() 0 3 1
A getCanOpenDoor() 0 22 5
A getLastLogin() 0 6 1
A messageRemoved() 0 3 1
A getAge() 0 7 2
A setLogin() 0 3 1
A getUserTags() 0 3 1
A getMobilePhone() 0 3 1
A getLogin() 0 3 1
A isFamilyOwner() 0 3 1
A accountAdded() 0 4 1
A getBirthday() 0 3 1
A getReceivesNewsletter() 0 3 1
A userTagAdded() 0 3 1
A getStatus() 0 3 1
A getMessages() 0 3 1
A getLastName() 0 3 1
A userTagRemoved() 0 3 1
A initialize() 0 4 1
A setMobilePhone() 0 3 1
A hasInsurance() 0 3 1
A getEmail() 0 3 1
B setStatus() 0 14 7
A getName() 0 3 1
A getLicenses() 0 3 1
A getPhone() 0 3 1
A setHasInsurance() 0 3 1
A __construct() 0 9 1
A getFirstName() 0 3 1
A bookingRemoved() 0 3 1
A getOwnerForCreation() 0 3 1
A getSwissSailingType() 0 3 1
A bookingAdded() 0 3 1
A getSwissSailing() 0 3 1
A getSex() 0 3 1
A getRunningBookings() 0 3 2
A setLastName() 0 3 1
A getResignDate() 0 3 1
A setSwissSailingType() 0 3 1
A accountRemoved() 0 3 1
A setTermsAgreement() 0 3 1
A getAccount() 0 7 4
B setOwner() 0 25 8
A getFirstLogin() 0 6 1
A setBirthday() 0 3 1
A setResignDate() 0 3 1
A getRole() 0 3 1
A licenseAdded() 0 3 1
A getFamilyRelationship() 0 3 1
A setFamilyRelationship() 0 3 1
A getSwissWindsurfType() 0 3 1
A getBillingType() 0 3 1
A messageAdded() 0 3 1
A setBillingType() 0 3 1
A setReceivesNewsletter() 0 3 1
A setSwissSailing() 0 3 1
A setPhone() 0 3 1
A setSex() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like User often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use User, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Model;
6
7
use Application\DBAL\Types\BillingTypeType;
8
use Application\DBAL\Types\BookingStatusType;
9
use Application\DBAL\Types\RelationshipType;
10
use Application\Repository\LogRepository;
11
use Application\Repository\UserRepository;
12
use Application\Service\Role;
13
use Application\Traits\HasAddress;
14
use Application\Traits\HasDoorAccess;
15
use Application\Traits\HasIban;
16
use Application\Traits\HasRemarks;
17
use Cake\Chronos\Chronos;
18
use Cake\Chronos\Date;
19
use Doctrine\Common\Collections\ArrayCollection;
20
use Doctrine\Common\Collections\Collection;
21
use Doctrine\ORM\Mapping as ORM;
22
use Ecodev\Felix\Api\Exception;
23
use Ecodev\Felix\Model\CurrentUser;
24
use Ecodev\Felix\Model\Traits\HasInternalRemarks;
25
use Ecodev\Felix\Model\Traits\HasPassword;
26
use GraphQL\Doctrine\Annotation as API;
27
28
/**
29
 * User.
30
 *
31
 * @ORM\Entity(repositoryClass="Application\Repository\UserRepository")
32
 * @ORM\HasLifecycleCallbacks
33
 * @ORM\AssociationOverrides({
34
 *     @ORM\AssociationOverride(name="owner", inversedBy="users")
35
 * })
36
 * @API\Sorting({
37
 *     "Application\Api\Input\Sorting\Age",
38
 *     "Application\Api\Input\Sorting\Balance",
39
 * })
40
 * @API\Filters({
41
 *     @API\Filter(field="custom", operator="Application\Api\Input\Operator\HasBookingWithTaggedBookableOperatorType", type="id"),
42
 *     @API\Filter(field="custom", operator="Application\Api\Input\Operator\HasBookingWithBookableOperatorType", type="id"),
43
 *     @API\Filter(field="custom", operator="Application\Api\Input\Operator\HasBookingStatusOperatorType", type="id"),
44
 *     @API\Filter(field="custom", operator="Application\Api\Input\Operator\HasBookingCompletedOperatorType", type="boolean"),
45
 *     @API\Filter(field="bookingCount", operator="Application\Api\Input\Operator\BookingCount\BookingCountEqualOperatorType", type="int"),
46
 *     @API\Filter(field="bookingCount", operator="Application\Api\Input\Operator\BookingCount\BookingCountGreaterOperatorType", type="int"),
47
 *     @API\Filter(field="bookingCount", operator="Application\Api\Input\Operator\BookingCount\BookingCountGreaterOrEqualOperatorType", type="int"),
48
 *     @API\Filter(field="bookingCount", operator="Application\Api\Input\Operator\BookingCount\BookingCountLessOperatorType", type="int"),
49
 *     @API\Filter(field="bookingCount", operator="Application\Api\Input\Operator\BookingCount\BookingCountLessOrEqualOperatorType", type="int"),
50
 *     @API\Filter(field="balance", operator="Application\Api\Input\Operator\AccountBalance\EqualOperatorType", type="Money"),
51
 *     @API\Filter(field="balance", operator="Application\Api\Input\Operator\AccountBalance\GreaterOperatorType", type="Money"),
52
 *     @API\Filter(field="balance", operator="Application\Api\Input\Operator\AccountBalance\GreaterOrEqualOperatorType", type="Money"),
53
 *     @API\Filter(field="balance", operator="Application\Api\Input\Operator\AccountBalance\LessOperatorType", type="Money"),
54
 *     @API\Filter(field="balance", operator="Application\Api\Input\Operator\AccountBalance\LessOrEqualOperatorType", type="Money"),
55
 *     @API\Filter(field="bookingDate", operator="Application\Api\Input\Operator\BookingDate\BookingDateEqualOperatorType", type="Date"),
56
 *     @API\Filter(field="bookingDate", operator="Application\Api\Input\Operator\BookingDate\BookingDateGreaterOperatorType", type="Date"),
57
 *     @API\Filter(field="bookingDate", operator="Application\Api\Input\Operator\BookingDate\BookingDateGreaterOrEqualOperatorType", type="Date"),
58
 *     @API\Filter(field="bookingDate", operator="Application\Api\Input\Operator\BookingDate\BookingDateLessOperatorType", type="Date"),
59
 *     @API\Filter(field="bookingDate", operator="Application\Api\Input\Operator\BookingDate\BookingDateLessOrEqualOperatorType", type="Date"),
60
 * })
61
 */
62
class User extends AbstractModel implements \Ecodev\Felix\Model\User
63
{
64
    final public const ROLE_ANONYMOUS = 'anonymous';
65
    final public const ROLE_BOOKING_ONLY = 'booking_only';
66
    final public const ROLE_INDIVIDUAL = 'individual';
67
    final public const ROLE_ACCOUNTING_VERIFICATOR = 'accounting_verificator';
68
    final public const ROLE_MEMBER = 'member';
69
    final public const ROLE_TRAINER = 'trainer';
70
    final public const ROLE_FORMATION_RESPONSIBLE = 'formation_responsible';
71
    final public const ROLE_RESPONSIBLE = 'responsible';
72
    final public const ROLE_ADMINISTRATOR = 'administrator';
73
74
    final public const STATUS_INACTIVE = 'inactive';
75
    final public const STATUS_NEW = 'new';
76
    final public const STATUS_ACTIVE = 'active';
77
    final public const STATUS_ARCHIVED = 'archived';
78
79
    use HasAddress;
80
    use HasDoorAccess;
81
    use HasIban;
82
    use HasInternalRemarks;
83
    use HasPassword;
84
    use HasRemarks;
85
86
    private static ?User $currentUser = null;
87
88
    /**
89
     * Set currently logged in user
90
     * WARNING: this method should only be called from \Application\Authentication\AuthenticationListener.
91
     *
92
     * @param User $user
93
     */
94 273
    public static function setCurrent(?self $user): void
95
    {
96 273
        self::$currentUser = $user;
97
98
        // Initialize ACL filter with current user if a logged in one exists
99
        /** @var UserRepository $userRepository */
100 273
        $userRepository = _em()->getRepository(self::class);
101 273
        $aclFilter = $userRepository->getAclFilter();
102 273
        $aclFilter->setUser($user);
103
104 273
        CurrentUser::set($user);
105
    }
106
107
    /**
108
     * Returns currently logged user or null.
109
     */
110 98
    public static function getCurrent(): ?self
111
    {
112 98
        return self::$currentUser;
113
    }
114
115
    /**
116
     * @ORM\Column(type="string", length=50, nullable=true, unique=true)
117
     */
118
    private ?string $login = null;
119
120
    /**
121
     * @ORM\Column(type="string", length=191)
122
     */
123
    private string $firstName = '';
124
125
    /**
126
     * @ORM\Column(type="string", length=191)
127
     */
128
    private string $lastName = '';
129
130
    /**
131
     * @ORM\Column(type="string", length=191, nullable=true, unique=true)
132
     */
133
    private ?string $email = null;
134
135
    /**
136
     * @ORM\Column(type="UserRole", options={"default" = User::ROLE_INDIVIDUAL})
137
     */
138
    private string $role = self::ROLE_INDIVIDUAL;
139
140
    /**
141
     * @ORM\Column(type="UserStatus", options={"default" = User::STATUS_NEW})
142
     */
143
    private string $status = self::STATUS_NEW;
144
145
    /**
146
     * @ORM\Column(type="datetime", nullable=true)
147
     */
148
    private ?Chronos $welcomeSessionDate = null;
149
150
    /**
151
     * @ORM\Column(type="datetime", nullable=true)
152
     */
153
    private ?Chronos $resignDate = null;
154
155
    /**
156
     * @var int sex
157
     * @ORM\Column(type="smallint", options={"default" = 0}))
158
     */
159
    private int $sex = 0;
160
161
    /**
162
     * @ORM\Column(type="string", length=25, options={"default" = ""})
163
     */
164
    private string $phone = '';
165
166
    /**
167
     * @ORM\Column(type="string", length=25, options={"default" = ""})
168
     */
169
    private string $mobilePhone = '';
170
171
    /**
172
     * @ORM\Column(type="string", length=25, options={"default" = ""})
173
     */
174
    private string $swissSailing = '';
175
176
    /**
177
     * @ORM\Column(type="SwissSailingType", nullable=true)
178
     */
179
    private ?string $swissSailingType = null;
180
181
    /**
182
     * @ORM\Column(type="SwissWindsurfType", nullable=true)
183
     */
184
    private ?string $swissWindsurfType = null;
185
186
    /**
187
     * @ORM\Column(type="date", nullable=true)
188
     */
189
    private ?Date $birthday = null;
190
191
    /**
192
     * @ORM\Column(type="boolean", options={"default" = 0})
193
     */
194
    private bool $termsAgreement = false;
195
196
    /**
197
     * @ORM\Column(type="boolean", options={"default" = 0})
198
     */
199
    private bool $hasInsurance = false;
200
201
    /**
202
     * @ORM\Column(type="boolean", options={"default" = 0})
203
     */
204
    private bool $receivesNewsletter = false;
205
206
    /**
207
     * @ORM\Column(type="Relationship", options={"default" = RelationshipType::HOUSEHOLDER})
208
     */
209
    private string $familyRelationship = RelationshipType::HOUSEHOLDER;
210
211
    /**
212
     * @ORM\Column(type="BillingType", options={"default" = BillingTypeType::ELECTRONIC})
213
     */
214
    private string $billingType = BillingTypeType::ELECTRONIC;
215
216
    /**
217
     * @var Collection<Booking>
218
     * @ORM\OneToMany(targetEntity="Booking", mappedBy="owner")
219
     */
220
    private Collection $bookings;
221
222
    /**
223
     * @var Collection<License>
224
     * @ORM\ManyToMany(targetEntity="License", mappedBy="users")
225
     */
226
    private Collection $licenses;
227
228
    /**
229
     * @var Collection<UserTag>
230
     * @ORM\ManyToMany(targetEntity="UserTag", mappedBy="users")
231
     */
232
    private Collection $userTags;
233
234
    /**
235
     * @var Collection<Message>
236
     * @ORM\OneToMany(targetEntity="Message", mappedBy="recipient")
237
     */
238
    private Collection $messages;
239
240
    /**
241
     * There is actually 0 to 1 account, never more. And this is
242
     * enforced by DB unique constraints.
243
     *
244
     * @var Collection<Account>
245
     * @ORM\OneToMany(targetEntity="Account", mappedBy="owner")
246
     */
247
    private Collection $accounts;
248
249
    /**
250
     * @var Collection<User>
251
     * @ORM\OneToMany(targetEntity="User", mappedBy="owner")
252
     */
253
    private Collection $users;
254
255
    /**
256
     * Constructor.
257
     *
258
     * @param string $role role for new user
259
     */
260 110
    public function __construct(string $role = self::ROLE_INDIVIDUAL)
261
    {
262 110
        $this->role = $role;
263 110
        $this->bookings = new ArrayCollection();
264 110
        $this->accounts = new ArrayCollection();
265 110
        $this->licenses = new ArrayCollection();
266 110
        $this->userTags = new ArrayCollection();
267 110
        $this->messages = new ArrayCollection();
268 110
        $this->users = new ArrayCollection();
269
    }
270
271
    /**
272
     * Set login (eg: johndoe).
273
     *
274
     * @API\Input(type="Login")
275
     */
276 16
    public function setLogin(string $login): void
277
    {
278 16
        $this->login = $login;
279
    }
280
281
    /**
282
     * Get login (eg: johndoe).
283
     *
284
     * @API\Field(type="?Login")
285
     */
286 29
    public function getLogin(): ?string
287
    {
288 29
        return $this->login;
289
    }
290
291
    /**
292
     * Set first name.
293
     */
294 11
    public function setFirstName(string $firstName): void
295
    {
296 11
        $this->firstName = $firstName;
297
    }
298
299
    /**
300
     * Get first name.
301
     */
302 33
    public function getFirstName(): string
303
    {
304 33
        return $this->firstName;
305
    }
306
307
    /**
308
     * Set last name.
309
     */
310 11
    public function setLastName(string $lastName): void
311
    {
312 11
        $this->lastName = $lastName;
313
    }
314
315
    /**
316
     * Get last name.
317
     */
318 26
    public function getLastName(): string
319
    {
320 26
        return $this->lastName;
321
    }
322
323
    /**
324
     * Get full name.
325
     */
326 26
    public function getName(): string
327
    {
328 26
        return implode(' ', [$this->getFirstName(), $this->getLastName()]);
329
    }
330
331
    /**
332
     * Set email.
333
     *
334
     * @API\Input(type="?Email")
335
     */
336 6
    public function setEmail(?string $email): void
337
    {
338 6
        $this->email = $email;
339
    }
340
341
    /**
342
     * Get email.
343
     *
344
     * @API\Field(type="?Email")
345
     */
346 16
    public function getEmail(): ?string
347
    {
348 16
        return $this->email;
349
    }
350
351
    /**
352
     * Get the user role.
353
     *
354
     * @API\Field(type="UserRole")
355
     */
356 100
    public function getRole(): string
357
    {
358 100
        return $this->role;
359
    }
360
361
    /**
362
     * Sets the user role.
363
     *
364
     * @API\Input(type="UserRole")
365
     */
366 8
    public function setRole(string $role): void
367
    {
368 8
        if (!Role::canUpdate(self::getCurrent(), $this->role, $role)) {
369 3
            $currentRole = self::getCurrent() ? self::getCurrent()->getRole() : self::ROLE_ANONYMOUS;
370
371 3
            throw new Exception($currentRole . ' is not allowed to change role from ' . $this->role . ' to ' . $role);
372
        }
373
374 5
        $this->role = $role;
375
    }
376
377 10
    public function setOwner(?self $owner = null): void
378
    {
379 10
        if ($owner === $this) {
380 1
            throw new Exception('This user cannot be owned by himself');
381
        }
382
383 9
        if ($owner && $owner->getOwner()) {
384 1
            throw new Exception('This user cannot be owned by a user who is himself owned by somebody else');
385
        }
386
387 9
        if ($owner && $this->users->count()) {
388 1
            throw new Exception('This user owns other users, so he cannot himself be owned by somebody else');
389
        }
390
391 9
        if ($this->getOwner()) {
392 2
            $this->getOwner()->getEmail(); // Trigger lazy loading
393 2
            $this->getOwner()->users->removeElement($this);
394
        }
395
396 9
        parent::setOwner($owner);
397
398 9
        if ($this->getOwner()) {
399 5
            $this->getOwner()->getEmail(); // Trigger lazy loading
400 5
            $this->getOwner()->users->add($this);
401 5
            $this->setStatus($this->getOwner()->getStatus());
402
        }
403
    }
404
405
    /**
406
     * @API\Field(type="Application\Api\Enum\UserStatusType")
407
     */
408 9
    public function getStatus(): string
409
    {
410 9
        return $this->status;
411
    }
412
413
    /**
414
     * @API\Input(type="Application\Api\Enum\UserStatusType")
415
     */
416 15
    public function setStatus(string $newStatus): void
417
    {
418 15
        if ($newStatus === self::STATUS_ARCHIVED && $this->status !== self::STATUS_ARCHIVED) {
419 2
            $this->setResignDate(Chronos::NOW());
420 14
        } elseif ($this->status === self::STATUS_ARCHIVED && $newStatus !== self::STATUS_ARCHIVED) {
421
            $this->setResignDate(null);
422
        }
423
424 15
        $this->status = $newStatus;
425 15
        $this->revokeToken();
426
427 15
        foreach ($this->users as $user) {
428 1
            if ($user !== $this) {
429 1
                $user->setStatus($newStatus);
430
            }
431
        }
432
    }
433
434
    /**
435
     * Whether this user is a family owner or not.
436
     *
437
     * This is used for our internal logic and should
438
     * NEVER be related to `familyRelationship`. That field
439
     * is purely informative for humans.
440
     */
441 1
    public function isFamilyOwner(): bool
442
    {
443 1
        return !$this->getOwner();
444
    }
445
446 1
    public function initialize(): void
447
    {
448 1
        $this->role = self::ROLE_MEMBER; // Bypass security
449 1
        $this->setStatus(self::STATUS_NEW);
450
    }
451
452
    public function getPhone(): string
453
    {
454
        return $this->phone;
455
    }
456
457 1
    public function setPhone(string $phone): void
458
    {
459 1
        $this->phone = $phone;
460
    }
461
462
    public function getMobilePhone(): string
463
    {
464
        return $this->mobilePhone;
465
    }
466
467 2
    public function setMobilePhone(string $mobilePhone): void
468
    {
469 2
        $this->mobilePhone = $mobilePhone;
470
    }
471
472
    public function getBirthday(): ?Date
473
    {
474
        return $this->birthday;
475
    }
476
477 1
    public function setBirthday(?Date $birthday): void
478
    {
479 1
        $this->birthday = $birthday;
480
    }
481
482
    /**
483
     * return null|int.
484
     */
485
    public function getAge(): ?int
486
    {
487
        if ($this->birthday) {
488
            return (new Date())->diffInYears($this->birthday);
489
        }
490
491
        return null;
492
    }
493
494
    /**
495
     * Get bookings.
496
     */
497 3
    public function getBookings(): Collection
498
    {
499 3
        return $this->bookings;
500
    }
501
502
    /**
503
     * Get active bookings (confirmed and non-terminated).
504
     *
505
     * @API\Exclude
506
     */
507 2
    public function getRunningBookings(): Collection
508
    {
509 2
        return $this->bookings->filter(fn (Booking $booking) => $booking->getStatus() === BookingStatusType::BOOKED && $booking->getEndDate() === null);
510
    }
511
512
    /**
513
     * Notify the user that it has a new booking.
514
     * This should only be called by Booking::setResponsible().
515
     */
516 16
    public function bookingAdded(Booking $booking): void
517
    {
518 16
        $this->bookings->add($booking);
519
    }
520
521
    /**
522
     * Notify the user that it has a booking was removed.
523
     * This should only be called by Booking::setResponsible().
524
     */
525 4
    public function bookingRemoved(Booking $booking): void
526
    {
527 4
        $this->bookings->removeElement($booking);
528
    }
529
530 2
    public function getLicenses(): Collection
531
    {
532 2
        return $this->licenses;
533
    }
534
535 1
    public function getUserTags(): Collection
536
    {
537 1
        return $this->userTags;
538
    }
539
540
    /**
541
     * Notify the user that it has a new license.
542
     * This should only be called by License::addUser().
543
     */
544 1
    public function licenseAdded(License $license): void
545
    {
546 1
        $this->licenses->add($license);
547
    }
548
549
    /**
550
     * Notify the user that it a license was removed.
551
     * This should only be called by License::removeUser().
552
     */
553 1
    public function licenseRemoved(License $license): void
554
    {
555 1
        $this->licenses->removeElement($license);
556
    }
557
558
    /**
559
     * Notify the user that it has a new userTag.
560
     * This should only be called by UserTag::addUser().
561
     */
562 1
    public function userTagAdded(UserTag $userTag): void
563
    {
564 1
        $this->userTags->add($userTag);
565
    }
566
567
    /**
568
     * Notify the user that a userTag was removed.
569
     * This should only be called by UserTag::removeUser().
570
     */
571 1
    public function userTagRemoved(UserTag $userTag): void
572
    {
573 1
        $this->userTags->removeElement($userTag);
574
    }
575
576
    public function isTermsAgreement(): bool
577
    {
578
        return $this->termsAgreement;
579
    }
580
581 2
    public function setTermsAgreement(bool $termsAgreement): void
582
    {
583 2
        $this->termsAgreement = $termsAgreement;
584
    }
585
586
    public function hasInsurance(): bool
587
    {
588
        return $this->hasInsurance;
589
    }
590
591 2
    public function setHasInsurance(bool $hasInsurance): void
592
    {
593 2
        $this->hasInsurance = $hasInsurance;
594
    }
595
596
    public function getWelcomeSessionDate(): ?Chronos
597
    {
598
        return $this->welcomeSessionDate;
599
    }
600
601
    public function setWelcomeSessionDate(?Chronos $welcomeSessionDate): void
602
    {
603
        $this->welcomeSessionDate = $welcomeSessionDate;
604
    }
605
606 1
    public function getResignDate(): ?Chronos
607
    {
608 1
        return $this->resignDate;
609
    }
610
611 2
    public function setResignDate(?Chronos $resignDate): void
612
    {
613 2
        $this->resignDate = $resignDate;
614
    }
615
616
    public function getReceivesNewsletter(): bool
617
    {
618
        return $this->receivesNewsletter;
619
    }
620
621 1
    public function setReceivesNewsletter(bool $receivesNewsletter): void
622
    {
623 1
        $this->receivesNewsletter = $receivesNewsletter;
624
    }
625
626
    /**
627
     * Get the sex.
628
     *
629
     * @API\Field(type="Sex")
630
     */
631
    public function getSex(): int
632
    {
633
        return $this->sex;
634
    }
635
636
    /**
637
     * Set the sex.
638
     *
639
     * @API\Input(type="Sex")
640
     */
641 1
    public function setSex(int $sex): void
642
    {
643 1
        $this->sex = $sex;
644
    }
645
646
    /**
647
     * Get the Swiss Sailing licence number.
648
     */
649
    public function getSwissSailing(): string
650
    {
651
        return $this->swissSailing;
652
    }
653
654 1
    public function setSwissSailing(string $swissSailing): void
655
    {
656 1
        $this->swissSailing = $swissSailing;
657
    }
658
659
    /**
660
     * Get the Swiss Sailing licence type.
661
     *
662
     * @API\Field(type="?SwissSailingType")
663
     */
664
    public function getSwissSailingType(): ?string
665
    {
666
        return $this->swissSailingType;
667
    }
668
669
    /**
670
     * Set the Swiss Sailing licence type.
671
     *
672
     * @API\Input(type="?SwissSailingType")
673
     */
674
    public function setSwissSailingType(?string $swissSailingType): void
675
    {
676
        $this->swissSailingType = $swissSailingType;
677
    }
678
679
    /**
680
     * Get the Swiss Windsurf licence type.
681
     *
682
     * @API\Field(type="?SwissWindsurfType")
683
     */
684
    public function getSwissWindsurfType(): ?string
685
    {
686
        return $this->swissWindsurfType;
687
    }
688
689
    /**
690
     * Set the Swiss Windsurf licence type.
691
     *
692
     * @API\Input(type="?SwissWindsurfType")
693
     */
694
    public function setSwissWindsurfType(?string $swissWindsurfType): void
695
    {
696
        $this->swissWindsurfType = $swissWindsurfType;
697
    }
698
699
    /**
700
     * Get the first login date.
701
     */
702
    public function getFirstLogin(): ?Chronos
703
    {
704
        /** @var LogRepository $logRepository */
705
        $logRepository = _em()->getRepository(Log::class);
706
707
        return $logRepository->getLoginDate($this, true);
708
    }
709
710
    /**
711
     * Get the last login date.
712
     */
713
    public function getLastLogin(): ?Chronos
714
    {
715
        /** @var LogRepository $logRepository */
716
        $logRepository = _em()->getRepository(Log::class);
717
718
        return $logRepository->getLoginDate($this, false);
719
    }
720
721
    /**
722
     * @API\Field(type="Relationship")
723
     */
724
    public function getFamilyRelationship(): string
725
    {
726
        return $this->familyRelationship;
727
    }
728
729
    /**
730
     * @API\Input(type="Relationship")
731
     */
732 2
    public function setFamilyRelationship(string $familyRelationship): void
733
    {
734 2
        $this->familyRelationship = $familyRelationship;
735
    }
736
737
    /**
738
     * @API\Field(type="BillingType")
739
     */
740
    public function getBillingType(): string
741
    {
742
        return $this->billingType;
743
    }
744
745
    /**
746
     * @API\Input(type="BillingType")
747
     */
748 1
    public function setBillingType(string $billingType): void
749
    {
750 1
        $this->billingType = $billingType;
751
    }
752
753
    /**
754
     * Get the user transaction account.
755
     */
756 39
    public function getAccount(): ?Account
757
    {
758 39
        if ($this->getOwner() && $this->getOwner() !== $this) {
759 8
            return $this->getOwner()->getAccount();
760
        }
761
762 39
        return $this->accounts->count() ? $this->accounts->first() : null;
763
    }
764
765
    /**
766
     * Notify the user that it has a new account
767
     * This should only be called by Account::setOwner().
768
     */
769 20
    public function accountAdded(Account $account): void
770
    {
771 20
        $this->accounts->clear();
772 20
        $this->accounts->add($account);
773
    }
774
775
    /**
776
     * Notify the user that a account was removed
777
     * This should only be called by Account::setOwner().
778
     */
779 1
    public function accountRemoved(): void
780
    {
781 1
        $this->accounts->clear();
782
    }
783
784
    /**
785
     * Get messages sent to the user.
786
     */
787 1
    public function getMessages(): Collection
788
    {
789 1
        return $this->messages;
790
    }
791
792
    /**
793
     * Notify the user that it has a new message
794
     * This should only be called by Message::setRecipient().
795
     */
796 9
    public function messageAdded(Message $message): void
797
    {
798 9
        $this->messages->add($message);
799
    }
800
801
    /**
802
     * Notify the user that a message was removed
803
     * This should only be called by Message::setRecipient().
804
     */
805 1
    public function messageRemoved(Message $message): void
806
    {
807 1
        $this->messages->removeElement($message);
808
    }
809
810
    /**
811
     * Check if the user can *really* open a door
812
     * This also takes into account the user status and role.
813
     *
814
     * @API\Field(args={@API\Argument(name="door", type="?Application\Api\Enum\DoorType")})
815
     *
816
     * @param null|string $door a particular door, or null for any
817
     */
818 6
    public function getCanOpenDoor(?string $door = null): bool
819
    {
820 6
        $allowedStatus = [self::STATUS_ACTIVE];
821 6
        $allowedRoles = [
822
            self::ROLE_ACCOUNTING_VERIFICATOR,
823
            self::ROLE_INDIVIDUAL,
824
            self::ROLE_MEMBER,
825
            self::ROLE_TRAINER,
826
            self::ROLE_FORMATION_RESPONSIBLE,
827
            self::ROLE_RESPONSIBLE,
828
            self::ROLE_ADMINISTRATOR,
829
        ];
830
831 6
        if ($door && !$this->$door) {
832 3
            return false;
833
        }
834
835 6
        if (!in_array($this->status, $allowedStatus, true) || !in_array($this->role, $allowedRoles, true)) {
836 2
            return false;
837
        }
838
839 4
        return true;
840
    }
841
842
    /**
843
     * Override parent to prevents users created from administration to be family of the administrator.
844
     *
845
     * The owner must be explicitly set for all users.
846
     */
847 3
    protected function getOwnerForCreation(): ?self
848
    {
849 3
        return null;
850
    }
851
}
852