Passed
Push — master ( 4d4163...319ade )
by Nico
10:25 queued 04:57
created

WormholeRestrictionItem::getAllianceName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib;
6
7
use Stu\Component\Ship\Wormhole\WormholeEntryModeEnum;
8
use Stu\Component\Ship\Wormhole\WormholeEntryTypeEnum;
9
use Stu\Orm\Entity\WormholeRestriction;
10
use Stu\Orm\Repository\AllianceRepositoryInterface;
11
use Stu\Orm\Repository\FactionRepositoryInterface;
12
use Stu\Orm\Repository\UserRepositoryInterface;
13
14
final class WormholeRestrictionItem
15
{
16
    public function __construct(
17
        private UserRepositoryInterface $userRepository,
18
        private AllianceRepositoryInterface $allianceRepository,
19
        private FactionRepositoryInterface $factionRepository,
20
        private WormholeRestriction $restriction
21
    ) {}
22
23
    public function getId(): int
24
    {
25
        return $this->restriction->getId();
26
    }
27
28
    public function getTargetName(): string
29
    {
30
        $type = $this->restriction->getPrivilegeType();
31
        if ($type === null) {
32
            return 'Unbekannt';
33
        }
34
35
        return match ($type) {
36
            WormholeEntryTypeEnum::USER => $this->getUserName(),
37
            WormholeEntryTypeEnum::ALLIANCE => $this->getAllianceName(),
38
            WormholeEntryTypeEnum::FACTION => $this->getFactionName(),
39
            WormholeEntryTypeEnum::SHIP => 'Schiff ' . $this->restriction->getTargetId()
40
        };
41
    }
42
43
    public function getTypeName(): string
44
    {
45
        $type = $this->restriction->getPrivilegeType();
46
        if ($type === null) {
47
            return 'Unbekannt';
48
        }
49
50
        return match ($type) {
51
            WormholeEntryTypeEnum::USER => 'Spieler',
52
            WormholeEntryTypeEnum::ALLIANCE => 'Allianz',
53
            WormholeEntryTypeEnum::FACTION => 'Rasse',
54
            WormholeEntryTypeEnum::SHIP => 'Schiff'
55
        };
56
    }
57
58
    private function getUserName(): string
59
    {
60
        $user = $this->userRepository->find($this->restriction->getTargetId());
61
        return $user === null
62
            ? 'Unbekannter Spieler'
63
            : $user->getName();
64
    }
65
66
    private function getAllianceName(): string
67
    {
68
        $ally = $this->allianceRepository->find($this->restriction->getTargetId());
69
        return $ally === null
70
            ? 'Unbekannte Allianz'
71
            : $ally->getName();
72
    }
73
74
    private function getFactionName(): string
75
    {
76
        $faction = $this->factionRepository->find($this->restriction->getTargetId());
77
        return $faction === null
78
            ? 'Unbekannte Rasse'
79
            : $faction->getName();
80
    }
81
82
    public function getPrivilegeModeString(): string
83
    {
84
        $mode = $this->restriction->getMode();
85
        return $mode === WormholeEntryModeEnum::ALLOW->value ? 'Erlauben' : 'Verbieten';
86
    }
87
88
    public function isAllowed(): bool
89
    {
90
        return $this->restriction->getMode() === WormholeEntryModeEnum::ALLOW->value;
91
    }
92
93
    public function getEntry(): object
94
    {
95
        return $this->restriction->getWormholeEntry();
96
    }
97
}