Passed
Pull Request — master (#2257)
by Janko
10:11 queued 05:17
created

ColonySurface::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 10
dl 0
loc 1
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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