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

ColonyProductionPreviewWrapper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
c 2
b 0
f 0
dl 0
loc 47
ccs 0
cts 19
cp 0
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getPreview() 0 24 3
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