Passed
Pull Request — master (#1696)
by Nico
49:43 queued 22:34
created

ColonyGuiHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Colony\Lib\Gui;
6
7
use RuntimeException;
8
use Stu\Component\Colony\ColonyMenuEnum;
9
use Stu\Lib\Colony\PlanetFieldHostInterface;
10
use Stu\Module\Colony\Lib\Gui\Component\GuiComponentProviderInterface;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Orm\Entity\ColonyInterface;
13
use Stu\Orm\Entity\ColonySandboxInterface;
14
15
final class ColonyGuiHelper implements ColonyGuiHelperInterface
16
{
17
    /** @var array<int, GuiComponentProviderInterface> */
18
    private array $guiComponentProviders;
19
20
    /** @param array<int, GuiComponentProviderInterface> $guiComponentProviders */
21
    public function __construct(array $guiComponentProviders)
22
    {
23
        $this->guiComponentProviders = $guiComponentProviders;
24
    }
25
26
    public function registerMenuComponents(
27
        ColonyMenuEnum $menu,
28
        PlanetFieldHostInterface $host,
29
        GameControllerInterface $game
30
    ): void {
31
32
        foreach ($menu->getNecessaryGuiComponents() as $component) {
33
            $this->processComponent($component, $host, $game);
34
        }
35
36
        $game->setTemplateVar('HOST', $host);
37
        $game->setTemplateVar('CURRENT_MENU', $menu);
38
39
        if ($host instanceof ColonyInterface) {
40
            $game->setTemplateVar('COLONY', $host);
41
            $game->setTemplateVar('FORM_ACTION', 'colony.php');
42
        }
43
        if ($host instanceof ColonySandboxInterface) {
44
            $game->setTemplateVar('COLONY', $host->getColony());
45
            $game->setTemplateVar('FORM_ACTION', '/admin/index.php');
46
        }
47
    }
48
49
    public function registerComponents(
50
        PlanetFieldHostInterface $host,
51
        GameControllerInterface $game,
52
        array $guiComponents
53
    ): void {
54
        foreach ($guiComponents as $component) {
55
            $this->processComponent($component, $host, $game);
56
        }
57
    }
58
59
    private function processComponent(
60
        GuiComponentEnum $guiComponent,
61
        PlanetFieldHostInterface $host,
62
        GameControllerInterface $game,
63
    ): void {
64
        if (!array_key_exists($guiComponent->value, $this->guiComponentProviders)) {
65
            throw new RuntimeException(sprintf('guiComponentProvider with follwing id does not exist: %d', $guiComponent->value));
66
        }
67
68
        $componentProvider = $this->guiComponentProviders[$guiComponent->value];
69
        $componentProvider->setTemplateVariables($host, $game);
70
    }
71
}
72