Passed
Push — master ( 5da018...ff48bc )
by Sam
07:31
created

User::setOwner()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 9.4924

Importance

Changes 0
Metric Value
cc 8
eloc 13
c 0
b 0
f 0
nc 10
nop 1
dl 0
loc 23
ccs 10
cts 14
cp 0.7143
crap 9.4924
rs 8.4444
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="custom", operator="Application\Api\Input\Operator\HasBookingWithBookableOperatorType", type="id"),
41
 *     @API\Filter(field="balance", operator="Application\Api\Input\Operator\AccountBalance\EqualOperatorType", type="Money"),
42
 *     @API\Filter(field="balance", operator="Application\Api\Input\Operator\AccountBalance\GreaterOperatorType", type="Money"),
43
 *     @API\Filter(field="balance", operator="Application\Api\Input\Operator\AccountBalance\GreaterOrEqualOperatorType", type="Money"),
44
 *     @API\Filter(field="balance", operator="Application\Api\Input\Operator\AccountBalance\LessOperatorType", type="Money"),
45
 *     @API\Filter(field="balance", operator="Application\Api\Input\Operator\AccountBalance\LessOrEqualOperatorType", type="Money"),
46
 * })
47
 */
48
class User extends AbstractModel implements \Ecodev\Felix\Model\User
49
{
50
    const ROLE_ANONYMOUS = 'anonymous';
51
    const ROLE_BOOKING_ONLY = 'booking_only';
52
    const ROLE_INDIVIDUAL = 'individual';
53
    const ROLE_MEMBER = 'member';
54
    const ROLE_TRAINER = 'trainer';
55
    const ROLE_RESPONSIBLE = 'responsible';
56
    const ROLE_ADMINISTRATOR = 'administrator';
57
58
    const STATUS_INACTIVE = 'inactive';
59
    const STATUS_NEW = 'new';
60
    const STATUS_ACTIVE = 'active';
61
    const STATUS_ARCHIVED = 'archived';
62
63
    use HasDoorAccess;
64
    use HasRemarks;
65
    use HasInternalRemarks;
66
    use HasAddress;
67
    use HasIban;
68
    use HasPassword;
69
70
    /**
71
     * @var User
72
     */
73
    private static $currentUser;
74
75
    /**
76
     * Set currently logged in user
77
     * WARNING: this method should only be called from \Application\Authentication\AuthenticationListener
78
     *
79
     * @param \Application\Model\User $user
80
     */
81 195
    public static function setCurrent(?self $user): void
82
    {
83 195
        self::$currentUser = $user;
84
85
        // Initalize ACL filter with current user if a logged in one exists
86
        /** @var UserRepository $userRepository */
87 195
        $userRepository = _em()->getRepository(self::class);
88 195
        $aclFilter = $userRepository->getAclFilter();
89 195
        $aclFilter->setUser($user);
90
91 195
        CurrentUser::set($user);
92 195
    }
93
94
    /**
95
     * Returns currently logged user or null
96
     */
97 72
    public static function getCurrent(): ?self
98
    {
99 72
        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 53
    public function __construct(string $role = self::ROLE_INDIVIDUAL)
268
    {
269 53
        $this->role = $role;
270 53
        $this->bookings = new ArrayCollection();
271 53
        $this->accounts = new ArrayCollection();
272 53
        $this->licenses = new ArrayCollection();
273 53
        $this->userTags = new ArrayCollection();
274 53
        $this->messages = new ArrayCollection();
275 53
        $this->users = new ArrayCollection();
276 53
    }
277
278
    /**
279
     * Set login (eg: johndoe)
280
     *
281
     * @API\Input(type="Login")
282
     */
283 4
    public function setLogin(string $login): void
284
    {
285 4
        $this->login = $login;
286 4
    }
287
288
    /**
289
     * Get login (eg: johndoe)
290
     *
291
     * @API\Field(type="?Login")
292
     */
293 14
    public function getLogin(): ?string
294
    {
295 14
        return $this->login;
296
    }
297
298
    /**
299
     * Set first name
300
     *
301
     * @param string $firstName
302
     */
303 10
    public function setFirstName($firstName): void
304
    {
305 10
        $this->firstName = $firstName;
306 10
    }
307
308
    /**
309
     * Get first name
310
     */
311 18
    public function getFirstName(): string
312
    {
313 18
        return (string) $this->firstName;
314
    }
315
316
    /**
317
     * Set last name
318
     *
319
     * @param string $lastName
320
     */
321 10
    public function setLastName($lastName): void
322
    {
323 10
        $this->lastName = $lastName;
324 10
    }
325
326
    /**
327
     * Get last name
328
     */
329 12
    public function getLastName(): string
330
    {
331 12
        return (string) $this->lastName;
332
    }
333
334
    /**
335
     * Get full name
336
     */
337 12
    public function getName(): string
338
    {
339 12
        return implode(' ', [$this->getFirstName(), $this->getLastName()]);
340
    }
341
342
    /**
343
     * Set email
344
     *
345
     * @API\Input(type="?Email")
346
     */
347 5
    public function setEmail(?string $email): void
348
    {
349 5
        $this->email = $email;
350 5
    }
351
352
    /**
353
     * Get email
354
     *
355
     * @API\Field(type="?Email")
356
     */
357 10
    public function getEmail(): ?string
358
    {
359 10
        return $this->email;
360
    }
361
362
    /**
363
     * Returns whether the user is administrator and thus have can do anything.
364
     *
365
     * @API\Field(type="Application\Api\Enum\UserRoleType")
366
     */
367 78
    public function getRole(): string
368
    {
369 78
        return $this->role;
370
    }
371
372
    /**
373
     * Sets the user role
374
     *
375
     * The current user is allowed to promote another user up to the same role as himself. So
376
     * a Responsible can promote a Member to Responsible. Or an Admin can promote a Individual to Admin.
377
     *
378
     * But the current user is **not** allowed to demote a user who has a higher role than himself.
379
     * That means that a Responsible cannot demote an Admin to Individual.
380
     */
381 7
    public function setRole(string $role): void
382
    {
383 7
        if ($role === $this->role) {
384 2
            return;
385
        }
386
387 5
        $currentRole = self::getCurrent() ? self::getCurrent()->getRole() : self::ROLE_ANONYMOUS;
388
        $orderedRoles = [
389 5
            self::ROLE_ANONYMOUS,
390 5
            self::ROLE_INDIVIDUAL,
391 5
            self::ROLE_MEMBER,
392 5
            self::ROLE_TRAINER,
393 5
            self::ROLE_BOOKING_ONLY,
394 5
            self::ROLE_RESPONSIBLE,
395 5
            self::ROLE_ADMINISTRATOR,
396
        ];
397
398 5
        $newFound = false;
399 5
        $oldFound = false;
400 5
        foreach ($orderedRoles as $r) {
401 5
            if ($r === $this->role) {
402 3
                $oldFound = true;
403
            }
404 5
            if ($r === $role) {
405 2
                $newFound = true;
406
            }
407
408 5
            if ($r === $currentRole) {
409 5
                break;
410
            }
411
        }
412
413 5
        if (!$newFound || !$oldFound) {
414 3
            throw new Exception($currentRole . ' is not allowed to change role from ' . $this->role . ' to ' . $role);
415
        }
416
417 2
        $this->role = $role;
418 2
    }
419
420 1
    public function setOwner(?self $owner = null): void
421
    {
422 1
        if ($owner && $owner !== $this) {
423
            if ($owner->getOwner() && $owner !== $owner->getOwner()) {
424
                throw new Exception('This user cannot be owned by a user who is himself owned by somebody else');
425
            }
426
427
            if ($this->users->count()) {
428
                throw new Exception('This user owns other users, so he cannot himself be owned by somebody else');
429
            }
430
        }
431
432 1
        if ($this->getOwner()) {
433 1
            $this->getOwner()->getEmail(); // Trigger lazy loading
434 1
            $this->getOwner()->users->removeElement($this);
435
        }
436
437 1
        parent::setOwner($owner);
438
439 1
        if ($this->getOwner()) {
440 1
            $this->getOwner()->getEmail(); // Trigger lazy loading
441 1
            $this->getOwner()->users->add($this);
442 1
            $this->setStatus($this->getOwner()->getStatus());
443
        }
444 1
    }
445
446
    /**
447
     * @API\Field(type="Application\Api\Enum\UserStatusType")
448
     */
449 3
    public function getStatus(): string
450
    {
451 3
        return $this->status;
452
    }
453
454
    /**
455
     * @API\Input(type="Application\Api\Enum\UserStatusType")
456
     */
457 9
    public function setStatus(string $newStatus): void
458
    {
459 9
        if ($newStatus === self::STATUS_ARCHIVED && $this->status !== self::STATUS_ARCHIVED) {
460 1
            $this->setResignDate(Chronos::NOW());
461 8
        } elseif ($this->status === self::STATUS_ARCHIVED && $newStatus !== self::STATUS_ARCHIVED) {
462
            $this->setResignDate(null);
463
        }
464
465 9
        $this->status = $newStatus;
466 9
        $this->revokeToken();
467
468 9
        foreach ($this->users as $user) {
469 1
            if ($user !== $this) {
470
                $user->setStatus($newStatus);
471
            }
472
        }
473 9
    }
474
475 1
    public function initialize(): void
476
    {
477 1
        $this->role = self::ROLE_MEMBER; // Bypass security
478 1
        $this->setStatus(self::STATUS_NEW);
479 1
    }
480
481
    public function getPhone(): string
482
    {
483
        return $this->phone;
484
    }
485
486
    public function setPhone(string $phone): void
487
    {
488
        $this->phone = $phone;
489
    }
490
491
    public function getMobilePhone(): string
492
    {
493
        return $this->mobilePhone;
494
    }
495
496 1
    public function setMobilePhone(string $mobilePhone): void
497
    {
498 1
        $this->mobilePhone = $mobilePhone;
499 1
    }
500
501
    public function getBirthday(): ?Date
502
    {
503
        return $this->birthday;
504
    }
505
506 1
    public function setBirthday(?Date $birthday): void
507
    {
508 1
        $this->birthday = $birthday;
509 1
    }
510
511
    /**
512
     * return null|int
513
     */
514
    public function getAge(): ?int
515
    {
516
        if ($this->birthday) {
517
            return (new Date())->diffInYears($this->birthday);
518
        }
519
520
        return null;
521
    }
522
523
    /**
524
     * Get bookings
525
     */
526 4
    public function getBookings(): Collection
527
    {
528 4
        return $this->bookings;
529
    }
530
531
    /**
532
     * Notify the user that it has a new booking.
533
     * This should only be called by Booking::setResponsible()
534
     */
535 15
    public function bookingAdded(Booking $booking): void
536
    {
537 15
        $this->bookings->add($booking);
538 15
    }
539
540
    /**
541
     * Notify the user that it has a booking was removed.
542
     * This should only be called by Booking::setResponsible()
543
     */
544 4
    public function bookingRemoved(Booking $booking): void
545
    {
546 4
        $this->bookings->removeElement($booking);
547 4
    }
548
549 2
    public function getLicenses(): Collection
550
    {
551 2
        return $this->licenses;
552
    }
553
554 1
    public function getUserTags(): Collection
555
    {
556 1
        return $this->userTags;
557
    }
558
559
    /**
560
     * Notify the user that it has a new license.
561
     * This should only be called by License::addUser()
562
     */
563 1
    public function licenseAdded(License $license): void
564
    {
565 1
        $this->licenses->add($license);
566 1
    }
567
568
    /**
569
     * Notify the user that it a license was removed.
570
     * This should only be called by License::removeUser()
571
     */
572 1
    public function licenseRemoved(License $license): void
573
    {
574 1
        $this->licenses->removeElement($license);
575 1
    }
576
577
    /**
578
     * Notify the user that it has a new userTag.
579
     * This should only be called by UserTag::addUser()
580
     */
581 1
    public function userTagAdded(UserTag $userTag): void
582
    {
583 1
        $this->userTags->add($userTag);
584 1
    }
585
586
    /**
587
     * Notify the user that a userTag was removed.
588
     * This should only be called by UserTag::removeUser()
589
     */
590 1
    public function userTagRemoved(UserTag $userTag): void
591
    {
592 1
        $this->userTags->removeElement($userTag);
593 1
    }
594
595
    public function isTermsAgreement(): bool
596
    {
597
        return $this->termsAgreement;
598
    }
599
600 1
    public function setTermsAgreement(bool $termsAgreement): void
601
    {
602 1
        $this->termsAgreement = $termsAgreement;
603 1
    }
604
605
    public function hasInsurance(): bool
606
    {
607
        return $this->hasInsurance;
608
    }
609
610 1
    public function setHasInsurance(bool $hasInsurance): void
611
    {
612 1
        $this->hasInsurance = $hasInsurance;
613 1
    }
614
615
    public function getWelcomeSessionDate(): ?Chronos
616
    {
617
        return $this->welcomeSessionDate;
618
    }
619
620
    public function setWelcomeSessionDate(?Chronos $welcomeSessionDate): void
621
    {
622
        $this->welcomeSessionDate = $welcomeSessionDate;
623
    }
624
625
    public function getResignDate(): ?Chronos
626
    {
627
        return $this->resignDate;
628
    }
629
630 1
    public function setResignDate(?Chronos $resignDate): void
631
    {
632 1
        $this->resignDate = $resignDate;
633 1
    }
634
635
    public function getReceivesNewsletter(): bool
636
    {
637
        return $this->receivesNewsletter;
638
    }
639
640
    public function setReceivesNewsletter(bool $receivesNewsletter): void
641
    {
642
        $this->receivesNewsletter = $receivesNewsletter;
643
    }
644
645
    /**
646
     * Get the sex
647
     *
648
     * @API\Field(type="Sex")
649
     */
650
    public function getSex(): int
651
    {
652
        return $this->sex;
653
    }
654
655
    /**
656
     * Set the sex
657
     *
658
     * @API\Input(type="Sex")
659
     */
660
    public function setSex(int $sex): void
661
    {
662
        $this->sex = $sex;
663
    }
664
665
    /**
666
     * Get the Swiss Sailing licence number
667
     */
668
    public function getSwissSailing(): string
669
    {
670
        return $this->swissSailing;
671
    }
672
673
    public function setSwissSailing(string $swissSailing): void
674
    {
675
        $this->swissSailing = $swissSailing;
676
    }
677
678
    /**
679
     * Get the Swiss Sailing licence type
680
     *
681
     * @API\Field(type="?SwissSailingType")
682
     */
683
    public function getSwissSailingType(): ?string
684
    {
685
        return $this->swissSailingType;
686
    }
687
688
    /**
689
     * Set the Swiss Sailing licence type
690
     *
691
     * @API\Input(type="?SwissSailingType")
692
     */
693
    public function setSwissSailingType(?string $swissSailingType): void
694
    {
695
        $this->swissSailingType = $swissSailingType;
696
    }
697
698
    /**
699
     * Get the Swiss Windsurf licence type
700
     *
701
     * @API\Field(type="?SwissWindsurfType")
702
     */
703
    public function getSwissWindsurfType(): ?string
704
    {
705
        return $this->swissWindsurfType;
706
    }
707
708
    /**
709
     * Set the Swiss Windsurf licence type
710
     *
711
     * @API\Input(type="?SwissWindsurfType")
712
     */
713
    public function setSwissWindsurfType(?string $swissWindsurfType): void
714
    {
715
        $this->swissWindsurfType = $swissWindsurfType;
716
    }
717
718
    /**
719
     * Get the first login date
720
     */
721
    public function getFirstLogin(): ?Chronos
722
    {
723
        /** @var LogRepository $logRepository */
724
        $logRepository = _em()->getRepository(Log::class);
725
726
        return $logRepository->getLoginDate($this, true);
727
    }
728
729
    /**
730
     * Get the last login date
731
     */
732
    public function getLastLogin(): ?Chronos
733
    {
734
        /** @var LogRepository $logRepository */
735
        $logRepository = _em()->getRepository(Log::class);
736
737
        return $logRepository->getLoginDate($this, false);
738
    }
739
740
    /**
741
     * @API\Field(type="Relationship")
742
     */
743 1
    public function getFamilyRelationship(): string
744
    {
745 1
        return $this->familyRelationship;
746
    }
747
748
    /**
749
     * @API\Input(type="Relationship")
750
     */
751 1
    public function setFamilyRelationship(string $familyRelationship): void
752
    {
753 1
        $this->familyRelationship = $familyRelationship;
754 1
    }
755
756
    /**
757
     * @API\Field(type="BillingType")
758
     */
759
    public function getBillingType(): string
760
    {
761
        return $this->billingType;
762
    }
763
764
    /**
765
     * @API\Input(type="BillingType")
766
     */
767
    public function setBillingType(string $billingType): void
768
    {
769
        $this->billingType = $billingType;
770
    }
771
772
    /**
773
     * Get the user transaction account
774
     */
775 25
    public function getAccount(): ?Account
776
    {
777 25
        return $this->accounts->count() ? $this->accounts->first() : null;
778
    }
779
780
    /**
781
     * Notify the user that it has a new account
782
     * This should only be called by Account::setOwner()
783
     */
784 13
    public function accountAdded(Account $account): void
785
    {
786 13
        $this->accounts->clear();
787 13
        $this->accounts->add($account);
788 13
    }
789
790
    /**
791
     * Notify the user that a account was removed
792
     * This should only be called by Account::setOwner()
793
     */
794 1
    public function accountRemoved(): void
795
    {
796 1
        $this->accounts->clear();
797 1
    }
798
799
    /**
800
     * Get messages sent to the user
801
     */
802 1
    public function getMessages(): Collection
803
    {
804 1
        return $this->messages;
805
    }
806
807
    /**
808
     * Notify the user that it has a new message
809
     * This should only be called by Message::setRecipient()
810
     */
811 8
    public function messageAdded(Message $message): void
812
    {
813 8
        $this->messages->add($message);
814 8
    }
815
816
    /**
817
     * Notify the user that a message was removed
818
     * This should only be called by Message::setRecipient()
819
     */
820 1
    public function messageRemoved(Message $message): void
821
    {
822 1
        $this->messages->removeElement($message);
823 1
    }
824
825
    /**
826
     * Check if the user can *really* open a door
827
     * This also takes into account the user status and role
828
     *
829
     * @API\Field(args={@API\Argument(name="door", type="?Application\Api\Enum\DoorType")})
830
     *
831
     * @param null|string $door a particular door, or null for any
832
     */
833 6
    public function getCanOpenDoor(?string $door = null): bool
834
    {
835 6
        $allowedStatus = [self::STATUS_ACTIVE];
836 6
        $allowedRoles = [self::ROLE_INDIVIDUAL, self::ROLE_MEMBER, self::ROLE_RESPONSIBLE, self::ROLE_ADMINISTRATOR];
837
838 6
        if ($door && !$this->$door) {
839 3
            return false;
840
        }
841
842 6
        if (!in_array($this->status, $allowedStatus, true) || !in_array($this->role, $allowedRoles, true)) {
843 2
            return false;
844
        }
845
846 4
        return true;
847
    }
848
849
    /**
850
     * Automatically called by Doctrine when the object is saved for the first time
851
     *
852
     * @ORM\PrePersist
853
     */
854 3
    public function timestampCreation(): void
855
    {
856 3
        $this->setCreationDate(new Chronos());
857 3
        $this->setCreator(self::getCurrent());
858 3
    }
859
}
860