Passed
Push — master ( f388d7...367320 )
by Paweł
03:15
created

Player::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AardsGerds\Game\Player;
6
7
use AardsGerds\Game\Build\Attribute\AttributePoints;
8
use AardsGerds\Game\Build\Experience;
9
use AardsGerds\Game\Build\Level;
10
use AardsGerds\Game\Build\LevelProgress;
11
use AardsGerds\Game\Build\Talent\TalentPoints;
12
13
final class Player
14
{
15
    public function __construct(
16
        private string $name,
17
        private LevelProgress $levelProgress,
18
        private AttributePoints $attributePoints,
19
        private TalentPoints $talentPoints,
20
    ) {}
21
22
    public static function unserialize(array $data): self
23
    {
24
        return new self(
25
            $data['name'],
26
            new LevelProgress(
27
                new Level($data['level']),
28
                new Experience($data['current_experience']),
29
            ),
30
            new AttributePoints($data['attribute_points']),
31
            new TalentPoints($data['talent_points']),
32
        );
33
    }
34
35
    public function serialize(): array
36
    {
37
        return [
38
            'name' => $this->name,
39
            'level' => $this->levelProgress->getLevel()->get(),
40
            'current_experience' => $this->levelProgress->getCurrentExperience()->get(),
41
            'attribute_points' => $this->attributePoints->get(),
42
            'talent_points' => $this->talentPoints->get(),
43
        ];
44
    }
45
46
    public function increaseExperience(Experience $experience): void
47
    {
48
        $this->levelProgress->increase($experience, $this);
49
    }
50
51
    public function getAttributePoints(): AttributePoints
52
    {
53
        return $this->attributePoints;
54
    }
55
56
    public function getTalentPoints(): TalentPoints
57
    {
58
        return $this->talentPoints;
59
    }
60
}
61