Passed
Push — master ( 552948...2e7820 )
by Paweł
02:59
created

DenormalizePlayer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 44
c 1
b 0
f 0
dl 0
loc 78
ccs 48
cts 48
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A denormalizeInventory() 0 16 1
A denormalizeWeapon() 0 13 1
A denormalizeTalents() 0 22 1
A __invoke() 0 17 2
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\Strength;
11
use AardsGerds\Game\Build\Experience;
12
use AardsGerds\Game\Build\Level;
13
use AardsGerds\Game\Build\LevelProgress;
14
use AardsGerds\Game\Build\Talent\SecretKnowledge\Ascension;
15
use AardsGerds\Game\Build\Talent\SecretKnowledge\SecretKnowledge;
16
use AardsGerds\Game\Build\Talent\Talent;
17
use AardsGerds\Game\Build\Talent\TalentCollection;
18
use AardsGerds\Game\Build\Talent\TalentPoints;
19
use AardsGerds\Game\Build\Talent\WeaponMastery\WeaponMastery;
20
use AardsGerds\Game\Build\Talent\WeaponMastery\WeaponMasteryLevel;
21
use AardsGerds\Game\Inventory\Inventory;
22
use AardsGerds\Game\Inventory\InventoryItem;
23
use AardsGerds\Game\Inventory\Weapon\RebredirWeapon;
24
use AardsGerds\Game\Inventory\Weapon\Weapon;
25
use AardsGerds\Game\Inventory\Weapon\WeaponType;
26
use AardsGerds\Game\Player\Player;
27
use function Lambdish\Phunctional\map;
28
29
final class DenormalizePlayer
30
{
31 1
    public function __invoke(array $data): Player
32
    {
33 1
        return new Player(
34 1
            $data['name'],
35 1
            new Health($data['health']),
36 1
            new Etherum($data['etherum']),
37 1
            new Strength($data['strength']),
38 1
            self::denormalizeTalents($data['talents']),
39 1
            self::denormalizeInventory($data['inventory']),
0 ignored issues
show
Bug Best Practice introduced by
The method AardsGerds\Game\Infrastr...:denormalizeInventory() is not static, but was called statically. ( Ignorable by Annotation )

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

39
            self::/** @scrutinizer ignore-call */ 
40
                  denormalizeInventory($data['inventory']),
Loading history...
40 1
            $data['weapon'] !== null ? self::denormalizeWeapon($data['weapon']) : null,
41 1
            $data['corrupted'],
42 1
            new LevelProgress(
43 1
                new Level($data['levelProgress']['level']),
44 1
                new Experience($data['levelProgress']['currentExperience']),
45
            ),
46 1
            new AttributePoints($data['attributePoints']),
47 1
            new TalentPoints($data['talentPoints']),
48
        );
49
    }
50
51 1
    private static function denormalizeTalents(array $data): TalentCollection
52
    {
53 1
        return new TalentCollection(map(
54 1
            static function(array $talent): Talent {
55 1
                $reflection = new \ReflectionClass($talent['className']);
56
57 1
                $talent = match ($talent['className']) {
58
                    WeaponMastery::class =>
59 1
                        $reflection->newInstance(
60 1
                            new WeaponType($talent['type']),
61 1
                            new WeaponMasteryLevel($talent['level']),
62
                        ),
63
                    SecretKnowledge::class =>
64 1
                        $reflection->newInstance(new Ascension($talent['ascension'])),
65 1
                    default => $reflection->newInstance(),
66
                };
67
68 1
                assert($talent instanceof Talent);
69
70 1
                return $talent;
71 1
            },
72 1
            $data,
73
        ));
74
    }
75
76 1
    private static function denormalizeWeapon(array $data): Weapon
77
    {
78 1
        $reflection = new \ReflectionClass($data['className']);
79
80 1
        $weapon = match (true) {
81 1
            in_array(RebredirWeapon::class, $reflection->getTraitNames()) =>
82 1
                $reflection->newInstance(new Etherum($data['etherumLoad'])),
83 1
            default => $reflection->newInstance(),
84
        };
85
86 1
        assert($weapon instanceof Weapon);
87
88 1
        return $weapon;
89
    }
90
91 1
    private function denormalizeInventory(array $data): Inventory
92
    {
93 1
        return new Inventory(map(
94 1
            static function(array $item): InventoryItem {
95 1
                $reflection = new \ReflectionClass($item['className']);
96
97 1
                $item = match (true) {
98 1
                    $reflection->isSubclassOf(Weapon::class) => self::denormalizeWeapon($item),
99 1
                    default => $reflection->newInstance(),
100
                };
101
102 1
                assert($item instanceof InventoryItem);
103
104 1
                return $item;
105 1
            },
106 1
            $data,
107
        ));
108
    }
109
}
110