Passed
Push — master ( 782176...fdfe0b )
by Peter
44s
created

DetailedPlayerFactory::createFromArray()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
ccs 20
cts 20
cp 1
rs 8.8571
cc 1
eloc 20
nc 1
nop 1
crap 1
1
<?php
2
3
namespace PtrTn\Battlerite\Factory;
4
5
use PtrTn\Battlerite\Dto\Player\DetailedPlayer;
6
use PtrTn\Battlerite\Dto\Player\Stats;
7
use PtrTn\Battlerite\Repository\DataMappingRepository;
8
use Webmozart\Assert\Assert;
9
10
class DetailedPlayerFactory
11
{
12
    /**
13
     * @var DataMappingRepository
14
     */
15
    private $mappingRepository;
16
17 31
    public function __construct(DataMappingRepository $mappingRepository)
18
    {
19 31
        $this->mappingRepository = $mappingRepository;
20 31
    }
21
22 1
    public function createFromArray(array $player) : DetailedPlayer
23
    {
24 1
        Assert::string($player['type']);
25 1
        Assert::string($player['id']);
26 1
        Assert::string($player['attributes']['name']);
27 1
        Assert::string($player['attributes']['patchVersion']);
28 1
        Assert::string($player['attributes']['shardId']);
29 1
        Assert::integer($player['attributes']['stats']['picture']);
30 1
        Assert::integer($player['attributes']['stats']['title']);
31 1
        Assert::string($player['attributes']['titleId']);
32
33 1
        $stats = $this->createPlayerStats($player['attributes']['stats']);
34
35 1
        return new DetailedPlayer(
36 1
            $player['type'],
37 1
            $player['id'],
38 1
            $player['attributes']['name'],
39 1
            $player['attributes']['patchVersion'],
40 1
            $player['attributes']['shardId'],
41 1
            $player['attributes']['stats']['picture'],
42 1
            $player['attributes']['stats']['title'],
43 1
            $player['attributes']['titleId'],
44 1
            $stats
45
        );
46
    }
47
48 1
    private function createPlayerStats(array $stats) : Stats
49
    {
50 1
        $accountStats = [];
51 1
        $championStats = [];
52 1
        $mapStats = [];
53 1
        foreach ($stats as $statKey => $statValue) {
54 1
            $mappedStat = $this->mappingRepository->getStatMapping($statKey);
55
56 1
            if ($mappedStat === null) {
57 1
                continue;
58
            }
59
60 1
            if ($mappedStat->isChampionStat()) {
61 1
                $championStats[$mappedStat->champion][$mappedStat->statName] = $statValue;
62
            }
63 1
            if ($mappedStat->isMapStat()) {
64
                $mapStats[$mappedStat->map][$mappedStat->statName] = $statValue;
65
            }
66 1
            $accountStats[$mappedStat->statName] = $statValue;
67
        }
68
69 1
        $playerStats = Stats::createFromArray($accountStats, $championStats, $mapStats);
70 1
        return $playerStats;
71
    }
72
}
73