Passed
Pull Request — master (#1699)
by Nico
43:19 queued 18:45
created

GameTwigRenderer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Control\Render;
6
7
use Noodlehaus\ConfigInterface;
8
use Stu\Component\Game\GameEnum;
9
use Stu\Module\Control\GameControllerInterface;
10
use Stu\Module\Game\Lib\Component\ComponentLoaderInterface;
11
use Stu\Module\Twig\TwigPageInterface;
12
use Stu\Orm\Entity\UserInterface;
13
14
/**
15
 * Executes the render chain for the site template
16
 *
17
 * Also registers a set of default variables for rendering
18
 */
19
final class GameTwigRenderer implements GameTwigRendererInterface
20
{
21
    private ConfigInterface $config;
22
23
    private ComponentLoaderInterface $componentLoader;
24
25 4
    public function __construct(
26
        ConfigInterface $config,
27
        ComponentLoaderInterface $componentLoader
28
    ) {
29 4
        $this->config = $config;
30 4
        $this->componentLoader = $componentLoader;
31
    }
32
33
    public function render(
34
        GameControllerInterface $game,
35
        ?UserInterface $user,
36
        TwigPageInterface $twigPage
37
    ): string {
38
39
        $this->componentLoader->loadComponentUpdates($game);
40
        $this->componentLoader->loadRegisteredComponents($twigPage, $game);
41
        $this->setGameVariables($twigPage, $game);
42
        $this->setUserVariables($user, $twigPage);
43
44
        $twigPage->setVar('WIKI', $this->config->get('wiki.base_url'));
45
        $twigPage->setVar('FORUM', $this->config->get('board.base_url'));
46
        $twigPage->setVar('CHAT', $this->config->get('discord.url'));
47
48
        return $twigPage->render();
49
    }
50
51
    private function setGameVariables(TwigPageInterface $twigPage, GameControllerInterface $game): void
52
    {
53
        $twigPage->setVar('MACRO', $game->getMacro());
54
        $twigPage->setVar('NAVIGATION', $game->getNavigation());
55
        $twigPage->setVar('PAGETITLE', $game->getPageTitle());
56
        $twigPage->setVar('INFORMATION', $game->getInformation());
57
        $twigPage->setVar('ACHIEVEMENTS', $game->getAchievements());
58
        $twigPage->setVar('EXECUTEJSBEFORERENDER', $game->getExecuteJS(GameEnum::JS_EXECUTION_BEFORE_RENDER));
59
        $twigPage->setVar('EXECUTEJSAFTERRENDER', $game->getExecuteJS(GameEnum::JS_EXECUTION_AFTER_RENDER));
60
        $twigPage->setVar('EXECUTEJSAJAXUPDATE', $game->getExecuteJS(GameEnum::JS_EXECUTION_AJAX_UPDATE));
61
        $twigPage->setVar('SESSIONSTRING', $game->getSessionString(), true);
62
        $twigPage->setVar('JAVASCRIPTPATH', $game->getJavascriptPath(), true);
63
        $twigPage->setVar('ISADMIN', $game->isAdmin());
64
        $twigPage->setVar('BENCHMARK', $game->getBenchmarkResult());
65
    }
66
67
    private function setUserVariables(?UserInterface $user, TwigPageInterface $twigPage): void
68
    {
69
        if ($user === null) {
70
            $twigPage->setVar('USER', null);
71
        } else {
72
            $twigPage->setVar('USER', new UserContainer(
73
                $user->getId(),
74
                $user->getAvatar(),
75
                $user->getName(),
76
                $user->getFactionId(),
77
                $user->getCss(),
78
                $user->hasStationsNavigation(),
79
                $user->getDeals()
80
            ));
81
        }
82
    }
83
}
84