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

CommodityConsumption::getConsumption()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 5
nop 2
dl 0
loc 28
ccs 0
cts 17
cp 0
crap 30
rs 9.3888
c 0
b 0
f 0
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