Passed
Pull Request — master (#1969)
by Janko
22:34 queued 10:03
created

GameTwigRenderer::setUserVariables()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 12
ccs 9
cts 10
cp 0.9
crap 2.004
rs 9.9332
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 Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Stu\Component\Game\GameEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Game\GameEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Stu\Lib\Component\ComponentLoaderInterface;
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 2
    public function __construct(
23
        private TwigPageInterface $twigPage,
24
        private ConfigInterface $config,
25
        private ComponentLoaderInterface $componentLoader
26 2
    ) {}
27
28 181
    #[Override]
29
    public function render(
30
        GameControllerInterface $game,
31
        ?UserInterface $user
32
    ): string {
33
34 181
        $this->componentLoader->loadComponentUpdates($game);
35 181
        $this->componentLoader->loadRegisteredComponents($game);
36 181
        $this->setGameVariables($game);
37 181
        $this->setUserVariables($user);
38
39 181
        $this->twigPage->setVar('WIKI', $this->config->get('wiki.base_url'));
40 181
        $this->twigPage->setVar('FORUM', $this->config->get('board.base_url'));
41 181
        $this->twigPage->setVar('CHAT', $this->config->get('discord.url'));
42
43 181
        return $this->twigPage->render();
44
    }
45
46 181
    private function setGameVariables(GameControllerInterface $game): void
47
    {
48 181
        $this->twigPage->setVar('MACRO', $game->getMacro());
49 181
        $this->twigPage->setVar('NAVIGATION', $game->getNavigation());
50 181
        $this->twigPage->setVar('PAGETITLE', $game->getPageTitle());
51 181
        $this->twigPage->setVar('INFORMATION', $game->getInformation());
52 181
        $this->twigPage->setVar('TARGET_LINK', $game->getTargetLink());
53 181
        $this->twigPage->setVar('ACHIEVEMENTS', $game->getAchievements());
54 181
        $this->twigPage->setVar('EXECUTEJSBEFORERENDER', $game->getExecuteJS(GameEnum::JS_EXECUTION_BEFORE_RENDER));
55 181
        $this->twigPage->setVar('EXECUTEJSAFTERRENDER', $game->getExecuteJS(GameEnum::JS_EXECUTION_AFTER_RENDER));
56 181
        $this->twigPage->setVar('EXECUTEJSAJAXUPDATE', $game->getExecuteJS(GameEnum::JS_EXECUTION_AJAX_UPDATE));
57 181
        $this->twigPage->setVar('JAVASCRIPTPATH', $game->getJavascriptPath(), true);
58 181
        $this->twigPage->setVar('ISNPC', $game->isNpc());
59 181
        $this->twigPage->setVar('ISADMIN', $game->isAdmin());
60 181
        $this->twigPage->setVar('BENCHMARK', $game->getBenchmarkResult());
61 181
        $this->twigPage->setVar('GAME_STATS', $game->getGameStats());
62
63 181
        if ($game->hasUser()) {
64 181
            $this->twigPage->setVar('SESSIONSTRING', $game->getSessionString(), true);
65
        }
66
    }
67
68 181
    private function setUserVariables(?UserInterface $user): void
69
    {
70 181
        if ($user === null) {
71
            $this->twigPage->setVar('USER', null);
72
        } else {
73 181
            $this->twigPage->setVar('USER', new UserContainer(
74 181
                $user->getId(),
75 181
                $user->getAvatar(),
76 181
                $user->getName(),
77 181
                $user->getFactionId(),
78 181
                $user->getCss(),
79 181
                $user->hasStationsNavigation()
80 181
            ));
81
        }
82
    }
83
}
84