Passed
Push — master ( fa53c0...9478ec )
by Paweł
02:37
created

NormalizePlayer::normalizeWeapon()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 3
b 0
f 0
nc 2
nop 1
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 3
rs 10
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
            'talents' => self::normalizeTalents($player->getTalents()),
28 1
            'inventory' => self::normalizeInventory($player->getInventory()),
29 1
            'weapon' => $player->getWeapon() !== null ? self::normalizeWeapon($player->getWeapon()) : null,
30 1
            'corrupted' => $player->isCorrupted(),
31
            'levelProgress' => [
32 1
                'level' => $player->getLevelProgress()->getLevel()->get(),
33 1
                'currentExperience' => $player->getLevelProgress()->getCurrentExperience()->get(),
34
            ],
35 1
            'attributePoints' => $player->getAttributePoints()->get(),
36 1
            'talentPoints' => $player->getTalentPoints()->get(),
37
        ];
38
    }
39
40 1
    private static function normalizeWeapon(Weapon $weapon): array
41
    {
42 1
        $data = ['className' => $className = get_class($weapon)];
43
44 1
        if (in_array(RebredirWeapon::class, class_uses($className) ?: [])) {
45
            /** @var RebredirWeapon $weapon */
46 1
            $data = array_merge($data, ['etherumLoad' => $weapon->getEtherumLoad()->get()]); /** @phpstan-ignore-line */
47
        }
48
49 1
        return $data;
50
    }
51
52 1
    private static function normalizeTalents(TalentCollection $talentCollection): array
53
    {
54 1
        return map(
55 1
            static function(Talent $talent): array {
56 1
                $data = ['className' => get_class($talent)];
57
58
                return match (true) {
59 1
                    $talent instanceof SecretKnowledge => array_merge($data, [
60 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

60
                        'ascension' => $talent->/** @scrutinizer ignore-call */ getAscension()->get(),
Loading history...
61
                    ]),
62 1
                    $talent instanceof WeaponMastery => array_merge($data, [
63 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

63
                        'type' => (string) $talent->/** @scrutinizer ignore-call */ getType(),
Loading history...
64 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

64
                        'level' => $talent->/** @scrutinizer ignore-call */ getLevel()->get(),
Loading history...
65
                    ]),
66 1
                    default => $data,
67
                };
68 1
            },
69 1
            $talentCollection,
70
        );
71
    }
72
73 1
    private static function normalizeInventory(Inventory $inventory): array
74
    {
75 1
        return map(
76 1
            static function(InventoryItem $inventoryItem): array {
77 1
                if ($inventoryItem instanceof Weapon) {
78 1
                    return self::normalizeWeapon($inventoryItem);
79
                }
80
81 1
                return ['className' => get_class($inventoryItem)];
82 1
            },
83 1
            $inventory,
84
        );
85
    }
86
}
87