Passed
Push — master ( cbe5a3...403d23 )
by Paweł
02:46
created

NormalizePlayer::normalizeTalents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AardsGerds\Game\Infrastructure\Persistence;
6
7
use AardsGerds\Game\Build\Talent\Talent;
8
use AardsGerds\Game\Build\Talent\TalentCollection;
9
use AardsGerds\Game\Inventory\Inventory;
10
use AardsGerds\Game\Inventory\InventoryItem;
11
use AardsGerds\Game\Inventory\Weapon\RebredirWeapon;
12
use AardsGerds\Game\Inventory\Weapon\Weapon;
13
use AardsGerds\Game\Player\Player;
14
15
final class NormalizePlayer
16
{
17
    public function __invoke(Player $player): array
18
    {
19
        return [
20
            'name' => $player->getName(),
21
            'health' => $player->getHealth()->get(),
22
            'etherum' => $player->getEtherum()->get(),
23
            'strength' => $player->getStrength()->get(),
24
            'talents' => $this->normalizeTalents($player->getTalents()),
25
            'inventory' => $this->normalizeInventory($player->getInventory()),
26
            'weapon' => $player->getWeapon() !== null ? $this->normalizeWeapon($player->getWeapon()) : null,
27
            'corrupted' => $player->isCorrupted(),
28
            'levelProgress' => [
29
                'level' => $player->getLevelProgress()->getLevel()->get(),
30
                'currentExperience' => $player->getLevelProgress()->getCurrentExperience()->get(),
31
            ],
32
            'attributePoints' => $player->getAttributePoints()->get(),
33
            'talentPoints' => $player->getTalentPoints()->get(),
34
        ];
35
    }
36
37
    private function normalizeWeapon(Weapon $weapon): array
38
    {
39
        $data = ['className' => $className = get_class($weapon)];
40
41
        if (in_array(RebredirWeapon::class, class_uses($className))) {
42
            /** @var RebredirWeapon $weapon */
43
            $data = array_merge($data, ['etherumLoad' => $weapon->getEtherumLoad()]);
44
        }
45
46
        return $data;
47
    }
48
49
    private function normalizeTalents(TalentCollection $talentCollection): array
50
    {
51
        return array_map(
52
            static fn(Talent $talent): array => [
53
                'className' => get_class($talent),
54
            ],
55
            $talentCollection->getItems(),
56
        );
57
    }
58
59
    private function normalizeInventory(Inventory $inventory): array
60
    {
61
        return array_map(
62
            static fn(InventoryItem $inventoryItem): array => [
63
                'className' => get_class($inventoryItem),
64
            ],
65
            $inventory->getItems(),
66
        );
67
    }
68
}
69