Passed
Push — master ( f544cb...b3a3d9 )
by Nico
36:43 queued 09:10
created

ColonyProductionPreviewWrapper::loadPreview()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 1
dl 0
loc 25
ccs 0
cts 15
cp 0
crap 12
rs 9.7998
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\Lib\ColonyProduction\ColonyProduction;
8
use Stu\Orm\Entity\BuildingInterface;
9
10
class ColonyProductionPreviewWrapper
11
{
12
    private ColonyLibFactoryInterface $colonyLibFactory;
13
14
    private BuildingInterface $building;
15
16
    /** @var array<ColonyProduction> */
17
    private array $production = [];
18
19
    /**
20
     * @param array<ColonyProduction> $production
21
     */
22
    public function __construct(
23
        ColonyLibFactoryInterface $colonyLibFactory,
24
        BuildingInterface $building,
25
        array $production
26
    ) {
27
        $this->colonyLibFactory = $colonyLibFactory;
28
        $this->building = $building;
29
        $this->production = $production;
30
    }
31
32
    /** @return array<ColonyProduction> */
33
    public function getPreview(): array
34
    {
35
        $bcommodities = $this->building->getCommodities();
36
37
        $ret = [];
38
        foreach ($bcommodities as $commodityId => $prod) {
39
            $commodityId = $prod->getCommodityId();
40
            if (array_key_exists($commodityId, $this->production)) {
41
42
                $ret[$commodityId] = clone $this->production[$commodityId];
43
                $ret[$commodityId]->upperProduction($prod->getAmount());
44
            } else {
45
46
                $obj = $this->colonyLibFactory->createColonyProduction(
47
                    $prod->getCommodity(),
48
                    $prod->getAmount()
49
                );
50
                $ret[$commodityId] = $obj;
51
            }
52
53
            $ret[$commodityId]->setPreviewProduction($prod->getAmount());
54
        }
55
56
        return $ret;
57
    }
58
}
59