Passed
Push — dev ( 231d4b...6ea563 )
by Janko
101:30 queued 71:11
created

GameTwigRenderer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Control\Render;
6
7
use Noodlehaus\ConfigInterface;
8
use Override;
9
use Stu\Component\Game\GameEnum;
10
use Stu\Module\Config\StuConfigInterface;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Twig\TwigPageInterface;
13
use Stu\Orm\Entity\UserInterface;
14
15
/**
16
 * Executes the render chain for the site template
17
 *
18
 * Also registers a set of default variables for rendering
19
 */
20
final class GameTwigRenderer implements GameTwigRendererInterface
21
{
22
    private const string GAME_VERSION_DEV = 'dev';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 22 at column 25
Loading history...
23
24 2
    public function __construct(
25
        private TwigPageInterface $twigPage,
26
        private ConfigInterface $config,
27
        private StuConfigInterface $stuConfig
28 2
    ) {}
29
30 184
    #[Override]
31
    public function render(
32
        GameControllerInterface $game,
33
        ?UserInterface $user
34
    ): string {
35
36 184
        $this->setGameVariables($game);
37 184
        $this->setUserVariables($user);
38
39 184
        $this->twigPage->setVar('WIKI', $this->config->get('wiki.base_url'));
40 184
        $this->twigPage->setVar('FORUM', $this->config->get('board.base_url'));
41 184
        $this->twigPage->setVar('CHAT', $this->config->get('discord.url'));
42
43 184
        return $this->twigPage->render();
44
    }
45
46 184
    private function setGameVariables(GameControllerInterface $game): void
47
    {
48 184
        $this->twigPage->setVar('MACRO', $game->getMacro());
49 184
        $this->twigPage->setVar('NAVIGATION', $game->getNavigation());
50 184
        $this->twigPage->setVar('PAGETITLE', $game->getPageTitle());
51 184
        $this->twigPage->setVar('INFORMATION', $game->getInformation());
52 184
        $this->twigPage->setVar('TARGET_LINK', $game->getTargetLink());
53 184
        $this->twigPage->setVar('ACHIEVEMENTS', $game->getAchievements());
54 184
        $this->twigPage->setVar('EXECUTEJSBEFORERENDER', $game->getExecuteJS(GameEnum::JS_EXECUTION_BEFORE_RENDER));
55 184
        $this->twigPage->setVar('EXECUTEJSAFTERRENDER', $game->getExecuteJS(GameEnum::JS_EXECUTION_AFTER_RENDER));
56 184
        $this->twigPage->setVar('EXECUTEJSAJAXUPDATE', $game->getExecuteJS(GameEnum::JS_EXECUTION_AJAX_UPDATE));
57 184
        $this->twigPage->setVar('JAVASCRIPTPATH', $this->getJavascriptPath(), true);
58 184
        $this->twigPage->setVar('ISNPC', $game->isNpc());
59 184
        $this->twigPage->setVar('ISADMIN', $game->isAdmin());
60 184
        $this->twigPage->setVar('BENCHMARK', $game->getBenchmarkResult());
61 184
        $this->twigPage->setVar('GAME_STATS', $game->getGameStats());
62
63 184
        if ($game->hasUser()) {
64 184
            $this->twigPage->setVar('SESSIONSTRING', $game->getSessionString(), true);
65
        }
66
    }
67
68 184
    private function setUserVariables(?UserInterface $user): void
69
    {
70 184
        if ($user === null) {
71
            $this->twigPage->setVar('USER', null);
72
        } else {
73 184
            $this->twigPage->setVar('USER', new UserContainer(
74 184
                $user->getId(),
75 184
                $user->getAvatar(),
76 184
                $user->getName(),
77 184
                $user->getFactionId(),
78 184
                $user->getCss(),
79 184
                $user->hasStationsNavigation()
80 184
            ));
81
        }
82
    }
83
84 184
    private function getJavascriptPath(): string
85
    {
86 184
        $gameVersion = $this->stuConfig->getGameSettings()->getVersion();
87 184
        if ($gameVersion === self::GAME_VERSION_DEV) {
88 184
            return '/static';
89
        }
90
91
        return sprintf(
92
            '/version_%s/static',
93
            $gameVersion
94
        );
95
    }
96
}
97