Passed
Pull Request — master (#1777)
by Nico
22:32 queued 18s
created

ScrollBuildMenu::handle()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 42
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 31
c 0
b 0
f 0
nc 32
nop 1
dl 0
loc 42
ccs 0
cts 34
cp 0
crap 42
rs 8.8017
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Colony\Action\ScrollBuildMenu;
6
7
use request;
8
use Stu\Component\Building\BuildMenuEnum;
9
use Stu\Component\Colony\ColonyEnum;
10
use Stu\Lib\Colony\PlanetFieldHostProviderInterface;
11
use Stu\Module\Colony\View\ShowBuildMenuPart\ShowBuildMenuPart;
12
use Stu\Module\Control\ActionControllerInterface;
13
use Stu\Module\Control\GameControllerInterface;
14
use Stu\Orm\Repository\BuildingRepositoryInterface;
15
16
final class ScrollBuildMenu implements ActionControllerInterface
17
{
18
    public const ACTION_IDENTIFIER = 'B_SCROLL_BUILDMENU';
19
20
    private PlanetFieldHostProviderInterface $planetFieldHostProvider;
21
22
    private BuildingRepositoryInterface $buildingRepository;
23
24
    public function __construct(
25
        PlanetFieldHostProviderInterface $planetFieldHostProvider,
26
        BuildingRepositoryInterface $buildingRepository
27
    ) {
28
        $this->planetFieldHostProvider = $planetFieldHostProvider;
29
        $this->buildingRepository = $buildingRepository;
30
    }
31
32
    public function handle(GameControllerInterface $game): void
33
    {
34
        $userId = $game->getUser()->getId();
35
36
        $host = $this->planetFieldHostProvider->loadHostViaRequestParameters($game->getUser(), false);
37
38
        $menu = request::getIntFatal('menu');
39
        $offset = request::getInt('offset');
40
        $fieldType = request::has('fieldtype') ? request::getIntFatal('fieldtype') : null;
41
        if ($fieldType === 0) {
42
            $fieldType = null;
43
        }
44
45
        if ($offset < 0) {
46
            $offset = 0;
47
        }
48
        if ($offset % ColonyEnum::BUILDMENU_SCROLLOFFSET != 0) {
49
            $offset = (int)floor($offset / ColonyEnum::BUILDMENU_SCROLLOFFSET);
50
        }
51
        $ret = $this->buildingRepository->getBuildmenuBuildings(
52
            $host,
53
            $userId,
54
            $menu,
55
            $offset,
56
            null,
57
            $fieldType
58
        );
59
        if ($ret === []) {
60
            $offset = max(0, $offset - ColonyEnum::BUILDMENU_SCROLLOFFSET);
61
            $ret = $this->buildingRepository->getBuildmenuBuildings(
62
                $host,
63
                $userId,
64
                $menu,
65
                $offset,
66
                null,
67
                $fieldType
68
            );
69
        }
70
        $game->setTemplateVar('menu', ['buildings' => $ret, 'name' => BuildMenuEnum::getDescription($menu)]);
71
        $game->setTemplateVar('menutype', $menu);
72
        $game->setTemplateVar('scrolloffset', $offset);
73
        $game->setView(ShowBuildMenuPart::VIEW_IDENTIFIER);
74
    }
75
76
    public function performSessionCheck(): bool
77
    {
78
        return false;
79
    }
80
}
81