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

ColonyProductionPreviewWrapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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