Test Failed
Push — master ( c4c9a7...5df554 )
by Nico
17:23 queued 08:14
created

EditSection::handle()   F

Complexity

Conditions 16
Paths 288

Size

Total Lines 153
Code Lines 115

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 0
Metric Value
cc 16
eloc 115
c 0
b 0
f 0
nc 288
nop 1
dl 0
loc 153
rs 2.9066
ccs 0
cts 119
cp 0
crap 272

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Admin\View\Map\EditSection;
6
7
use Stu\Component\Map\MapEnum;
8
use Stu\Module\Control\GameControllerInterface;
9
use Stu\Module\Control\ViewControllerInterface;
10
use Stu\Module\Starmap\Lib\StarmapUiFactoryInterface;
11
use Stu\Module\Starmap\View\ShowSection\ShowSectionRequestInterface;
12
use Stu\Orm\Entity\StarSystem;
13
use Stu\Orm\Repository\LayerRepositoryInterface;
14
use Stu\Orm\Repository\MapFieldTypeRepositoryInterface;
15
use Stu\Orm\Repository\StarSystemTypeRepositoryInterface;
16
17
final class EditSection implements ViewControllerInterface
18
{
19
    public const VIEW_IDENTIFIER = 'SHOW_EDIT_MAP_SECTION';
20
21
    private ShowSectionRequestInterface $request;
22
23
    private LayerRepositoryInterface $layerRepository;
24
25
    private MapFieldTypeRepositoryInterface $mapFieldTypeRepository;
26
27
    private StarmapUiFactoryInterface $starmapUiFactory;
28
29
    private StarSystemTypeRepositoryInterface $starSystemTypeRepository;
30
31
    public function __construct(
32
        ShowSectionRequestInterface $request,
33
        LayerRepositoryInterface $layerRepository,
34
        StarmapUiFactoryInterface $starmapUiFactory,
35
        MapFieldTypeRepositoryInterface $mapFieldTypeRepository,
36
        StarSystemTypeRepositoryInterface $starSystemTypeRepository
37
    ) {
38
        $this->request = $request;
39
        $this->layerRepository = $layerRepository;
40
        $this->mapFieldTypeRepository = $mapFieldTypeRepository;
41
        $this->starmapUiFactory = $starmapUiFactory;
42
        $this->starSystemTypeRepository = $starSystemTypeRepository;
43
    }
44
45
    public function handle(GameControllerInterface $game): void
46
    {
47
        $layerId = $this->request->getLayerId();
48
        $layer = $this->layerRepository->find($layerId);
49
50
        $xCoordinate = $this->request->getXCoordinate($layer);
0 ignored issues
show
Bug introduced by
It seems like $layer can also be of type null; however, parameter $layer of Stu\Module\Starmap\View\...rface::getXCoordinate() does only seem to accept Stu\Orm\Entity\LayerInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        $xCoordinate = $this->request->getXCoordinate(/** @scrutinizer ignore-type */ $layer);
Loading history...
51
        $yCoordinate = $this->request->getYCoordinate($layer);
0 ignored issues
show
Bug introduced by
It seems like $layer can also be of type null; however, parameter $layer of Stu\Module\Starmap\View\...rface::getYCoordinate() does only seem to accept Stu\Orm\Entity\LayerInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        $yCoordinate = $this->request->getYCoordinate(/** @scrutinizer ignore-type */ $layer);
Loading history...
52
        $section_id = $this->request->getSectionId();
53
54
        $maxx = $xCoordinate * MapEnum::FIELDS_PER_SECTION;
55
        $minx = $maxx - MapEnum::FIELDS_PER_SECTION + 1;
56
        $maxy = $yCoordinate * MapEnum::FIELDS_PER_SECTION;
57
        $miny = $maxy - MapEnum::FIELDS_PER_SECTION + 1;
58
59
        $fields = [];
60
        foreach (range($miny, $maxy) as $value) {
61
            $fields[] = $this->starmapUiFactory->createYRow($layerId, $value, $minx, $maxx);
62
        }
63
64
        if ($yCoordinate - 1 >= 1) {
65
            $game->setTemplateVar(
66
                'TOP_PREVIEW_ROW',
67
                $this->starmapUiFactory->createYRow($layerId, $yCoordinate * MapEnum::FIELDS_PER_SECTION - MapEnum::FIELDS_PER_SECTION, $minx, $maxx)->getFields()
68
            );
69
        } else {
70
            $game->setTemplateVar('TOP_PREVIEW_ROW', false);
71
        }
72
        if ($yCoordinate * MapEnum::FIELDS_PER_SECTION + 1 <= $layer->getHeight()) {
73
            $game->setTemplateVar(
74
                'BOTTOM_PREVIEW_ROW',
75
                $this->starmapUiFactory->createYRow($layerId, $yCoordinate * MapEnum::FIELDS_PER_SECTION + 1, $minx, $maxx)->getFields()
76
            );
77
        } else {
78
            $game->setTemplateVar(
79
                'BOTTOM_PREVIEW_ROW',
80
                false
81
            );
82
        }
83
        if ($xCoordinate - 1 >= 1) {
84
            $row = [];
85
            for ($i = $miny; $i <= $maxy; $i++) {
86
                $row[] = $this->starmapUiFactory->createYRow($layerId, $i, $minx - 1, $minx - 1);
87
            }
88
89
            $game->setTemplateVar(
90
                'LEFT_PREVIEW_ROW',
91
                $row
92
            );
93
        } else {
94
            $game->setTemplateVar(
95
                'LEFT_PREVIEW_ROW',
96
                false
97
            );
98
        }
99
100
        if ($xCoordinate * MapEnum::FIELDS_PER_SECTION + 1 <= $layer->getWidth()) {
101
            $row = [];
102
            for ($i = $miny; $i <= $maxy; $i++) {
103
                $row[] = $this->starmapUiFactory->createYRow($layerId, $i, $maxx + 1, $maxx + 1);
104
            }
105
106
            $game->setTemplateVar(
107
                'RIGHT_PREVIEW_ROW',
108
                $row
109
            );
110
        } else {
111
            $game->setTemplateVar(
112
                'RIGHT_PREVIEW_ROW',
113
                false
114
            );
115
        }
116
117
        $possibleFieldTypes = ['row_0', 'row_1', 'row_2', 'row_3', 'row_4', 'row_5'];
118
        foreach ($this->mapFieldTypeRepository->findAll() as $key => $value) {
119
            if ($value->getIsSystem()) {
120
                continue;
121
            }
122
            $possibleFieldTypes['row_' . ($key % 6)][] = $value;
123
        }
124
125
        $possibleSystemTypes = ['row_0', 'row_1', 'row_2', 'row_3', 'row_4', 'row_5'];
126
        foreach ($this->starSystemTypeRepository->findAll() as $key => $value) {
127
            if (!$value->getIsGenerateable()) {
128
                continue;
129
            }
130
            $possibleSystemTypes['row_' . ($key % 6)][] = $value;
131
        }
132
133
        $game->setTemplateFile('html/admin/mapeditor_section.xhtml');
134
        $game->appendNavigationPart('/admin/?SHOW_MAP_EDITOR=1', _('Karteneditor'));
135
        $game->appendNavigationPart(
136
            sprintf(
137
                '/admin/?SHOW_EDIT_MAP_SECTION=1&x=%d&y=%d&sec=%d&layerid=%d',
138
                $xCoordinate,
139
                $yCoordinate,
140
                $section_id,
141
                $layerId
142
            ),
143
            sprintf(_('Sektion %d anzeigen'), $section_id)
144
        );
145
        $game->setPageTitle(_('Sektion anzeigen'));
146
        $game->setTemplateVar('POSSIBLE_FIELD_TYPES', $possibleFieldTypes);
147
        $game->setTemplateVar('POSSIBLE_SYSTEM_TYPES', $possibleSystemTypes);
148
        $game->setTemplateVar('FIELDS_PER_SECTION', MapEnum::FIELDS_PER_SECTION);
149
        $game->setTemplateVar('SECTION_ID', $section_id);
150
        $game->setTemplateVar('HEAD_ROW', range($minx, $maxx));
151
        $game->setTemplateVar('MAP_FIELDS', $fields);
152
        $game->setTemplateVar('HAS_NAV_LEFT', $xCoordinate > 1);
153
        $game->setTemplateVar('HAS_NAV_RIGHT', $xCoordinate * MapEnum::FIELDS_PER_SECTION < $layer->getWidth());
154
        $game->setTemplateVar('HAS_NAV_UP', $yCoordinate > 1);
155
        $game->setTemplateVar('HAS_NAV_DOWN', $yCoordinate * MapEnum::FIELDS_PER_SECTION < $layer->getHeight());
156
        $game->setTemplateVar(
157
            'NAV_UP',
158
            sprintf(
159
                '?%s=1&x=%d&y=%d&sec=%d&layerid=%d',
160
                static::VIEW_IDENTIFIER,
161
                $xCoordinate,
162
                $yCoordinate > 1 ? $yCoordinate - 1 : 1,
163
                $section_id - 6,
164
                $layerId
165
            )
166
        );
167
        $game->setTemplateVar(
168
            'NAV_DOWN',
169
            sprintf(
170
                "?%s=1&x=%d&y=%d&sec=%d&layerid=%d",
171
                static::VIEW_IDENTIFIER,
172
                $xCoordinate,
173
                $yCoordinate + 1 > $layer->getHeight() / MapEnum::FIELDS_PER_SECTION ? $yCoordinate : $yCoordinate + 1,
174
                $section_id + 6,
175
                $layerId
176
            )
177
        );
178
        $game->setTemplateVar(
179
            'NAV_LEFT',
180
            sprintf(
181
                "?%s=1&x=%d&y=%d&sec=%d&layerid=%d",
182
                static::VIEW_IDENTIFIER,
183
                $xCoordinate > 1 ? $xCoordinate - 1 : 1,
184
                $yCoordinate,
185
                $section_id - 1,
186
                $layerId
187
            )
188
        );
189
        $game->setTemplateVar(
190
            'NAV_RIGHT',
191
            sprintf(
192
                '?%s=1&x=%d&y=%d&sec=%d&layerid=%d',
193
                static::VIEW_IDENTIFIER,
194
                $xCoordinate + 1 > $layer->getWidth() / MapEnum::FIELDS_PER_SECTION ? $xCoordinate : $xCoordinate + 1,
195
                $yCoordinate,
196
                $section_id + 1,
197
                $layerId
198
            )
199
        );
200
    }
201
}
202