Passed
Push — dev ( 216c8f...156d5c )
by Janko
15:03
created

UserSettingWrapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 4
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Module\PlayerSetting\Lib;
4
5
use RuntimeException;
6
use Stu\Component\Game\ModuleEnum;
7
use Stu\Component\Player\Settings\UserSettingsProviderInterface;
8
use Stu\Component\Player\UserCssClassEnum;
9
use Stu\Component\Player\UserRpgBehaviorEnum;
10
use Stu\Orm\Entity\UserInterface;
11
12
class UserSettingWrapper
13
{
14 1
    public function __construct(
15
        private readonly UserInterface $user,
16
        private readonly UserSettingEnum $type,
17
        private readonly bool $isAdmin,
18
        private readonly UserSettingsProviderInterface $userSettingsProvider
19 1
    ) {}
20
21 1
    public function getType(): UserSettingEnum
22
    {
23 1
        return $this->type;
24
    }
25
26 1
    public function getParameterName(): string
27
    {
28 1
        return $this->type->value;
29
    }
30
31 1
    public function getCurrentValue(): mixed
32
    {
33 1
        return $this->type->getUserValue($this->user, $this->userSettingsProvider);
34
    }
35
36
    /** @return array<mixed> */
37 1
    public function getPossibleValues(): array
38
    {
39 1
        return match ($this->type) {
40 1
            UserSettingEnum::CSS_COLOR_SHEET => UserCssClassEnum::cases(),
41 1
            UserSettingEnum::RPG_BEHAVIOR => UserRpgBehaviorEnum::cases(),
42 1
            UserSettingEnum::DEFAULT_VIEW => $this->getFilteredViews($this->user, $this->isAdmin),
43 1
            default => throw new RuntimeException(sprintf('%s is not an enum', $this->type->name))
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Stu\Module\PlayerSetting\Lib\UserSettingEnum.
Loading history...
44 1
        };
45
    }
46
47 1
    public function getEnctype(): ?string
48
    {
49 1
        return match ($this->type) {
50 1
            UserSettingEnum::AVATAR => "multipart/form-data",
51 1
            default => null
52 1
        };
53
    }
54
55
    /** @return array<ModuleEnum> */
56 1
    private function getFilteredViews(UserInterface $user, bool $isAdmin): array
57
    {
58 1
        return array_filter(ModuleEnum::cases(), function (ModuleEnum $case) use ($user, $isAdmin): bool {
59
60 1
            if (in_array($case, [ModuleEnum::GAME, ModuleEnum::INDEX, ModuleEnum::NOTES])) {
61 1
                return false;
62
            }
63
64 1
            if ($case === ModuleEnum::ADMIN && !$isAdmin) {
65 1
                return false;
66
            }
67 1
            return !($case === ModuleEnum::NPC && !$user->isNpc());
68 1
        });
69
    }
70
}
71