Passed
Pull Request — master (#1918)
by Janko
49:46 queued 19:04
created

TutorialProvider   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 4.88%

Importance

Changes 0
Metric Value
eloc 37
c 0
b 0
f 0
dl 0
loc 73
ccs 2
cts 41
cp 0.0488
rs 10
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B setTemplateVariables() 0 66 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(
14
        private TutorialStepRepositoryInterface $tutorialStepRepository,
15
        private UserTutorialRepositoryInterface $userTutorialRepository
16 4
    ) {}
17
18
    public function setTemplateVariables(
19
        ViewContext $viewContext,
20
        GameControllerInterface $game
21
    ): void {
22
23
        $user = $game->getUser();
24
        if ($user->getTutorials()->isEmpty()) {
25
            return;
26
        }
27
28
        $tutorialSteps = $this->tutorialStepRepository->findByUserAndViewContext(
29
            $game->getUser(),
30
            $viewContext
31
        );
32
        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...
33
            return;
34
        }
35
36
        $userTutorial = $this->userTutorialRepository->findUserTutorialByUserAndViewContext(
37
            $game->getUser(),
38
            $viewContext
39
        );
40
41
        if (!$userTutorial) {
42
            return;
43
        }
44
45
46
        $currentStep = $userTutorial->getTutorialStep();
47
        $currentStepId = $currentStep->getId();
48
49
        $payloadArray = [];
50
        foreach ($tutorialSteps as $tutorialStep) {
51
            $result = [];
52
53
            if ($tutorialStep->getElementIds() !== null) {
54
                $result['elementIds'] = array_map('trim', explode(',', $tutorialStep->getElementIds()));
55
            }
56
57
            if ($tutorialStep->getTitle() !== null) {
58
                $result['title'] = trim((string)json_encode($tutorialStep->getTitle()), '"');
59
            }
60
61
            if ($tutorialStep->getText() !== null) {
62
                $result['text'] = trim((string)json_encode($tutorialStep->getText()), '"');
63
            }
64
65
            if ($tutorialStep->getInnerUpdate() !== null) {
66
                $result['innerUpdate'] = $tutorialStep->getInnerUpdate();
67
            }
68
69
            if ($tutorialStep->getFallbackIndex() !== null) {
70
                $result['fallbackIndex'] = $tutorialStep->getFallbackIndex();
71
            }
72
73
            $result['previousid'] = $tutorialStep->getPreviousStepId();
74
            $result['nextid'] = $tutorialStep->getNextStepId();
75
76
            $payloadArray[$tutorialStep->getId()] = $result;
77
        }
78
79
        $game->addExecuteJS(sprintf(
80
            "initTutorialSteps('%s', %d);",
81
            json_encode($payloadArray),
82
            $currentStepId
83
        ), GameEnum::JS_EXECUTION_AFTER_RENDER);
84
    }
85
}
86