Failed Conditions
Push — master ( 64d413...b77024 )
by Adrien
10:04
created

User   F

Complexity

Total Complexity 92

Size/Duplication

Total Lines 803
Duplicated Lines 0 %

Test Coverage

Coverage 72.27%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 92
eloc 162
c 4
b 0
f 0
dl 0
loc 803
rs 2
ccs 159
cts 220
cp 0.7227

68 Methods

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