Passed
Push — dev ( 807205...be9384 )
by Janko
27:21
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
c 0
b 0
f 0
nc 32
nop 1
dl 0
loc 28
ccs 0
cts 15
cp 0
crap 42
rs 9.2222
1
<?php
2
3
namespace Stu\Module\Game\Lib;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
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...
8
use Stu\Module\Control\GameControllerInterface;
9
use Stu\Module\Control\ViewContext;
10
use Stu\Orm\Entity\TutorialStepInterface;
11
use Stu\Orm\Entity\UserTutorialInterface;
12
use Stu\Orm\Repository\TutorialStepRepositoryInterface;
13
use Stu\Orm\Repository\UserTutorialRepositoryInterface;
14
15
final class TutorialProvider
16
{
17 4
    public function __construct(
18
        private UserTutorialRepositoryInterface $userTutorialRepository,
19
        private TutorialStepRepositoryInterface $tutorialStepRepository
20 4
    ) {}
21
22
    public function setTemplateVariables(
23
        ViewContext $viewContext,
24
        GameControllerInterface $game
25
    ): void {
26
27
        $user = $game->getUser();
28
29
        $userTutorials = $user->getTutorials();
30
        if ($userTutorials->isEmpty()) {
31
            return;
32
        }
33
34
        $userTutorial = $this->userTutorialRepository->findByUserAndViewContext($user, $viewContext);
35
        if ($userTutorial === null) {
36
            return;
37
        }
38
39
        $tutorialSteps = $this->tutorialStepRepository->findByUserAndViewContext($user, $viewContext);
40
        if ($tutorialSteps == []) {
41
            return;
42
        }
43
44
        $currentStep = $userTutorial->getTutorialStep();
45
        $currentStepId = $currentStep->getId();
46
47
        $payloadArray = $this->getAllTutorialSteps($userTutorial)
48
            ->map(fn(TutorialStepInterface $tutorialStep) => $this->convertTutorialStep($tutorialStep))
49
            ->toArray();
50
51
        $game->addExecuteJS(sprintf(
52
            "initTutorialSteps('%s', %d);",
53
            json_encode($payloadArray),
54
            $currentStepId
55
        ), GameEnum::JS_EXECUTION_AFTER_RENDER);
56
    }
57
58
    /**
59
     * @return Collection<int, TutorialStepInterface>
60
     */
61
    private function getAllTutorialSteps(UserTutorialInterface $userTutorial): Collection
62
    {
63
        $result = new ArrayCollection();
64
65
        $currentStep = $userTutorial->getTutorialStep();
66
        $result->set($currentStep->getId(), $currentStep);
67
68
        $this->addPreviousSteps($currentStep->getPreviousStep(), $result);
69
        $this->addNextSteps($currentStep->getNextStep(), $result);
70
71
        return $result;
72
    }
73
74
    /**
75
     * @param Collection<int, TutorialStepInterface> $steps
76
     */
77
    private function addPreviousSteps(?TutorialStepInterface $previousStep, Collection $steps): void
78
    {
79
        if ($previousStep == null) {
80
            return;
81
        }
82
83
        $steps->set($previousStep->getId(), $previousStep);
84
        $this->addPreviousSteps($previousStep->getPreviousStep(), $steps);
85
    }
86
87
    /**
88
     * @param Collection<int, TutorialStepInterface> $steps
89
     */
90
    private function addNextSteps(?TutorialStepInterface $nextStep, Collection $steps): void
91
    {
92
        if ($nextStep == null) {
93
            return;
94
        }
95
96
        $steps->set($nextStep->getId(), $nextStep);
97
        $this->addNextSteps($nextStep->getNextStep(), $steps);
98
    }
99
100
    /** @return array<string, mixed> */
101
    private function convertTutorialStep(TutorialStepInterface $tutorialStep): array
102
    {
103
        $result = [];
104
105
        if ($tutorialStep->getElementIds() !== null) {
106
            $result['elementIds'] = array_map('trim', explode(',', $tutorialStep->getElementIds()));
107
        }
108
109
        if ($tutorialStep->getTitle() !== null) {
110
            $result['title'] = trim((string)json_encode($tutorialStep->getTitle()), '"');
111
        }
112
113
        if ($tutorialStep->getText() !== null) {
114
            $result['text'] = trim((string)json_encode($tutorialStep->getText()), '"');
115
        }
116
117
        if ($tutorialStep->getInnerUpdate() !== null) {
118
            $result['innerUpdate'] = $tutorialStep->getInnerUpdate();
119
        }
120
121
        if ($tutorialStep->getFallbackIndex() !== null) {
122
            $result['fallbackIndex'] = $tutorialStep->getFallbackIndex();
123
        }
124
125
        $result['previousid'] = $tutorialStep->getPreviousStepId();
126
        $result['nextid'] = $tutorialStep->getNextStepId();
127
128
        return $result;
129
    }
130
}
131