Failed Conditions
Push — master ( 51347b...4787ce )
by Adrien
08:37
created

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