Passed
Push — master ( 52dd57...a4c9fa )
by Willy
02:14
created

StatisticService::getStatisticsSubCategories()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Kubinashi\BattlenetApi\WorldOfWarcraft\CharacterProfileApi\Statistic\Service;
4
5
use Kubinashi\BattlenetApi\WorldOfWarcraft\CharacterProfileApi\Statistic\Model\StatisticsValueObject;
6
use Kubinashi\BattlenetApi\WorldOfWarcraft\CharacterProfileApi\Statistic\Model\StatisticValueObject;
7
8
/**
9
 * @author  Willy Reiche
10
 * @since   2017-08-26
11
 * @version 1.0
12
 */
13
class StatisticService
14
{
15
    /**
16
     * @param \StdClass $statistics
17
     *
18
     * @return StatisticsValueObject
19
     */
20
    public function getStatistics($statistics)
21
    {
22
        $categoryStatistics = [];
23
        if (isset($statistics->statistics)) {
24
            $categoryStatistics = $this->getCategoryStatistics($statistics->statistics);
25
        }
26
27
        $statisticsSubCategories = [];
28
        if (isset($statistics->subCategories)) {
29
            $statisticsSubCategories = $this->getStatisticsSubCategories($statistics->subCategories);
30
        }
31
32
        $statisticsValueObject =  new StatisticsValueObject(
33
            $statistics->id,
34
            $statistics->name,
35
            $statisticsSubCategories,
36
            $categoryStatistics
37
        );
38
39
        return $statisticsValueObject;
40
    }
41
42
    /**
43
     * @param \StdClass[] $subCategories
44
     *
45
     * @return StatisticsValueObject[]
46
     */
47
    private function getStatisticsSubCategories($subCategories)
48
    {
49
        $categories = [];
50
        foreach ($subCategories as $subCategory) {
51
            $categories[] = $this->getStatistics($subCategory);
52
        }
53
        return $categories;
54
    }
55
56
    /**
57
     * @param \StdClass[] $statistics
58
     *
59
     * @return StatisticValueObject[]
60
     */
61
    private function getCategoryStatistics($statistics)
62
    {
63
        $categoryStatistics = [];
64
        foreach ($statistics as $statistic) {
65
            $highest = "";
66
            if (isset($statistic->highest)) {
67
                $highest = $statistic->highest;
68
            }
69
70
            $categoryStatistics[] = new StatisticValueObject(
71
                $statistic->id,
72
                $statistic->name,
73
                $statistic->quantity,
74
                $statistic->lastUpdated,
75
                $statistic->money,
76
                $highest
77
            );
78
        }
79
80
        return $categoryStatistics;
81
    }
82
}
83