Passed
Push — dev ( 219c54...831a1f )
by Janko
08:28
created

TutorialProvider::convertTutorialStep()   A

Complexity

Conditions 6
Paths 32

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 32
nop 1
dl 0
loc 28
ccs 0
cts 15
cp 0
crap 42
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Control\Component;
4
5
use Stu\Component\Game\JavascriptExecutionTypeEnum;
6
use Stu\Module\Control\GameControllerInterface;
7
use Stu\Module\Control\ViewContext;
8
use Stu\Orm\Entity\TutorialStep;
9
use Stu\Orm\Repository\TutorialStepRepositoryInterface;
10
use Stu\Orm\Repository\UserTutorialRepositoryInterface;
11
12
class TutorialProvider
13
{
14 2
    public function __construct(
15
        private UserTutorialRepositoryInterface $userTutorialRepository,
16
        private TutorialStepRepositoryInterface $tutorialStepRepository
17 2
    ) {}
18
19
    public function setTemplateVariables(
20
        ViewContext $viewContext,
21
        GameControllerInterface $game
22
    ): void {
23
24
        $user = $game->getUser();
25
26
        $userTutorials = $user->getTutorials();
27
        if ($userTutorials->isEmpty()) {
28
            return;
29
        }
30
31
        $userTutorial = $this->userTutorialRepository->findByUserAndViewContext($user, $viewContext);
32
        if ($userTutorial === null) {
33
            return;
34
        }
35
36
        $tutorialSteps = $this->tutorialStepRepository->findByUserAndViewContext($user, $viewContext);
37
        if ($tutorialSteps == []) {
38
            return;
39
        }
40
41
        $payloadArray = array_map(fn(TutorialStep $tutorialStep): array => $this->convertTutorialStep($tutorialStep), $tutorialSteps);
42
43
        $game->addExecuteJS(sprintf(
44
            "initTutorialSteps('%s', %d);",
45
            json_encode($payloadArray),
46
            $userTutorial->getTutorialStep()->getId()
47
        ), JavascriptExecutionTypeEnum::AFTER_RENDER);
48
    }
49
50
    /** @return array<string, mixed> */
51
    private function convertTutorialStep(TutorialStep $tutorialStep): array
52
    {
53
        $result = [];
54
55
        if ($tutorialStep->getElementIds() !== null) {
56
            $result['elementIds'] = array_map('trim', explode(',', $tutorialStep->getElementIds()));
57
        }
58
59
        if ($tutorialStep->getTitle() !== null) {
60
            $result['title'] = trim((string)json_encode($tutorialStep->getTitle()), '"');
61
        }
62
63
        if ($tutorialStep->getText() !== null) {
64
            $result['text'] = trim((string)json_encode($tutorialStep->getText()), '"');
65
        }
66
67
        if ($tutorialStep->getInnerUpdate() !== null) {
68
            $result['innerUpdate'] = $tutorialStep->getInnerUpdate();
69
        }
70
71
        if ($tutorialStep->getFallbackIndex() !== null) {
72
            $result['fallbackIndex'] = $tutorialStep->getFallbackIndex();
73
        }
74
75
        $result['previousid'] = $tutorialStep->getPreviousStepId();
76
        $result['nextid'] = $tutorialStep->getNextStepId();
77
78
        return $result;
79
    }
80
}
81