Failed Conditions
Push — master ( f136c2...a524b2 )
by Sam
08:19
created

User::hasInsurance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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