Passed
Push — dev ( 8e4f3d...84d979 )
by Janko
29:51
created

GameTwigRenderer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 97.22%

Importance

Changes 0
Metric Value
eloc 34
c 0
b 0
f 0
dl 0
loc 57
ccs 35
cts 36
cp 0.9722
rs 10
wmc 6

4 Methods

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