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

ViewComponentLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
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\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