Passed
Push — dev ( 1c3353...ed00e7 )
by Nico
29:47
created

TutorialProvider   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 2.08%

Importance

Changes 0
Metric Value
eloc 44
c 0
b 0
f 0
dl 0
loc 82
ccs 1
cts 48
cp 0.0208
rs 10
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
B setTemplateVariables() 0 78 10
1
<?php
2
3
namespace Stu\Module\Game\Lib;
4
5
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...
6
use Stu\Module\Control\GameControllerInterface;
7
use Stu\Module\Control\ViewContext;
8
use Stu\Orm\Repository\TutorialStepRepositoryInterface;
9
use Stu\Orm\Repository\UserTutorialRepositoryInterface;
10
11
final class TutorialProvider
12
{
13 4
    public function __construct(private TutorialStepRepositoryInterface $tutorialStepRepository, private UserTutorialRepositoryInterface $userTutorialRepository) {}
14
15
    public function setTemplateVariables(
16
        ViewContext $viewContext,
17
        GameControllerInterface $game
18
    ): void {
19
20
        /** @var array<int, array{elementIds: array<string>, title: string, text: string}> */
21
        $dummyarray = [
0 ignored issues
show
Unused Code introduced by
The assignment to $dummyarray is dead and can be removed.
Loading history...
22
            ['elementIds' => ['colsurface', 'submenu'], 'title' => 'Oberfläche und Kolonieinformationen', 'text' => 'Willkommen auf der Seite deiner Kolonie. Hier links siehst du die Oberfläche deiner Kolo mit allen Gebäuden bla bla bla. Hier findest du wichtige Informationen zu deiner Kolonie. Blub'],
23
            ['elementIds' => ['colonystorage'], 'title' => 'Kolonielager', 'text' => 'Ja und hier ist dein Lager mit allem Krams'],
24
            ['elementIds' => ['colmenubutton_1'], 'title' => 'Baumenü', 'text' => 'Klick mal aufs Baumenü und danach auf weiter. Dann können wir mal endlich was bauen.', 'innerUpdate' => 'switchColonySubmenu'],
25
            ['elementIds' => ['Baumaterialfabrik'], 'title' => 'Bau ne Baumaterialfabrik', 'text' => 'BM Fabriken sind mega dufte am anfang. Klick sie an und dann bau eine auf einem Gebäude auf deiner Oberfläche. Denk dran auf weiter zu klicken.', 'innerUpdate' => 'openBuildingInfo', 'fallbackIndex' => 2],
26
            ['elementIds' => ['buildinginfo'], 'title' => 'Gebäudeinformationen', 'text' => 'Hier siehst du alle Infos zu deinem Gebäude. Klick mal weiter.', 'fallbackIndex' => 2],
27
            ['elementIds' => ['colsurface'], 'title' => 'Bau jetzt endlich!', 'text' => 'Ja genau hier auf irgendeinem Feld das hier passend erscheint.', 'innerUpdate' => 'fieldMouseClick', 'fallbackIndex' => 2],
28
            ['elementIds' => ['colmenubutton_2'], 'title' => 'Infomenü', 'text' => 'Naja test']
29
        ];
30
31
        $user = $game->getUser();
32
        if ($user->getTutorials()->isEmpty()) {
33
            return;
34
        }
35
36
        $tutorialSteps = $this->tutorialStepRepository->findByUserAndViewContext(
37
            $game->getUser(),
38
            $viewContext
39
40
        );
41
        if (!$tutorialSteps) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tutorialSteps of type array<integer,Stu\Orm\En...\TutorialStepInterface> is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
42
            return;
43
        }
44
45
        $userTutorial = $this->userTutorialRepository->findUserTutorialByUserAndViewContext(
46
            $game->getUser(),
47
            $viewContext
48
        );
49
50
        if (!$userTutorial) {
51
            return;
52
        }
53
54
55
        $currentStep = $userTutorial->getTutorialStep()->getSort();
56
57
        $payloadArray = [];
58
        foreach ($tutorialSteps as $tutorialStep) {
59
            $result = [];
60
61
            if ($tutorialStep->getElementIds() !== null) {
62
                $result['elementIds'] = array_map('trim', explode(',', $tutorialStep->getElementIds()));
63
            }
64
65
            if ($tutorialStep->getTitle() !== null) {
66
                $result['title'] = trim((string)json_encode($tutorialStep->getTitle()), '"');
67
            }
68
69
            if ($tutorialStep->getText() !== null) {
70
                $result['text'] = trim((string)json_encode($tutorialStep->getText()), '"');
71
            }
72
73
            if ($tutorialStep->getInnerUpdate() !== null) {
74
                $result['innerUpdate'] = $tutorialStep->getInnerUpdate();
75
            }
76
77
            if ($tutorialStep->getFallbackIndex() !== null) {
78
                $result['fallbackIndex'] = $tutorialStep->getFallbackIndex();
79
            }
80
81
            $result['id'] = $tutorialStep->getId();
82
            $result['sort'] = $tutorialStep->getSort();
83
84
            $payloadArray[] = $result;
85
        }
86
87
        $game->addExecuteJS(sprintf(
88
            "updateTutorialStep('%s', %d);",
89
            json_encode($payloadArray),
90
            $currentStep
91
92
        ), GameEnum::JS_EXECUTION_AFTER_RENDER);
93
    }
94
}