Passed
Pull Request — master (#1716)
by Nico
23:15
created

User::getSettings()   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 Stu\Orm\Entity;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ORM\Mapping\Column;
10
use Doctrine\ORM\Mapping\Entity;
11
use Doctrine\ORM\Mapping\GeneratedValue;
12
use Doctrine\ORM\Mapping\Id;
13
use Doctrine\ORM\Mapping\Index;
14
use Doctrine\ORM\Mapping\JoinColumn;
15
use Doctrine\ORM\Mapping\ManyToOne;
16
use Doctrine\ORM\Mapping\OneToMany;
17
use Doctrine\ORM\Mapping\OneToOne;
18
use Doctrine\ORM\Mapping\OrderBy;
19
use Doctrine\ORM\Mapping\Table;
20
use Stu\Component\Game\GameEnum;
21
use Stu\Component\Game\ModuleViewEnum;
22
use Stu\Component\Map\MapEnum;
23
use Stu\Component\Player\UserAwardEnum;
24
use Stu\Component\Player\UserCssClassEnum;
25
use Stu\Component\Player\UserRpgBehaviorEnum;
26
use Stu\Module\PlayerSetting\Lib\UserEnum;
27
use Stu\Module\PlayerSetting\Lib\UserSettingEnum;
28
29
/**
30
 * @Entity(repositoryClass="Stu\Orm\Repository\UserRepository")
31
 * @Table(
32
 *     name="stu_user",
33
 *     indexes={
34
 *         @Index(name="user_alliance_idx", columns={"allys_id"})
35
 *     }
36
 * )
37
 **/
38
class User implements UserInterface
39
{
40
    /**
41
     * @Id
42
     * @Column(type="integer")
43
     * @GeneratedValue(strategy="IDENTITY")
44
     *
45
     */
46
    private int $id;
47
48
    /**
49
     * @Column(type="string")
50
     *
51
     */
52
    private string $username = '';
53
54
    /**
55
     * @Column(type="string", length=20)
56
     *
57
     */
58
    private string $login = '';
59
60
    /**
61
     * @Column(type="string", length=255)
62
     *
63
     */
64
    private string $pass = '';
65
66
    /**
67
     * @Column(type="string", length=6, nullable=true)
68
     *
69
     */
70
    private ?string $sms_code = null;
71
72
    /**
73
     * @Column(type="string", length=200)
74
     *
75
     */
76
    private string $email = '';
77
78
    /**
79
     * @Column(type="string", length=255, nullable=true)
80
     *
81
     */
82
    private ?string $mobile = null;
83
84
    /**
85
     * @Column(type="integer", nullable=true)
86
     *
87
     */
88
    private ?int $allys_id = null;
89
90
    /**
91
     * @Column(type="integer")
92
     *
93
     */
94
    private int $race = 9;
95
96
    /**
97
     * @Column(type="smallint")
98
     *
99
     */
100
    private int $state = UserEnum::USER_STATE_NEW;
101
102
    /**
103
     * @Column(type="string", length=200, nullable=true)
104
     *
105
     */
106
    private string $propic = '';
107
108
    /**
109
     * @Column(type="boolean", nullable=true)
110
     *
111
     */
112
    private bool $email_notification = true;
113
114
    /**
115
     * @Column(type="integer")
116
     *
117
     */
118
    private int $lastaction = 0;
119
120
    /**
121
     * @Column(type="integer")
122
     *
123
     */
124
    private int $creation = 0;
125
126
    /**
127
     * @Column(type="integer")
128
     *
129
     */
130
    private int $kn_lez = 0;
131
132
    /**
133
     * @Column(type="smallint")
134
     *
135
     */
136
    private int $delmark = 0;
137
138
    /**
139
     * @Column(type="boolean")
140
     *
141
     */
142
    private bool $vac_active = false;
143
144
    /**
145
     * @Column(type="integer")
146
     *
147
     */
148
    private int $vac_request_date = 0;
149
150
    /**
151
     * @Column(type="boolean", nullable=true)
152
     *
153
     */
154
    private bool $storage_notification = true;
155
156
    /**
157
     * @Column(type="text")
158
     *
159
     */
160
    private string $description = '';
161
162
    /**
163
     * @Column(type="boolean", nullable=true)
164
     *
165
     */
166
    private bool $show_online_status = true;
167
168
    /**
169
     * @Column(type="boolean", nullable=true)
170
     *
171
     */
172
    private bool $show_pm_read_receipt = true;
173
174
    /**
175
     * @Column(type="boolean", nullable=true)
176
     *
177
     */
178
    private bool $save_login = true;
179
180
    /**
181
     * @Column(type="boolean", nullable=true)
182
     *
183
     */
184
    private bool $fleet_fixed_default = false;
185
186
    /**
187
     * @Column(type="boolean", nullable=true)
188
     *
189
     */
190
    private ?bool $warp_split_auto_carryover_default = null;
0 ignored issues
show
introduced by
The private property $warp_split_auto_carryover_default is not used, and could be removed.
Loading history...
191
192
    /**
193
     * @Column(type="smallint")
194
     *
195
     */
196
    private int $tick = 1;
197
198
    /**
199
     * @Column(type="smallint", nullable=true)
200
     *
201
     */
202
    private ?int $maptype = MapEnum::MAPTYPE_INSERT;
0 ignored issues
show
introduced by
The private property $maptype is not used, and could be removed.
Loading history...
203
204
    /**
205
     * @Column(type="text")
206
     *
207
     */
208
    private string $sessiondata = '';
209
210
    /**
211
     * @Column(type="string", length=255)
212
     *
213
     */
214
    private string $password_token = '';
215
216
    /**
217
     * @Column(type="string", length=7, nullable=true)
218
     *
219
     */
220
    private string $rgb_code = '';
221
222
    /**
223
     * @Column(type="integer")
224
     *
225
     */
226
    private int $prestige = 0;
227
228
    /**
229
     * @Column(type="string", length=100, nullable=true)
230
     *
231
     */
232
    private ?string $start_page = null;
233
234
    /**
235
     * @Column(type="smallint", length=1, enumType=UserRpgBehaviorEnum::class, nullable=true)
236
     *
237
     */
238
    private UserRpgBehaviorEnum $rpg_behavior = UserRpgBehaviorEnum::RPG_BEHAVIOR_NOT_SET;
239
240
    /**
241
     * @Column(type="string", length=100, enumType=UserCssClassEnum::class, nullable=true)
242
     *
243
     */
244
    private UserCssClassEnum $css = UserCssClassEnum::BLACK;
245
246
    /**
247
     * @Column(type="boolean", options={"default": false})
248
     *
249
     */
250
    private bool $deals = false;
251
252
    /**
253
     * @Column(type="integer", nullable=true)
254
     *
255
     */
256
    private ?int $last_boarding = null;
257
258
    /**
259
     *
260
     * @ManyToOne(targetEntity="Alliance", inversedBy="members")
261
     * @JoinColumn(name="allys_id", referencedColumnName="id")
262
     */
263
    private ?AllianceInterface $alliance = null;
264
265
    /**
266
     *
267
     * @ManyToOne(targetEntity="Faction")
268
     * @JoinColumn(name="race", referencedColumnName="id")
269
     */
270
    private FactionInterface $faction;
271
272
    /**
273
     * @var ArrayCollection<int, UserAwardInterface>
274
     *
275
     * @OneToMany(targetEntity="UserAward", mappedBy="user", indexBy="award_id")
276
     * @OrderBy({"award_id": "ASC"})
277
     */
278
    private Collection $awards;
279
280
    /**
281
     * @var ArrayCollection<int, ColonyInterface>
282
     *
283
     * @OneToMany(targetEntity="Colony", mappedBy="user", indexBy="id")
284
     * @OrderBy({"colonies_classes_id": "ASC", "id": "ASC"})
285
     */
286
    private Collection $colonies;
287
288
    /**
289
     * @var ArrayCollection<int, UserLayerInterface>
290
     *
291
     * @OneToMany(targetEntity="UserLayer", mappedBy="user", indexBy="layer_id")
292
     */
293
    private Collection $userLayers;
294
295
    /**
296
     * @OneToOne(targetEntity="UserLock", mappedBy="user")
297
     */
298
    private ?UserLockInterface $userLock = null;
299
300
    /**
301
     * @var ArrayCollection<string, UserSettingInterface>
302
     *
303
     * @OneToMany(targetEntity="UserSetting", mappedBy="user", indexBy="setting")
304
     */
305
    private Collection $settings;
306
307
    /** @var null|array<mixed> */
308
    private $sessiondataUnserialized;
309
310
    public function __construct()
311
    {
312
        $this->awards = new ArrayCollection();
313
        $this->colonies = new ArrayCollection();
314
        $this->userLayers = new ArrayCollection();
315
        $this->settings = new ArrayCollection();
316
    }
317
318
    public function getId(): int
319
    {
320
        return $this->id;
321
    }
322
323
    public function getName(): string
324
    {
325
        //if UMODE active, add info to user name
326
        if ($this->isVacationRequestOldEnough()) {
327
            return $this->username . '[b][color=red] (UMODE)[/color][/b]';
328
        }
329
        return $this->username;
330
    }
331
332
    public function setUsername(string $username): UserInterface
333
    {
334
        $this->username = $username;
335
        return $this;
336
    }
337
338
    public function getLogin(): string
339
    {
340
        return $this->login;
341
    }
342
343
    public function setLogin(string $login): UserInterface
344
    {
345
        $this->login = $login;
346
        return $this;
347
    }
348
349
    public function getPassword(): string
350
    {
351
        return $this->pass;
352
    }
353
354
    public function setPassword(string $password): UserInterface
355
    {
356
        $this->pass = $password;
357
        return $this;
358
    }
359
360
    public function getSmsCode(): ?string
361
    {
362
        return $this->sms_code;
363
    }
364
365
    public function setSmsCode(?string $code): UserInterface
366
    {
367
        $this->sms_code = $code;
368
        return $this;
369
    }
370
371
    public function getEmail(): string
372
    {
373
        return $this->email;
374
    }
375
376
    public function setEmail(string $email): UserInterface
377
    {
378
        $this->email = $email;
379
        return $this;
380
    }
381
382
    public function getMobile(): ?string
383
    {
384
        return $this->mobile;
385
    }
386
387
    public function setMobile(?string $mobile): UserInterface
388
    {
389
        $this->mobile = $mobile;
390
        return $this;
391
    }
392
393
    public function getRgbCode(): string
394
    {
395
        $setting = $this->getSettings()->get(UserSettingEnum::RGB_CODE->value);
396
        if ($setting !== null) {
397
            return $setting->getValue();
398
        }
399
400
        return $this->rgb_code;
401
    }
402
403
    public function getCss(): string
404
    {
405
        $setting = $this->getSettings()->get(UserSettingEnum::CSS_COLOR_SHEET->value);
406
        if ($setting !== null) {
407
            return $setting->getValue();
408
        }
409
410
        return $this->css->value;
411
    }
412
413
    public function getFactionId(): int
414
    {
415
        return $this->race;
416
    }
417
418
    public function setFaction(FactionInterface $faction): UserInterface
419
    {
420
        $this->faction = $faction;
421
        return $this;
422
    }
423
424
    public function getFaction(): FactionInterface
425
    {
426
        return $this->faction;
427
    }
428
429
    public function getAwards(): Collection
430
    {
431
        return $this->awards;
432
    }
433
434
    public function getColonies(): Collection
435
    {
436
        return $this->colonies;
437
    }
438
439
    public function hasColony(): bool
440
    {
441
        if (
442
            $this->getState() === UserEnum::USER_STATE_COLONIZATION_SHIP
443
            || $this->getState() === UserEnum::USER_STATE_UNCOLONIZED
444
        ) {
445
            return false;
446
        }
447
448
        return !$this->getColonies()->isEmpty();
449
    }
450
451
    public function getState(): int
452
    {
453
        return $this->state;
454
    }
455
456
    public function isLocked(): bool
457
    {
458
        return $this->getUserLock() !== null && $this->getUserLock()->getRemainingTicks() > 0;
459
    }
460
461
    public function getUserStateDescription(): string
462
    {
463
        if ($this->isLocked()) {
464
            return _('GESPERRT');
465
        }
466
        return UserEnum::getUserStateDescription($this->getState());
467
    }
468
469
    public function setState(int $state): UserInterface
470
    {
471
        $this->state = $state;
472
        return $this;
473
    }
474
475
    public function getAvatar(): string
476
    {
477
        $setting = $this->getSettings()->get(UserSettingEnum::AVATAR->value);
478
        if ($setting !== null) {
479
            return $setting->getValue();
480
        }
481
482
        return $this->propic;
483
    }
484
485
    public function isEmailNotification(): bool
486
    {
487
        $setting = $this->getSettings()->get(UserSettingEnum::EMAIL_NOTIFICATION->value);
488
        if ($setting !== null) {
489
            return (bool)$setting->getValue();
490
        }
491
492
        return $this->email_notification;
493
    }
494
495
    public function getLastaction(): int
496
    {
497
        return $this->lastaction;
498
    }
499
500
    public function setLastaction(int $lastaction): UserInterface
501
    {
502
        $this->lastaction = $lastaction;
503
        return $this;
504
    }
505
506
    public function getCreationDate(): int
507
    {
508
        return $this->creation;
509
    }
510
511
    public function setCreationDate(int $creationDate): UserInterface
512
    {
513
        $this->creation = $creationDate;
514
        return $this;
515
    }
516
517
    public function getKnMark(): int
518
    {
519
        return $this->kn_lez;
520
    }
521
522
    public function setKnMark(int $knMark): UserInterface
523
    {
524
        $this->kn_lez = $knMark;
525
        return $this;
526
    }
527
528
    public function getDeletionMark(): int
529
    {
530
        return $this->delmark;
531
    }
532
533
    public function setDeletionMark(int $deletionMark): UserInterface
534
    {
535
        $this->delmark = $deletionMark;
536
        return $this;
537
    }
538
539
    public function isVacationMode(): bool
540
    {
541
        return $this->vac_active;
542
    }
543
544
    public function setVacationMode(bool $vacationMode): UserInterface
545
    {
546
        $this->vac_active = $vacationMode;
547
        return $this;
548
    }
549
550
    public function getVacationRequestDate(): int
551
    {
552
        return $this->vac_request_date;
553
    }
554
555
    public function setVacationRequestDate(int $date): UserInterface
556
    {
557
        $this->vac_request_date = $date;
558
559
        return $this;
560
    }
561
562
    public function isVacationRequestOldEnough(): bool
563
    {
564
        return $this->isVacationMode() && (time() - $this->getVacationRequestDate() > UserEnum::VACATION_DELAY_IN_SECONDS);
565
    }
566
567
    public function isStorageNotification(): bool
568
    {
569
        $setting = $this->getSettings()->get(UserSettingEnum::STORAGE_NOTIFICATION->value);
570
        if ($setting !== null) {
571
            return (bool)$setting->getValue();
572
        }
573
574
        return $this->storage_notification;
575
    }
576
577
    public function getDescription(): string
578
    {
579
        return $this->description;
580
    }
581
582
    public function setDescription(string $description): UserInterface
583
    {
584
        $this->description = $description;
585
        return $this;
586
    }
587
588
    public function isShowOnlineState(): bool
589
    {
590
        $setting = $this->getSettings()->get(UserSettingEnum::SHOW_ONLINE_STATUS->value);
591
        if ($setting !== null) {
592
            return (bool)$setting->getValue();
593
        }
594
595
        return $this->show_online_status;
596
    }
597
598
    public function isShowPmReadReceipt(): bool
599
    {
600
        $setting = $this->getSettings()->get(UserSettingEnum::SHOW_PM_READ_RECEIPT->value);
601
        if ($setting !== null) {
602
            return (bool)$setting->getValue();
603
        }
604
605
        return $this->show_pm_read_receipt;
606
    }
607
608
    public function isSaveLogin(): bool
609
    {
610
        $setting = $this->getSettings()->get(UserSettingEnum::SAVE_LOGIN->value);
611
        if ($setting !== null) {
612
            return (bool)$setting->getValue();
613
        }
614
615
        return $this->save_login;
616
    }
617
618
    public function getFleetFixedDefault(): bool
619
    {
620
        $setting = $this->getSettings()->get(UserSettingEnum::FLEET_FIXED_DEFAULT->value);
621
        if ($setting !== null) {
622
            return (bool)$setting->getValue();
623
        }
624
625
        return $this->fleet_fixed_default;
626
    }
627
628
    public function getWarpsplitAutoCarryoverDefault(): bool
629
    {
630
        $setting = $this->getSettings()->get(UserSettingEnum::WARPSPLIT_AUTO_CARRYOVER_DEFAULT->value);
631
        if ($setting !== null) {
632
            return (bool)$setting->getValue();
633
        }
634
635
        return false;
636
    }
637
638
    public function getTick(): int
639
    {
640
        return $this->tick;
641
    }
642
643
    public function setTick(int $tick): UserInterface
644
    {
645
        $this->tick = $tick;
646
        return $this;
647
    }
648
649
    public function getUserLayers(): Collection
650
    {
651
        return $this->userLayers;
652
    }
653
654
    public function hasSeen(int $layerId): bool
655
    {
656
        return $this->getUserLayers()->containsKey($layerId);
657
    }
658
659
    public function hasExplored(int $layerId): bool
660
    {
661
        return $this->hasSeen($layerId) && $this->getUserLayers()->get($layerId)->isExplored();
662
    }
663
664
    public function getSettings(): Collection
665
    {
666
        return $this->settings;
667
    }
668
669
    public function getSessiondata(): string
670
    {
671
        return $this->sessiondata;
672
    }
673
674
    public function setSessiondata(string $sessiondata): UserInterface
675
    {
676
        $this->sessiondata = $sessiondata;
677
        $this->sessiondataUnserialized = null;
678
        return $this;
679
    }
680
681
    public function getPasswordToken(): string
682
    {
683
        return $this->password_token;
684
    }
685
686
    public function setPasswordToken(string $password_token): UserInterface
687
    {
688
        $this->password_token = $password_token;
689
        return $this;
690
    }
691
692
    public function getPrestige(): int
693
    {
694
        return $this->prestige;
695
    }
696
697
    public function setPrestige(int $prestige): UserInterface
698
    {
699
        $this->prestige = $prestige;
700
        return $this;
701
    }
702
703
    public function getDefaultView(): ModuleViewEnum
704
    {
705
        $setting = $this->getSettings()->get(UserSettingEnum::DEFAULT_VIEW->value);
706
        if ($setting !== null) {
707
            return ModuleViewEnum::from($setting->getValue());
708
        }
709
710
        if ($this->start_page === null) {
711
            return ModuleViewEnum::MAINDESK;
712
        }
713
714
        return ModuleViewEnum::from($this->start_page);
715
    }
716
717
    public function getRpgBehavior(): UserRpgBehaviorEnum
718
    {
719
        $setting = $this->getSettings()->get(UserSettingEnum::RPG_BEHAVIOR->value);
720
        if ($setting !== null) {
721
            return UserRpgBehaviorEnum::from((int)$setting->getValue());
722
        }
723
724
        return $this->rpg_behavior;
725
    }
726
727
    public function getDeals(): bool
728
    {
729
        return $this->deals;
730
    }
731
732
    public function setDeals(bool $deals): UserInterface
733
    {
734
        $this->deals = $deals;
735
        return $this;
736
    }
737
738
    public function getLastBoarding(): ?int
739
    {
740
        return $this->last_boarding;
741
    }
742
743
    public function setLastBoarding(int $lastBoarding): UserInterface
744
    {
745
        $this->last_boarding = $lastBoarding;
746
        return $this;
747
    }
748
749
    public function isOnline(): bool
750
    {
751
        return !($this->getLastAction() < time() - GameEnum::USER_ONLINE_PERIOD);
752
    }
753
754
    public function getAlliance(): ?AllianceInterface
755
    {
756
        return $this->alliance;
757
    }
758
759
    public function setAlliance(?AllianceInterface $alliance): UserInterface
760
    {
761
        $this->alliance = $alliance;
762
        return $this;
763
    }
764
765
    public function setAllianceId(?int $allianceId): UserInterface
766
    {
767
        $this->allys_id = $allianceId;
768
        return $this;
769
    }
770
771
    public function getSessionDataUnserialized(): array
772
    {
773
        if ($this->sessiondataUnserialized === null) {
774
            $this->sessiondataUnserialized = unserialize($this->getSessionData());
775
            if (!is_array($this->sessiondataUnserialized)) {
776
                $this->sessiondataUnserialized = [];
777
            }
778
        }
779
        return $this->sessiondataUnserialized;
780
    }
781
782
    public function isContactable(): bool
783
    {
784
        return $this->getId() != UserEnum::USER_NOONE;
785
    }
786
787
    public function hasAward(int $awardId): bool
788
    {
789
        return $this->awards->containsKey($awardId) === true;
790
    }
791
792
    public function hasStationsNavigation(): bool
793
    {
794
        if ($this->isNpc()) {
795
            return true;
796
        }
797
798
        return $this->hasAward(UserAwardEnum::RESEARCHED_STATIONS);
799
    }
800
801
    public function isNpc(): bool
802
    {
803
        return $this->getId() < UserEnum::USER_FIRST_ID;
804
    }
805
806
    public function getUserLock(): ?UserLockInterface
807
    {
808
        return $this->userLock;
809
    }
810
811
    public function __toString(): string
812
    {
813
        return sprintf('userName: %s', $this->getName());
814
    }
815
816
    public function hasTranslation(): bool
817
    {
818
        $text = $this->getDescription();
819
        return strpos($text, '[translate]') !== false && strpos($text, '[/translate]') !== false;
820
    }
821
}
822