DenormalizePlayer::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 20
ccs 18
cts 18
cp 1
rs 9.7
c 1
b 0
f 0
cc 3
nc 1
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AardsGerds\Game\Infrastructure\Persistence;
6
7
use AardsGerds\Game\Build\Attribute\AttributePoints;
8
use AardsGerds\Game\Build\Attribute\Etherum;
9
use AardsGerds\Game\Build\Attribute\Health;
10
use AardsGerds\Game\Build\Attribute\Initiative;
11
use AardsGerds\Game\Build\Attribute\Strength;
12
use AardsGerds\Game\Build\Experience;
13
use AardsGerds\Game\Build\Level;
14
use AardsGerds\Game\Build\LevelProgress;
15
use AardsGerds\Game\Build\Talent\SecretKnowledge\Ascension;
16
use AardsGerds\Game\Build\Talent\SecretKnowledge\SecretKnowledge;
17
use AardsGerds\Game\Build\Talent\Talent;
18
use AardsGerds\Game\Build\Talent\TalentCollection;
19
use AardsGerds\Game\Build\Talent\TalentPoints;
20
use AardsGerds\Game\Build\Talent\WeaponMastery\WeaponMastery;
21
use AardsGerds\Game\Build\Talent\WeaponMastery\WeaponMasteryLevel;
22
use AardsGerds\Game\Entity\Corruption;
23
use AardsGerds\Game\Inventory\Inventory;
24
use AardsGerds\Game\Inventory\InventoryItem;
25
use AardsGerds\Game\Inventory\Weapon\RebredirWeapon;
26
use AardsGerds\Game\Inventory\Weapon\Weapon;
27
use AardsGerds\Game\Inventory\Weapon\WeaponType;
28
use AardsGerds\Game\Player\Player;
29
use function Lambdish\Phunctional\map;
30
31
final class DenormalizePlayer
32
{
33 1
    public function __invoke(array $data): Player
34
    {
35 1
        return new Player(
36 1
            $data['name'],
37 1
            new Health($data['health']),
38 1
            new Etherum($data['etherum']),
39 1
            new Strength($data['strength']),
40 1
            new Initiative($data['initiative']),
41 1
            self::denormalizeTalents($data['talents']),
42 1
            self::denormalizeInventory($data['inventory']),
43 1
            $data['weapon'] !== null ? self::denormalizeWeapon($data['weapon']) : null,
44 1
            $data['corruption'] !== null ? new Corruption($data['corruption']) : null,
45 1
            new LevelProgress(
46 1
                new Level($data['levelProgress']['level']),
47 1
                new Experience($data['levelProgress']['currentExperience']),
48
            ),
49 1
            new Health($data['maximumHealth']),
50 1
            new AttributePoints($data['attributePoints']),
51 1
            new TalentPoints($data['talentPoints']),
52 1
            new $data['checkpoint']['className'](),
53
        );
54
    }
55
56 1
    private static function denormalizeTalents(array $data): TalentCollection
57
    {
58 1
        return new TalentCollection(map(
59 1
            static function(array $talent): Talent {
60 1
                $reflection = new \ReflectionClass($talent['className']);
61
62 1
                $talent = match ($talent['className']) {
63
                    WeaponMastery::class =>
64 1
                        $reflection->newInstance(
65 1
                            new WeaponType($talent['type']),
66 1
                            new WeaponMasteryLevel($talent['level']),
67
                        ),
68
                    SecretKnowledge::class =>
69 1
                        $reflection->newInstance(new Ascension($talent['ascension'])),
70 1
                    default => $reflection->newInstance(),
71
                };
72
73 1
                assert($talent instanceof Talent);
74
75 1
                return $talent;
76 1
            },
77 1
            $data,
78
        ));
79
    }
80
81 1
    private static function denormalizeWeapon(array $data): Weapon
82
    {
83 1
        $reflection = new \ReflectionClass($data['className']);
84
85 1
        $weapon = match (true) {
86 1
            $reflection->isSubclassOf(RebredirWeapon::class) =>
87 1
                $reflection->newInstance(new Etherum($data['etherumLoad'])),
88 1
            default => $reflection->newInstance(),
89
        };
90
91 1
        assert($weapon instanceof Weapon);
92
93 1
        return $weapon;
94
    }
95
96 1
    private static function denormalizeInventory(array $data): Inventory
97
    {
98 1
        return new Inventory(map(
99 1
            static function(array $item): InventoryItem {
100 1
                $reflection = new \ReflectionClass($item['className']);
101
102 1
                $item = match (true) {
103 1
                    $reflection->isSubclassOf(Weapon::class) => self::denormalizeWeapon($item),
104 1
                    default => $reflection->newInstance(),
105
                };
106
107 1
                assert($item instanceof InventoryItem);
108
109 1
                return $item;
110 1
            },
111 1
            $data,
112
        ));
113
    }
114
}
115