Passed
Branch master (9bcc45)
by Janko
41:23
created

ModuleFabricationListItem::getModuleLevel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Colony\View\ShowModuleFab;
6
7
use Stu\Orm\Entity\ColonyInterface;
8
use Stu\Orm\Entity\ModuleCostInterface;
9
use Stu\Orm\Entity\ModuleInterface;
10
use Stu\Orm\Entity\ShipBuildplanInterface;
11
use Stu\Orm\Entity\ShipRumpInterface;
12
use Stu\Orm\Repository\ModuleQueueRepositoryInterface;
13
14
final class ModuleFabricationListItem
15
{
16
    /** @var array<int> */
17
    private array $rump_ids = [];
18
    /** @var array<int> */
19
    private array $buildplan_ids = [];
20
21
    public function __construct(
22
        private ModuleQueueRepositoryInterface $moduleQueueRepository,
23
        private ModuleInterface $module,
24
        private ColonyInterface $colony
25
    ) {}
26
27
    public function getModule(): ModuleInterface
28
    {
29
        return $this->module;
30
    }
31
32
    public function getModuleId(): int
33
    {
34
        return $this->module->getId();
35
    }
36
37
    public function getModuleType(): int
38
    {
39
        return $this->module->getType()->value;
40
    }
41
42
    public function getModuleLevel(): int
43
    {
44
        return $this->module->getLevel();
45
    }
46
47
    public function getCommodityId(): int
48
    {
49
        return $this->module->getCommodityId();
50
    }
51
52
    public function getName(): string
53
    {
54
        return $this->module->getName();
55
    }
56
57
    public function getEnergyCost(): int
58
    {
59
        return $this->module->getEcost();
60
    }
61
62
    /** @return array<int, ModuleCostInterface> */
63
    public function getConstructionCosts(): array
64
    {
65
        return $this->module->getCostSorted();
66
    }
67
68
    public function getAmountQueued(): int
69
    {
70
        return $this->moduleQueueRepository->getAmountByColonyAndModule(
71
            $this->colony->getId(),
72
            $this->module->getId()
73
        );
74
    }
75
76
    public function getAmountInStock(): int
77
    {
78
        $result = $this->colony->getStorage()[$this->getCommodityId()] ?? null;
79
80
        if ($result === null) {
81
            return 0;
82
        }
83
        return $result->getAmount();
84
    }
85
86
    public function addRump(ShipRumpInterface $shipRump): void
87
    {
88
        if (!in_array($shipRump->getId(), $this->rump_ids)) {
89
            $this->rump_ids[] = $shipRump->getId();
90
        }
91
    }
92
93
    public function addBuildplan(ShipBuildplanInterface $buildplan): void
94
    {
95
        if (!in_array($buildplan->getId(), $this->buildplan_ids)) {
96
            $this->buildplan_ids[] = $buildplan->getId();
97
        }
98
    }
99
100
    public function getClass(): string
101
    {
102
        return sprintf(
103
            'type_%d level_%d rump_%s buildplan_%s',
104
            $this->getModuleType(),
105
            $this->getModuleLevel(),
106
            implode(' rump_', $this->rump_ids),
107
            implode(' buildplan_', $this->buildplan_ids)
108
        );
109
    }
110
}
111