Passed
Push — dev ( 25004f...be11ce )
by Janko
16:12
created

BuildingManager::getChangeable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Building;
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\Module\Building\Action\BuildingFunctionActionMapperInterface;
9
use Stu\Orm\Entity\BuildingInterface;
10
use Stu\Orm\Entity\ColonyChangeableInterface;
11
use Stu\Orm\Entity\ColonyInterface;
12
use Stu\Orm\Entity\ColonySandboxInterface;
13
use Stu\Orm\Entity\PlanetFieldInterface;
14
use Stu\Orm\Repository\ColonyRepositoryInterface;
15
use Stu\Orm\Repository\ColonySandboxRepositoryInterface;
16
use Stu\Orm\Repository\PlanetFieldRepositoryInterface;
17
18
/**
19
 * Manages actions relating to buildings on planets
20
 */
21
final class BuildingManager implements BuildingManagerInterface
22
{
23 19
    public function __construct(
24
        private readonly PlanetFieldRepositoryInterface $planetFieldRepository,
25
        private readonly ColonyRepositoryInterface $colonyRepository,
26
        private readonly ColonySandboxRepositoryInterface $colonySandboxRepository,
27
        private readonly BuildingFunctionActionMapperInterface $buildingFunctionActionMapper,
28
        private readonly BuildingPostActionInterface $buildingPostAction
29 19
    ) {}
30
31 7
    #[Override]
32
    public function activate(PlanetFieldInterface $field): bool
33
    {
34 7
        $building = $field->getBuilding();
35
36 7
        if ($building === null) {
37 1
            return false;
38
        }
39
40 6
        if (!$field->isActivateable()) {
41 2
            return false;
42
        }
43
44 4
        if ($field->isActive()) {
45 1
            return true;
46
        }
47
48 3
        if ($field->hasHighDamage()) {
49 1
            return false;
50
        }
51
52 2
        $changeable = $this->getChangeable($field);
53
54 2
        $workerAmount = $building->getWorkers();
55
56 2
        if ($changeable instanceof ColonyChangeableInterface) {
57 2
            $worklessAmount = $changeable->getWorkless();
58 2
            if ($worklessAmount < $workerAmount) {
59 1
                return false;
60
            }
61
62 1
            $changeable->setWorkless($worklessAmount - $workerAmount);
63
        }
64
65 1
        $changeable
66 1
            ->setWorkers($changeable->getWorkers() + $workerAmount)
67 1
            ->setMaxBev($changeable->getMaxBev() + $building->getHousing());
68 1
        $field->setActive(1);
69
70 1
        $this->planetFieldRepository->save($field);
71
72 1
        $this->buildingPostAction->handleActivation($building, $field->getHost());
73
74 1
        $this->saveHost($field->getHost());
75
76 1
        return true;
77
    }
78
79 8
    #[Override]
80
    public function deactivate(PlanetFieldInterface $field): void
81
    {
82 8
        $building = $field->getBuilding();
83
84 8
        if ($building === null) {
85 1
            return;
86
        }
87
88 7
        if (!$field->isActivateable()) {
89 2
            return;
90
        }
91
92 5
        if (!$field->isActive()) {
93 1
            return;
94
        }
95
96 4
        $changeable = $this->getChangeable($field);
97
98 4
        $this->updateWorkerAndMaxBev($building, $changeable);
99 4
        $field->setActive(0);
100
101 4
        $this->planetFieldRepository->save($field);
102
103 4
        $this->buildingPostAction->handleDeactivation($building, $field->getHost());
104
105 4
        $this->saveHost($field->getHost());
106
    }
107
108 7
    private function saveHost(ColonyInterface|ColonySandboxInterface $host): void
109
    {
110 7
        if ($host instanceof ColonyInterface) {
111 7
            $this->colonyRepository->save($host);
112
        } else {
113
            $this->colonySandboxRepository->save($host);
114
        }
115
    }
116
117 4
    private function updateWorkerAndMaxBev(BuildingInterface $building, ColonyChangeableInterface|ColonySandboxInterface $host): void
118
    {
119 4
        $workerAmount = $building->getWorkers();
120
121 4
        if ($host instanceof ColonyChangeableInterface) {
122 4
            $host->setWorkless($host->getWorkless() + $workerAmount);
123
        }
124 4
        $host->setWorkers($host->getWorkers() - $workerAmount);
125 4
        $host->setMaxBev($host->getMaxBev() - $building->getHousing());
126
    }
127
128 5
    #[Override]
129
    public function remove(PlanetFieldInterface $field, bool $isDueToUpgrade = false): void
130
    {
131 5
        $building = $field->getBuilding();
132 5
        if ($building === null) {
133 1
            return;
134
        }
135
136 4
        if (!$isDueToUpgrade && !$building->isRemovable()) {
137 1
            return;
138
        }
139
140 3
        $host = $field->getHost();
141 3
        $changeable = $this->getChangeable($field);
142
143 3
        if (!$field->isUnderConstruction()) {
144 3
            $this->deactivate($field);
145 3
            $changeable
146 3
                ->setMaxStorage($changeable->getMaxStorage() - $building->getStorage())
147 3
                ->setMaxEps($changeable->getMaxEps() - $building->getEpsStorage());
148
        }
149
150 3
        foreach ($building->getFunctions() as $function) {
151 3
            $buildingFunction = $function->getFunction();
152
153 3
            $handler = $this->buildingFunctionActionMapper->map($buildingFunction);
154 3
            if ($handler !== null && $host instanceof ColonyInterface) {
155 3
                $handler->destruct($buildingFunction, $host);
156
            }
157
        }
158
159 3
        $field->clearBuilding();
160
161 3
        $this->planetFieldRepository->save($field);
162 3
        $this->saveHost($field->getHost());
163
    }
164
165 2
    #[Override]
166
    public function finish(PlanetFieldInterface $field, bool $activate = true): ?string
167
    {
168 2
        $building = $field->getBuilding();
169 2
        if ($building === null) {
170 1
            return null;
171
        }
172
173 1
        $changeable = $this->getChangeable($field);
174
175 1
        $field
176 1
            ->setActive(0)
177 1
            ->setIntegrity($building->getIntegrity());
178
179 1
        $activationDetails = null;
180 1
        if ($building->isActivateAble()) {
181 1
            if ($activate) {
182 1
                $activationDetails = $this->activate($field)
183
                    ? '[color=green]Und konnte wunschgemäß aktiviert werden[/color]'
184 1
                    : '[color=red]Konnte allerdings nicht wie gewünscht aktiviert werden[/color]';
185
            } else {
186
                $activationDetails = 'Und wurde wunschgemäß nicht aktiviert';
187
            }
188
        }
189
190 1
        $changeable
191 1
            ->setMaxStorage($changeable->getMaxStorage() + $building->getStorage())
192 1
            ->setMaxEps($changeable->getMaxEps() + $building->getEpsStorage());
193
194 1
        $this->saveHost($field->getHost());
195 1
        $this->planetFieldRepository->save($field);
196
197 1
        return $activationDetails;
198
    }
199
200 8
    private function getChangeable(PlanetFieldInterface $field): ColonyChangeableInterface|ColonySandboxInterface
201
    {
202 8
        $host = $field->getHost();
203
204 8
        return $host instanceof ColonySandboxInterface
205
            ? $host
206 8
            : $host->getChangeable();
207
    }
208
}
209