User::setState()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
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 LogicException;
21
use Stu\Component\Faction\FactionEnum;
22
use Stu\Component\Game\GameEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Game\GameEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Stu\Module\PlayerSetting\Lib\UserConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserConstants was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Stu\Module\PlayerSetting\Lib\UserStateEnum;
25
use Stu\Orm\Repository\UserRepository;
26
27
#[Table(name: 'stu_user')]
28
#[Index(name: 'user_alliance_idx', columns: ['allys_id'])]
29
#[Entity(repositoryClass: UserRepository::class)]
30
class User
31
{
32
    #[Id]
33
    #[Column(type: 'integer')]
34
    #[GeneratedValue(strategy: 'IDENTITY')]
35
    private int $id;
36
37
    #[Column(type: 'string')]
38
    private string $username = '';
39
40
    #[Column(type: 'integer', nullable: true)]
41
    private ?int $allys_id = null;
0 ignored issues
show
introduced by
The private property $allys_id is not used, and could be removed.
Loading history...
42
43
    #[Column(type: 'integer', enumType: FactionEnum::class)]
44
    private FactionEnum $faction_id = FactionEnum::FACTION_FEDERATION;
0 ignored issues
show
introduced by
The private property $faction_id is not used, and could be removed.
Loading history...
45
46
    #[Column(type: 'smallint', enumType: UserStateEnum::class)]
47
    private UserStateEnum $state = UserStateEnum::NEW;
48
49
    #[Column(type: 'integer')]
50
    private int $lastaction = 0;
51
52
    #[Column(type: 'integer')]
53
    private int $kn_lez = 0;
54
55
    #[Column(type: 'boolean')]
56
    private bool $vac_active = false;
57
58
    #[Column(type: 'integer')]
59
    private int $vac_request_date = 0;
60
61
    #[Column(type: 'text')]
62
    private string $description = '';
63
64
    #[Column(type: 'text')]
65
    private string $sessiondata = '';
66
67
    #[Column(type: 'integer')]
68
    private int $prestige = 0;
69
70
    #[Column(type: 'boolean')]
71
    private bool $deals = false;
72
73
    #[Column(type: 'integer', nullable: true)]
74
    private ?int $last_boarding = null;
75
76
    #[OneToOne(targetEntity: UserRegistration::class, mappedBy: 'user', cascade: ['all'])]
77
    private ?UserRegistration $registration;
78
79
    #[ManyToOne(targetEntity: Alliance::class, inversedBy: 'members')]
80
    #[JoinColumn(name: 'allys_id', referencedColumnName: 'id')]
81
    private ?Alliance $alliance = null;
82
83
    #[ManyToOne(targetEntity: Faction::class)]
84
    #[JoinColumn(name: 'faction_id', nullable: false, referencedColumnName: 'id')]
85
    private Faction $faction;
86
87
    /**
88
     * @var ArrayCollection<int, Buoy>
89
     */
90
    #[OneToMany(targetEntity: Buoy::class, mappedBy: 'user')]
91
    private Collection $buoys;
92
93
    /**
94
     * @var ArrayCollection<int, UserAward>
95
     */
96
    #[OneToMany(targetEntity: UserAward::class, mappedBy: 'user', indexBy: 'award_id')]
97
    #[OrderBy(['award_id' => 'ASC'])]
98
    private Collection $awards;
99
100
    /**
101
     * @var ArrayCollection<int, Colony>
102
     */
103
    #[OneToMany(targetEntity: Colony::class, mappedBy: 'user', indexBy: 'id')]
104
    #[OrderBy(['colonies_classes_id' => 'ASC', 'id' => 'ASC'])]
105
    private Collection $colonies;
106
107
    /**
108
     * @var ArrayCollection<int, UserLayer>
109
     */
110
    #[OneToMany(targetEntity: UserLayer::class, mappedBy: 'user', indexBy: 'layer_id')]
111
    private Collection $userLayers;
112
113
    #[OneToOne(targetEntity: UserLock::class, mappedBy: 'user')]
114
    private ?UserLock $userLock = null;
115
116
    /**
117
     * @var ArrayCollection<string, UserSetting>
118
     */
119
    #[OneToMany(targetEntity: UserSetting::class, mappedBy: 'user', indexBy: 'setting')]
120
    private Collection $settings;
121
122
    /**
123
     * @var ArrayCollection<int, UserCharacter>
124
     */
125
    #[OneToMany(targetEntity: UserCharacter::class, mappedBy: 'user', fetch: 'EXTRA_LAZY')]
126
    private Collection $characters;
127
128
    /**
129
     * @var ArrayCollection<int, ColonyScan>
130
     */
131
    #[OneToMany(targetEntity: ColonyScan::class, mappedBy: 'user', indexBy: 'id', fetch: 'EXTRA_LAZY')]
132
    #[OrderBy(['colony_id' => 'ASC', 'date' => 'ASC'])]
133
    private Collection $colonyScans;
134
135
    #[OneToOne(targetEntity: PirateWrath::class, mappedBy: 'user')]
136
    private ?PirateWrath $pirateWrath = null;
137
138
    /**
139
     * @var ArrayCollection<int, UserTutorial>
140
     */
141
    #[OneToMany(targetEntity: UserTutorial::class, mappedBy: 'user', indexBy: 'tutorial_step_id', fetch: 'EXTRA_LAZY')]
142
    private Collection $tutorials;
143
144
    /**
145
     * @var ArrayCollection<int, WormholeRestriction>
146
     */
147
    #[OneToMany(targetEntity: WormholeRestriction::class, mappedBy: 'user', fetch: 'EXTRA_LAZY')]
148
    private Collection $wormholeRestrictions;
149
150
    public function __construct()
151
    {
152
        $this->awards = new ArrayCollection();
153
        $this->colonies = new ArrayCollection();
154
        $this->userLayers = new ArrayCollection();
155
        $this->settings = new ArrayCollection();
156
        $this->characters = new ArrayCollection();
157
        $this->buoys = new ArrayCollection();
158
        $this->colonyScans = new ArrayCollection();
159
        $this->tutorials = new ArrayCollection();
160
        $this->wormholeRestrictions = new ArrayCollection();
161
    }
162
163 210
    public function getId(): int
164
    {
165 210
        return $this->id;
166
    }
167
168 3
    public function getRegistration(): UserRegistration
169
    {
170 3
        return $this->registration ?? throw new LogicException('User has no registration');
171
    }
172
173
    public function setRegistration(UserRegistration $registration): User
174
    {
175
        $this->registration = $registration;
176
177
        return $this;
178
    }
179
180 210
    public function getName(): string
181
    {
182
        //if UMODE active, add info to user name
183 210
        if ($this->isVacationRequestOldEnough()) {
184
            return $this->username . '[b][color=red] (UMODE)[/color][/b]';
185
        }
186 210
        return $this->username;
187
    }
188
189
    public function setUsername(string $username): User
190
    {
191
        $this->username = $username;
192
        return $this;
193
    }
194
195 210
    public function getFactionId(): int
196
    {
197 210
        return $this->faction->getId();
198
    }
199
200
    public function setFaction(Faction $faction): User
201
    {
202
        $this->faction = $faction;
203
        return $this;
204
    }
205
206 12
    public function getFaction(): Faction
207
    {
208 12
        return $this->faction;
209
    }
210
211
    /**
212
     * @return Collection<int, UserAward>
213
     */
214 1
    public function getAwards(): Collection
215
    {
216 1
        return $this->awards;
217
    }
218
219
    /**
220
     * @return Collection<int, Colony>
221
     */
222 10
    public function getColonies(): Collection
223
    {
224 10
        return $this->colonies;
225
    }
226
227 1
    public function hasColony(): bool
228
    {
229 1
        return $this->getState() === UserStateEnum::ACTIVE && !$this->getColonies()->isEmpty();
230
    }
231
232 5
    public function getState(): UserStateEnum
233
    {
234 5
        return $this->state;
235
    }
236
237 1
    public function isLocked(): bool
238
    {
239 1
        return $this->getUserLock() !== null && $this->getUserLock()->getRemainingTicks() > 0;
240
    }
241
242
    public function setState(UserStateEnum $state): User
243
    {
244
        $this->state = $state;
245
        return $this;
246
    }
247
248 4
    public function getLastaction(): int
249
    {
250 4
        return $this->lastaction;
251
    }
252
253
    public function setLastaction(int $lastaction): User
254
    {
255
        $this->lastaction = $lastaction;
256
        return $this;
257
    }
258
259 4
    public function getKnMark(): int
260
    {
261 4
        return $this->kn_lez;
262
    }
263
264
    public function setKnMark(int $knMark): User
265
    {
266
        $this->kn_lez = $knMark;
267
        return $this;
268
    }
269
270 210
    public function isVacationMode(): bool
271
    {
272 210
        return $this->vac_active;
273
    }
274
275
    public function setVacationMode(bool $vacationMode): User
276
    {
277
        $this->vac_active = $vacationMode;
278
        return $this;
279
    }
280
281
    public function getVacationRequestDate(): int
282
    {
283
        return $this->vac_request_date;
284
    }
285
286
    public function setVacationRequestDate(int $date): User
287
    {
288
        $this->vac_request_date = $date;
289
290
        return $this;
291
    }
292
293 210
    public function isVacationRequestOldEnough(): bool
294
    {
295 210
        return $this->isVacationMode() && (time() - $this->getVacationRequestDate() > UserConstants::VACATION_DELAY_IN_SECONDS);
296
    }
297
298 2
    public function getDescription(): string
299
    {
300 2
        return $this->description;
301
    }
302
303
    public function setDescription(string $description): User
304
    {
305
        $this->description = $description;
306
        return $this;
307
    }
308
309
    /**
310
     * @return Collection<int, UserLayer>
311
     */
312 5
    public function getUserLayers(): Collection
313
    {
314 5
        return $this->userLayers;
315
    }
316
317
    /**
318
     * @return Collection<string, UserSetting>
319
     */
320 210
    public function getSettings(): Collection
321
    {
322 210
        return $this->settings;
323
    }
324
325 1
    public function getSessiondata(): string
326
    {
327 1
        return $this->sessiondata;
328
    }
329
330
    public function setSessiondata(string $sessiondata): User
331
    {
332
        $this->sessiondata = $sessiondata;
333
        return $this;
334
    }
335
336 3
    public function getPrestige(): int
337
    {
338 3
        return $this->prestige;
339
    }
340
341
    public function setPrestige(int $prestige): User
342
    {
343
        $this->prestige = $prestige;
344
        return $this;
345
    }
346
347 3
    public function getDeals(): bool
348
    {
349 3
        return $this->deals;
350
    }
351
352 1
    public function setDeals(bool $deals): User
353
    {
354 1
        $this->deals = $deals;
355 1
        return $this;
356
    }
357
358
    public function getLastBoarding(): ?int
359
    {
360
        return $this->last_boarding;
361
    }
362
363
    public function setLastBoarding(int $lastBoarding): User
364
    {
365
        $this->last_boarding = $lastBoarding;
366
        return $this;
367
    }
368
369 2
    public function isOnline(): bool
370
    {
371 2
        return !($this->getLastAction() < time() - GameEnum::USER_ONLINE_PERIOD);
372
    }
373
374 20
    public function getAlliance(): ?Alliance
375
    {
376 20
        return $this->alliance;
377
    }
378
379
    public function setAlliance(?Alliance $alliance): User
380
    {
381
        $this->alliance = $alliance;
382
        return $this;
383
    }
384
385 1
    public function isContactable(): bool
386
    {
387 1
        return $this->getId() != UserConstants::USER_NOONE;
388
    }
389
390 209
    public function hasAward(int $awardId): bool
391
    {
392 209
        return $this->awards->containsKey($awardId) === true;
393
    }
394
395 214
    public static function isUserNpc(int $userId): bool
396
    {
397 214
        return $userId < UserConstants::USER_FIRST_ID;
398
    }
399
400 210
    public function isNpc(): bool
401
    {
402 210
        return self::isUserNpc($this->getId());
403
    }
404
405 1
    public function getUserLock(): ?UserLock
406
    {
407 1
        return $this->userLock;
408
    }
409
410
    public function __toString(): string
411
    {
412
        return sprintf('userName: %s', $this->getName());
413
    }
414
415
    /**
416
     * @return Collection<int, UserCharacter>
417
     */
418 1
    public function getCharacters(): Collection
419
    {
420 1
        return $this->characters;
421
    }
422
423
    /**
424
     * @return Collection<int, ColonyScan>
425
     */
426 1
    public function getColonyScans(): Collection
427
    {
428 1
        return $this->colonyScans;
429
    }
430
431
    /**
432
     * @return Collection<int, Buoy>
433
     */
434
    public function getBuoys(): Collection
435
    {
436
        return $this->buoys;
437
    }
438
439 2
    public function getPirateWrath(): ?PirateWrath
440
    {
441 2
        return $this->pirateWrath;
442
    }
443
444
    public function setPirateWrath(?PirateWrath $wrath): User
445
    {
446
        $this->pirateWrath = $wrath;
447
448
        return $this;
449
    }
450
451
    /**
452
     * @return Collection<int, UserTutorial>
453
     */
454
    public function getTutorials(): Collection
455
    {
456
        return $this->tutorials;
457
    }
458
459
    /**
460
     * @return Collection<int, WormholeRestriction>
461
     */
462
    public function getWormholeRestrictions(): Collection
463
    {
464
        return $this->wormholeRestrictions;
465
    }
466
}
467