Passed
Push — dev ( 7914d2...bbc331 )
by Janko
10:29
created

ColonySurface::getSurface()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 13.9128

Importance

Changes 0
Metric Value
cc 9
eloc 27
nc 7
nop 0
dl 0
loc 47
ccs 17
cts 28
cp 0.6071
crap 13.9128
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Colony\Lib;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
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...
9
use Stu\Component\Building\BuildingEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Building\BuildingEnum 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...
10
use Stu\Component\Building\BuildingFunctionEnum;
11
use Stu\Exception\SanityCheckException;
12
use Stu\Lib\Colony\PlanetFieldHostInterface;
13
use Stu\Module\Building\BuildingFunctionTypeEnum;
14
use Stu\Orm\Entity\Colony;
15
use Stu\Orm\Entity\PlanetField;
16
use Stu\Orm\Entity\Researched;
17
use Stu\Orm\Repository\BuildingRepositoryInterface;
18
use Stu\Orm\Repository\ColonyRepositoryInterface;
19
use Stu\Orm\Repository\PlanetFieldRepositoryInterface;
20
use Stu\Orm\Repository\ResearchedRepositoryInterface;
21
use Stu\PlanetGenerator\Exception\PlanetGeneratorException;
22
use Stu\PlanetGenerator\PlanetGeneratorInterface;
23
24
/**
25
 * Provides access to several colony surface related methods
26
 */
27
final class ColonySurface implements ColonySurfaceInterface
28
{
29 8
    public function __construct(private PlanetFieldRepositoryInterface $planetFieldRepository, private BuildingRepositoryInterface $buildingRepository, private ColonyRepositoryInterface $colonyRepository, private ResearchedRepositoryInterface $researchedRepository, private PlanetGeneratorInterface $planetGenerator, private EntityManagerInterface $entityManager, private PlanetFieldTypeRetrieverInterface $planetFieldTypeRetriever, private PlanetFieldHostInterface $host, private ?int $buildingId, private bool $showUnderground) {}
30
31 5
    #[Override]
32
    public function getSurface(): array
33
    {
34
        try {
35 5
            $this->updateSurface();
36
        } catch (PlanetGeneratorException) {
37
            return $this->host->getPlanetFields()->toArray();
38
        }
39
40 5
        $fields = $this->host->getPlanetFields()->toArray();
41
42 5
        if (!$this->showUnderground) {
43
            $fields = array_filter(
44
                $fields,
45
                fn(PlanetField $field): bool => !$this->planetFieldTypeRetriever->isUndergroundField($field)
46
            );
47
        }
48
49 5
        if ($this->buildingId !== null) {
50 4
            $building = $this->buildingRepository->find($this->buildingId);
51 4
            if ($building === null) {
52
                throw new SanityCheckException(sprintf('buildingId %d does not exist', $this->buildingId));
53
            }
54 4
            $user = $this->host->getUser();
55
56 4
            $researchedArray = $this->researchedRepository->getFinishedListByUser($user->getId());
57
58 4
            array_walk(
59 4
                $fields,
60 4
                function (PlanetField $field) use ($building, $researchedArray): void {
61
                    if (
62 4
                        $field->getTerraformingId() === null &&
63 4
                        $building->getBuildableFields()->containsKey($field->getFieldType())
64
                    ) {
65
                        //PlanetFieldTypeBuilding
66
                        $fieldBuilding = $building->getBuildableFields()->get($field->getFieldType());
67
68
                        $researchId = $fieldBuilding?->getResearchId();
69
                        if ($researchId == null || $this->isResearched($researchId, $researchedArray)) {
70
                            $field->setBuildMode(true);
71
                        }
72
                    }
73 4
                }
74 4
            );
75
        }
76
77 5
        return $fields;
78
    }
79
80
    /** @param array<Researched> $researched */
81
    private function isResearched(int $researchId, array $researched): bool
82
    {
83
        foreach ($researched as $research) {
84
            if ($research->getResearchId() == $researchId) {
85
                return true;
86
            }
87
        }
88
89
        return false;
90
    }
91
92 5
    #[Override]
93
    public function getSurfaceTileStyle(): string
94
    {
95 5
        $width = $this->planetGenerator->loadColonyClassConfig($this->host->getColonyClass()->getId())['sizew'];
96 5
        $gridArray = [];
97 5
        for ($i = 0; $i < $width; $i++) {
98 5
            $gridArray[] = '43px';
99
        }
100
101 5
        return sprintf('display: grid; grid-template-columns: %s;', implode(' ', $gridArray));
102
    }
103
104 5
    #[Override]
105
    public function updateSurface(): void
106
    {
107 5
        $host = $this->host;
108 5
        if (!$host instanceof Colony) {
109
            return;
110
        }
111 5
        if (!$host->isFree()) {
112 5
            return;
113
        }
114
115
        $mask = $host->getMask();
116
117
        if ($mask === null) {
118
            $planetConfig = $this->planetGenerator->generateColony(
119
                $host->getColonyClass()->getId(),
120
                $host->getSystem()->getBonusFieldAmount()
121
            );
122
123
            $mask = base64_encode(serialize($planetConfig->getFieldArray()));
124
125
            $host->setMask($mask);
126
            $host->setSurfaceWidth($planetConfig->getSurfaceWidth());
127
128
            $this->colonyRepository->save($host);
129
        }
130
131
        $fields = $host->getPlanetFields()->toArray();
132
133
        $surface = unserialize(base64_decode($mask));
134
        foreach ($surface as $fieldId => $type) {
135
            if (!array_key_exists($fieldId, $fields)) {
136
                $newField = $this->planetFieldRepository->prototype();
137
                $fields[$fieldId] = $newField;
138
                $fields[$fieldId]->setColony($host);
139
                $fields[$fieldId]->setFieldId($fieldId);
140
                $host->getPlanetFields()->set($fieldId, $newField);
141
            }
142
143
            $fields[$fieldId]->setBuilding(null);
144
            $fields[$fieldId]->setIntegrity(0);
145
            $fields[$fieldId]->setFieldType((int) $type);
146
            $fields[$fieldId]->setActive(0);
147
148
            $this->planetFieldRepository->save($fields[$fieldId]);
149
        }
150
151
        $this->entityManager->flush();
152
    }
153
154 3
    #[Override]
155
    public function hasShipyard(): bool
156
    {
157 3
        return $this->planetFieldRepository->getCountByColonyAndBuildingFunctionAndState(
158 3
            $this->host,
159 3
            BuildingFunctionTypeEnum::getShipyardOptions(),
160 3
            [0, 1]
161 3
        ) > 0;
162
    }
163
164
    #[Override]
165
    public function hasAirfield(): bool
166
    {
167
        return $this->planetFieldRepository->getCountByColonyAndBuildingFunctionAndState(
168
            $this->host,
169
            [BuildingFunctionEnum::AIRFIELD],
170
            [0, 1]
171
        ) > 0;
172
    }
173
}
174