Passed
Push — dev ( 20f168...3a8ec2 )
by Nico
15:39
created

CommodityConsumption   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 55%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 35
ccs 11
cts 20
cp 0.55
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getConsumption() 0 29 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Colony\Lib;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Stu\Orm\Entity\ColonyInterface;
9
use Stu\Orm\Repository\ColonyDepositMiningRepositoryInterface;
10
11
final class CommodityConsumption implements CommodityConsumptionInterface
12
{
13 2
    public function __construct(
14
        private readonly ColonyDepositMiningRepositoryInterface $colonyDepositMiningRepository
15 2
    ) {}
16
17 1
    #[Override]
18
    public function getConsumption(
19
        array $production,
20
        ColonyInterface $colony
21
    ): array {
22 1
        $depositMinings = $this->colonyDepositMiningRepository->getCurrentUserDepositMinings($colony);
23 1
        $storages = $colony->getStorage();
24 1
        $ret = [];
25 1
        foreach ($production as $commodityId => $productionItem) {
26 1
            $proc = $productionItem->getProduction();
27 1
            if ($proc >= 0) {
28 1
                continue;
29
            }
30
31
            $commodity = $productionItem->getCommodity();
32
            $ret[$commodityId]['commodity'] = $commodity;
33
            $ret[$commodityId]['production'] = $productionItem->getProduction();
34
35
            if (array_key_exists($commodityId, $depositMinings)) {
36
                $deposit = $depositMinings[$commodityId];
37
                $ret[$commodityId]['turnsleft'] = (int) floor($deposit->getAmountLeft() / abs($proc));
38
            } else {
39
                $storage = $storages->get($commodityId);
40
                $stored = $storage !== null ? $storage->getAmount() : 0;
41
                $ret[$commodityId]['turnsleft'] = (int) floor($stored / abs($proc));
42
            }
43
        }
44
45 1
        return $ret;
46
    }
47
}
48