Passed
Pull Request — master (#1699)
by Nico
43:19 queued 18:45
created

ViewComponentLoader::registerViewComponents()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Game\Lib\View;
6
7
use RuntimeException;
8
use Stu\Component\Game\ModuleViewEnum;
9
use Stu\Module\Control\GameControllerInterface;
10
use Stu\Module\Game\Lib\View\Provider\ViewComponentProviderInterface;
11
12
final class ViewComponentLoader implements ViewComponentLoaderInterface
13
{
14
    /** @var array<int, ViewComponentProviderInterface> */
15
    private array $viewComponentProviders;
16
17
    /** @param array<int, ViewComponentProviderInterface> $viewComponentProviders */
18 2
    public function __construct(
19
        array $viewComponentProviders
20
    ) {
21 2
        $this->viewComponentProviders = $viewComponentProviders;
22
    }
23
24 2
    public function registerViewComponents(
25
        ModuleViewEnum $view,
26
        GameControllerInterface $game
27
    ): void {
28
29 2
        if (!array_key_exists($view->value, $this->viewComponentProviders)) {
30 1
            throw new RuntimeException(sprintf('viewComponentProvider with follwing id does not exist: %s', $view->value));
31
        }
32
33 1
        $game->appendNavigationPart(
34 1
            $view->getPhpPage(),
35 1
            $view->getTitle(),
36 1
        );
37
38 1
        $componentProvider = $this->viewComponentProviders[$view->value];
39 1
        $componentProvider->setTemplateVariables($game);
40
41 1
        $game->setTemplateVar('CURRENT_VIEW', $view);
42
    }
43
}
44