Test Failed
Push — dev ( 17f6d8...392257 )
by Janko
10:17 queued 12s
created

ComponentLoader::resetComponents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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