Passed
Push — master ( 288b46...98b0e3 )
by Nico
31:30 queued 08:00
created

CommodityConsumption   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 10.53%

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 38
ccs 2
cts 19
cp 0.1053
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getConsumption() 0 28 5
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Colony\Lib;
6
7
use Stu\Orm\Entity\ColonyInterface;
8
use Stu\Orm\Entity\CommodityInterface;
9
use Stu\Orm\Repository\CommodityRepositoryInterface;
10
11
final class CommodityConsumption implements CommodityConsumptionInterface
12
{
13
    private CommodityRepositoryInterface $commodityRepository;
14
15 2
    public function __construct(
16
        CommodityRepositoryInterface $commodityRepository
17
    ) {
18 2
        $this->commodityRepository = $commodityRepository;
19
    }
20
21
    public function getConsumption(
22
        array $production,
23
        ColonyInterface $colony
24
    ): array {
25
        $depositMinings = $colony->getUserDepositMinings();
26
        $stor = $colony->getStorage();
27
        $ret = [];
28
        foreach ($production as $commodityId => $productionItem) {
29
            $proc = $productionItem->getProduction();
30
            if ($proc >= 0) {
31
                continue;
32
            }
33
34
            /** @var CommodityInterface $commodity */
35
            $commodity = $this->commodityRepository->find($productionItem->getCommodityId());
36
            $ret[$commodityId]['commodity'] = $commodity;
37
            $ret[$commodityId]['production'] = $productionItem->getProduction();
38
39
            if (array_key_exists($commodityId, $depositMinings)) {
40
                $deposit = $depositMinings[$commodityId];
41
                $ret[$commodityId]['turnsleft'] = (int) floor($deposit->getAmountLeft() / abs($proc));
42
            } else {
43
                $stored = $stor->containsKey($commodityId) ? $stor[$commodityId]->getAmount() : 0;
44
                $ret[$commodityId]['turnsleft'] = (int) floor($stored / abs($proc));
45
            }
46
        }
47
48
        return $ret;
49
    }
50
}
51