Passed
Pull Request — master (#1696)
by Nico
49:43 queued 22:34
created

StorageProvider   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
dl 0
loc 69
ccs 0
cts 37
cp 0
rs 10
c 1
b 0
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
B setTemplateVariables() 0 31 6
A getStorageSumPercent() 0 13 3
A getStorageSum() 0 7 2
A __construct() 0 6 1
1
<?php
2
3
namespace Stu\Module\Colony\Lib\Gui\Component;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Stu\Lib\Colony\PlanetFieldHostInterface;
7
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
8
use Stu\Module\Commodity\CommodityTypeEnum;
9
use Stu\Module\Commodity\Lib\CommodityCacheInterface;
10
use Stu\Module\Control\GameControllerInterface;
11
use Stu\Orm\Entity\ColonyInterface;
12
13
final class StorageProvider implements GuiComponentProviderInterface
14
{
15
    private ColonyLibFactoryInterface $colonyLibFactory;
16
17
    private CommodityCacheInterface $commodityCache;
18
19
    public function __construct(
20
        ColonyLibFactoryInterface $colonyLibFactory,
21
        CommodityCacheInterface $commodityCache
22
    ) {
23
        $this->colonyLibFactory = $colonyLibFactory;
24
        $this->commodityCache = $commodityCache;
25
    }
26
27
    public function setTemplateVariables(
28
        PlanetFieldHostInterface $host,
29
        GameControllerInterface $game
30
    ): void {
31
        $commodities = $this->commodityCache->getAll(CommodityTypeEnum::COMMODITY_TYPE_STANDARD);
32
33
        $prod = $this->colonyLibFactory->createColonyCommodityProduction($host)->getProduction();
34
        $game->setTemplateVar(
35
            'PRODUCTION_SUM',
36
            $this->colonyLibFactory->createColonyProductionSumReducer()->reduce($prod)
37
        );
38
39
        $stor = $host instanceof ColonyInterface ? $host->getStorage() : new ArrayCollection();
40
        $storage = [];
41
        foreach ($commodities as $value) {
42
            $commodityId = $value->getId();
43
            if (array_key_exists($commodityId, $prod)) {
44
                $storage[$commodityId]['commodity'] = $value;
45
                $storage[$commodityId]['production'] = $prod[$commodityId];
46
                $storage[$commodityId]['storage'] = $stor->containsKey($commodityId) ? $stor[$commodityId] : false;
47
            } elseif ($stor->containsKey($commodityId)) {
48
                $storage[$commodityId]['commodity'] = $value;
49
                $storage[$commodityId]['storage'] = $stor[$commodityId];
50
                $storage[$commodityId]['production'] = false;
51
            }
52
        }
53
54
        $game->setTemplateVar('STORAGE', $storage);
55
        $game->setTemplateVar('STORAGE_SUM', $this->getStorageSum($host));
56
        $game->setTemplateVar('STORAGE_SUM_PERCENT', $this->getStorageSumPercent($host));
57
        $game->setTemplateVar('MAX_STORAGE', $host->getMaxStorage());
58
    }
59
60
    private function getStorageSum(PlanetFieldHostInterface $host): int
61
    {
62
        if (!$host instanceof ColonyInterface) {
63
            return 0;
64
        }
65
66
        return $host->getStorageSum();
67
    }
68
69
    private function getStorageSumPercent(PlanetFieldHostInterface $host): float
70
    {
71
        if (!$host instanceof ColonyInterface) {
72
            return 0;
73
        }
74
75
        $maxStorage = $host->getMaxStorage();
76
77
        if ($maxStorage === 0) {
78
            return 0;
79
        }
80
81
        return round(100 / $maxStorage * $host->getStorageSum(), 2);
82
    }
83
}
84