Passed
Push — dev ( b6688d...d42601 )
by Janko
11:12
created

ComponentLoader::registerStubbedComponent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
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 1
dl 0
loc 6
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\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\Config\Init;
11
use Stu\Lib\Component\ComponentInterface;
12
use Stu\Module\Control\GameControllerInterface;
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
15
final class ComponentLoader implements ComponentLoaderInterface
16
{
17
    /** @var array<ComponentEnumInterface> */
18
    private array $registeredStubs = [];
19
20 7
    public function __construct(
21
        private ComponentRegistrationInterface $componentRegistration,
22
        private ComponentRendererInterface $componentRenderer,
23 7
    ) {}
24
25
    /**
26
     * Adds the execute javascript after render.
27
     */
28 172
    #[Override]
29
    public function loadComponentUpdates(GameControllerInterface $game): void
30
    {
31 172
        foreach ($this->componentRegistration->getComponentUpdates() as $id => $update) {
32
33 7
            $componentEnum = $update->getComponentEnum();
34 7
            $isInstantUpdate = $update->isInstantUpdate();
35
36 7
            if ($isInstantUpdate) {
37 2
                $this->addExecuteJs(
38 2
                    $id,
39 2
                    $componentEnum,
40 2
                    '',
41 2
                    $game
42 2
                );
43
            }
44
45 7
            $refreshInterval = $componentEnum->getRefreshIntervalInSeconds();
46
47 7
            if (!$isInstantUpdate || $refreshInterval !== null) {
48 6
                $this->addExecuteJs(
49 6
                    $id,
50 6
                    $componentEnum,
51 6
                    $refreshInterval === null ? '' : sprintf(', %d', $refreshInterval * 1000),
52 6
                    $game
53 6
                );
54
            }
55
        }
56
    }
57
58 7
    private function addExecuteJs(string $id, ComponentEnumInterface $componentEnum, string $refreshParam, GameControllerInterface $game): void
59
    {
60 7
        $moduleView = $componentEnum->getModuleView();
61
62 7
        $game->addExecuteJS(sprintf(
63 7
            "updateComponent('%s', '/%s?%s=1&id=%s'%s);",
64 7
            $id,
65 7
            $moduleView->getPhpPage(),
66 7
            ShowComponent::VIEW_IDENTIFIER,
67 7
            $id,
68 7
            $refreshParam
69 7
        ), GameEnum::JS_EXECUTION_AFTER_RENDER);
70
    }
71
72 169
    #[Override]
73
    public function loadRegisteredComponents(GameControllerInterface $game): void
74
    {
75 169
        foreach ($this->componentRegistration->getRegisteredComponents() as $id => $componentEnum) {
76
77 4
            $isStubbed = in_array($componentEnum, $this->registeredStubs);
78
79 4
            if (!$isStubbed && $componentEnum->hasTemplateVariables()) {
80 4
                $moduleId = strtoupper($componentEnum->getModuleView()->value);
81
82
                /** @var array<string, ComponentInterface> */
83 4
                $moduleComponents = Init::getContainer()
84 4
                    ->get(sprintf('%s_COMPONENTS', $moduleId));
85
86 4
                if (!array_key_exists($componentEnum->getValue(), $moduleComponents)) {
87
                    throw new RuntimeException(sprintf('component with follwing id does not exist: %s', $id));
88
                }
89
90 4
                $component = $moduleComponents[$componentEnum->getValue()];
91 4
                $this->componentRenderer->renderComponent($component, $game);
92
            }
93
94 4
            $game->setTemplateVar($id, ['id' => $id, 'template' => $isStubbed ? null : $componentEnum->getTemplate()]);
95
        }
96
    }
97
98 147
    #[Override]
99
    public function registerStubbedComponent(ComponentEnumInterface $componentEnum): ComponentLoaderInterface
100
    {
101 147
        $this->registeredStubs[] = $componentEnum;
102
103 147
        return $this;
104
    }
105
106 168
    #[Override]
107
    public function resetStubbedComponents(): void
108
    {
109 168
        $this->registeredStubs = [];
110
    }
111
}
112