Passed
Push — master ( be429c...fef648 )
by Nico
28:06
created

ComponentLoader   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 36
c 1
b 0
f 1
dl 0
loc 87
ccs 40
cts 40
cp 1
rs 10
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addComponentUpdate() 0 4 2
A registerComponent() 0 4 2
A loadComponentUpdates() 0 22 6
A addExecuteJs() 0 10 1
A loadRegisteredComponents() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Game\Lib\Component;
6
7
use RuntimeException;
8
use Stu\Component\Game\GameEnum;
9
use Stu\Component\Game\ModuleViewEnum;
10
use Stu\Module\Control\GameControllerInterface;
11
use Stu\Module\Control\Render\Fragments\RenderFragmentInterface;
12
use Stu\Module\Game\View\ShowComponent\ShowComponent;
13
use Stu\Module\Twig\TwigPageInterface;
14
15
final class ComponentLoader implements ComponentLoaderInterface
16
{
17
    /** @var array<int, RenderFragmentInterface> */
18
    private array $componentProviders;
19
20
    /** @var array<string, ComponentUpdate> */
21
    private array $componentUpdates = [];
22
23
    /** @var array<ComponentEnum> */
24
    private array $neededComponents = [];
25
26
    /** @param array<int, RenderFragmentInterface> $componentProviders */
27 10
    public function __construct(
28
        array $componentProviders
29
    ) {
30 10
        $this->componentProviders = $componentProviders;
31
    }
32
33
34 4
    public function addComponentUpdate(ComponentEnum $component, bool $isInstantUpdate = true): void
35
    {
36 4
        if (!array_key_exists($component->value, $this->componentUpdates)) {
37 4
            $this->componentUpdates[$component->value] = new ComponentUpdate($component, $isInstantUpdate);
38
        }
39
    }
40
41
    /** 
42
     * Adds the execute javascript after render.
43
     */
44 4
    public function loadComponentUpdates(GameControllerInterface $game): void
45
    {
46 4
        foreach ($this->componentUpdates as $update) {
47
48 4
            $component = $update->getComponent();
49 4
            $isInstantUpdate = $update->isInstantUpdate();
50
51 4
            if ($isInstantUpdate) {
52 2
                $this->addExecuteJs(
53 2
                    $component->value,
54 2
                    '',
55 2
                    $game
56 2
                );
57
            }
58
59 4
            $refreshInterval = $component->getRefreshIntervalInSeconds();
60
61 4
            if (!$isInstantUpdate || $refreshInterval !== null) {
62 3
                $this->addExecuteJs(
63 3
                    $component->value,
64 3
                    $refreshInterval === null ? '' : sprintf(', %d', $refreshInterval * 1000),
65 3
                    $game
66 3
                );
67
            }
68
        }
69
    }
70
71 4
    private function addExecuteJs(string $componentValue, string $refreshParam, GameControllerInterface $game): void
72
    {
73 4
        $game->addExecuteJS(sprintf(
74 4
            "updateComponent('navlet_%s', '/%s?%s=1&component=%s'%s);",
75 4
            $componentValue,
76 4
            ModuleViewEnum::GAME->getPhpPage(),
77 4
            ShowComponent::VIEW_IDENTIFIER,
78 4
            $componentValue,
79 4
            $refreshParam
80 4
        ), GameEnum::JS_EXECUTION_AFTER_RENDER);
81
    }
82
83 2
    public function registerComponent(ComponentEnum $component): void
84
    {
85 2
        if (!in_array($component, $this->neededComponents)) {
86 2
            $this->neededComponents[] = $component;
87
        }
88
    }
89
90 2
    public function loadRegisteredComponents(
91
        TwigPageInterface $twigPage,
92
        GameControllerInterface $game
93
    ): void {
94
95 2
        foreach ($this->neededComponents as $component) {
96 2
            if (!array_key_exists($component->value, $this->componentProviders)) {
97 1
                throw new RuntimeException(sprintf('componentProvider with follwing id does not exist: %s', $component->value));
98
            }
99
100 1
            $componentProvider = $this->componentProviders[$component->value];
101 1
            $componentProvider->render($game->getUser(), $twigPage, $game);
102
        }
103
    }
104
}
105