Passed
Pull Request — master (#1930)
by Janko
14:58 queued 05:13
created

BuildingManager   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Test Coverage

Coverage 96.7%

Importance

Changes 0
Metric Value
eloc 91
dl 0
loc 171
ccs 88
cts 91
cp 0.967
rs 10
c 0
b 0
f 0
wmc 29

7 Methods

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