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

CommodityConsumption::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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