NormalizePlayer::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 17
c 3
b 0
f 0
dl 0
loc 20
ccs 16
cts 16
cp 1
rs 9.7
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AardsGerds\Game\Infrastructure\Persistence;
6
7
use AardsGerds\Game\Build\Talent\SecretKnowledge\SecretKnowledge;
8
use AardsGerds\Game\Build\Talent\Talent;
9
use AardsGerds\Game\Build\Talent\TalentCollection;
10
use AardsGerds\Game\Build\Talent\WeaponMastery\WeaponMastery;
11
use AardsGerds\Game\Inventory\Inventory;
12
use AardsGerds\Game\Inventory\InventoryItem;
13
use AardsGerds\Game\Inventory\Weapon\RebredirWeapon;
14
use AardsGerds\Game\Inventory\Weapon\Weapon;
15
use AardsGerds\Game\Player\Player;
16
use function Lambdish\Phunctional\map;
17
18
final class NormalizePlayer
19
{
20 1
    public function __invoke(Player $player): array
21
    {
22
        return [
23 1
            'name' => $player->getName(),
24 1
            'health' => $player->getHealth()->get(),
25 1
            'etherum' => $player->getEtherum()->get(),
26 1
            'strength' => $player->getStrength()->get(),
27 1
            'initiative' => $player->getInitiative()->get(),
28 1
            'talents' => self::normalizeTalents($player->getTalents()),
29 1
            'inventory' => self::normalizeInventory($player->getInventory()),
30 1
            'weapon' => $player->getWeapon() !== null ? self::normalizeWeapon($player->getWeapon()) : null,
31 1
            'corruption' => $player->getCorruption()?->get(),
32
            'levelProgress' => [
33 1
                'level' => $player->getLevelProgress()->getLevel()->get(),
34 1
                'currentExperience' => $player->getLevelProgress()->getCurrentExperience()->get(),
35
            ],
36 1
            'maximumHealth' => $player->getMaximumHealth()->get(),
37 1
            'attributePoints' => $player->getAttributePoints()->get(),
38 1
            'talentPoints' => $player->getTalentPoints()->get(),
39 1
            'checkpoint' => ['className' => get_class($player->getCheckpoint())],
40
        ];
41
    }
42
43 1
    private static function normalizeTalents(TalentCollection $talentCollection): array
44
    {
45 1
        return map(
46 1
            static function(Talent $talent): array {
47 1
                $data = ['className' => get_class($talent)];
48
49
                return match (true) {
50 1
                    $talent instanceof SecretKnowledge => array_merge($data, [
51 1
                        'ascension' => $talent->getAscension()->get(),
0 ignored issues
show
Bug introduced by
The method getAscension() does not exist on AardsGerds\Game\Build\Talent\Talent. It seems like you code against a sub-type of AardsGerds\Game\Build\Talent\Talent such as AardsGerds\Game\Build\Ta...owledge\SecretKnowledge. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
                        'ascension' => $talent->/** @scrutinizer ignore-call */ getAscension()->get(),
Loading history...
52
                    ]),
53 1
                    $talent instanceof WeaponMastery => array_merge($data, [
54 1
                        'type' => (string) $talent->getType(),
0 ignored issues
show
Bug introduced by
The method getType() does not exist on AardsGerds\Game\Build\Talent\Talent. It seems like you code against a sub-type of AardsGerds\Game\Build\Talent\Talent such as AardsGerds\Game\Build\Ta...onMastery\WeaponMastery. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
                        'type' => (string) $talent->/** @scrutinizer ignore-call */ getType(),
Loading history...
55 1
                        'level' => $talent->getLevel()->get(),
0 ignored issues
show
Bug introduced by
The method getLevel() does not exist on AardsGerds\Game\Build\Talent\Talent. It seems like you code against a sub-type of AardsGerds\Game\Build\Talent\Talent such as AardsGerds\Game\Build\Ta...onMastery\WeaponMastery. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
                        'level' => $talent->/** @scrutinizer ignore-call */ getLevel()->get(),
Loading history...
56
                    ]),
57 1
                    default => $data,
58
                };
59 1
            },
60 1
            $talentCollection,
61
        );
62
    }
63
64 1
    private static function normalizeWeapon(Weapon $weapon): array
65
    {
66 1
        $data = ['className' => get_class($weapon)];
67
68
        return match (true) {
69 1
            $weapon instanceof RebredirWeapon => array_merge($data, [
70 1
                'etherumLoad' => $weapon->getEtherumLoad()->get(),
0 ignored issues
show
Bug introduced by
The method getEtherumLoad() does not exist on AardsGerds\Game\Inventory\Weapon\Weapon. It seems like you code against a sub-type of AardsGerds\Game\Inventory\Weapon\Weapon such as AardsGerds\Game\Inventory\Weapon\GreatSword\Amuril or AardsGerds\Game\Inventor...on\GreatSword\Protector. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
                'etherumLoad' => $weapon->/** @scrutinizer ignore-call */ getEtherumLoad()->get(),
Loading history...
71
            ]),
72 1
            default => $data,
73
        };
74
    }
75
76 1
    private static function normalizeInventory(Inventory $inventory): array
77
    {
78 1
        return map(
79 1
            static function(InventoryItem $inventoryItem): array {
80
                return match (true) {
81 1
                    $inventoryItem instanceof Weapon => self::normalizeWeapon($inventoryItem),
82 1
                    default => ['className' => get_class($inventoryItem)],
83
                };
84 1
            },
85 1
            $inventory,
86
        );
87
    }
88
}
89