Passed
Push — master ( f544cb...b3a3d9 )
by Nico
36:43 queued 09:10
created

SwitchColonyMenu   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 27
c 0
b 0
f 0
dl 0
loc 60
ccs 0
cts 27
cp 0
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A performSessionCheck() 0 3 1
A handle() 0 24 5
A hasSpecialBuilding() 0 7 1
A __construct() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Colony\Action\SwitchColonyMenu;
6
7
use request;
8
use Stu\Component\Colony\ColonyMenuEnum;
9
use Stu\Lib\Colony\PlanetFieldHostInterface;
10
use Stu\Lib\Colony\PlanetFieldHostProviderInterface;
11
use Stu\Module\Building\BuildingFunctionTypeEnum;
12
use Stu\Module\Control\ActionControllerInterface;
13
use Stu\Module\Control\GameControllerInterface;
14
use Stu\Orm\Repository\BuildingFunctionRepositoryInterface;
15
use Stu\Orm\Repository\PlanetFieldRepositoryInterface;
16
17
final class SwitchColonyMenu implements ActionControllerInterface
18
{
19
    public const ACTION_IDENTIFIER = 'B_SWITCH_COLONYMENU';
20
21
    private PlanetFieldHostProviderInterface $planetFieldHostProvider;
22
23
    private BuildingFunctionRepositoryInterface $buildingFunctionRepository;
24
25
    private PlanetFieldRepositoryInterface $planetFieldRepository;
26
27
    public function __construct(
28
        PlanetFieldHostProviderInterface $planetFieldHostProvider,
29
        BuildingFunctionRepositoryInterface $buildingFunctionRepository,
30
        PlanetFieldRepositoryInterface $planetFieldRepository
31
    ) {
32
        $this->planetFieldHostProvider = $planetFieldHostProvider;
33
        $this->buildingFunctionRepository = $buildingFunctionRepository;
34
        $this->planetFieldRepository = $planetFieldRepository;
35
    }
36
37
    public function handle(GameControllerInterface $game): void
38
    {
39
        $host = $this->planetFieldHostProvider->loadHostViaRequestParameters($game->getUser());
40
41
        $menu = ColonyMenuEnum::getFor(request::getIntFatal('menu'));
42
43
        if (!$host->isMenuAllowed($menu)) {
44
            $game->addInformation('Dieses Menü ist nicht für die Sandbox geeignet');
45
            $game->setView($host->getDefaultViewIdentifier());
46
            return;
47
        }
48
49
        $game->setView($menu->getViewIdentifier());
50
51
        $neededBuildingFunctions = $menu->getNeededBuildingFunction();
52
        if (
53
            $neededBuildingFunctions !== null
54
            && !$this->hasSpecialBuilding($host, $neededBuildingFunctions)
55
        ) {
56
            return;
57
        }
58
        if (BuildingFunctionTypeEnum::isBuildingFunctionMandatory($menu)) {
59
            $func = $this->buildingFunctionRepository->find(request::getIntFatal('func'));
60
            $game->setTemplateVar('FUNC', $func);
61
        }
62
    }
63
64
    /** @param array<int> $functions */
65
    private function hasSpecialBuilding(PlanetFieldHostInterface $host, array $functions): bool
66
    {
67
        return $this->planetFieldRepository->getCountByColonyAndBuildingFunctionAndState(
68
            $host,
69
            $functions,
70
            [0, 1]
71
        ) > 0;
72
    }
73
74
    public function performSessionCheck(): bool
75
    {
76
        return false;
77
    }
78
}
79