Passed
Push — master ( 32c6f4...f8861d )
by Nico
26:07 queued 17:33
created

setTemplateVariables()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 19
c 2
b 0
f 0
nc 3
nop 3
dl 0
loc 33
ccs 0
cts 19
cp 0
crap 20
rs 9.6333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\View\ShowSystemSettings;
6
7
use Stu\Component\Spacecraft\System\Data\SubspaceSystemData;
8
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
9
use Stu\Component\Spacecraft\System\SpacecraftSystemWrapperFactoryInterface;
10
use Stu\Module\Control\GameControllerInterface;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Control\GameControllerInterface 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...
11
use Stu\Module\Control\StuTime;
12
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
13
use Stu\Orm\Repository\FlightSignatureRepositoryInterface;
14
15
class SubspaceSensorSystemSettings implements SystemSettingsProviderInterface
16
{
17 1
    public function __construct(
18
        private readonly SpacecraftSystemWrapperFactoryInterface $spacecraftSystemWrapperFactory,
19
        private readonly FlightSignatureRepositoryInterface $flightSignatureRepository,
20
        private readonly StuTime $stuTime
21 1
    ) {}
22
23
    public function setTemplateVariables(
24
        SpacecraftSystemTypeEnum $systemType,
25
        SpacecraftWrapperInterface $wrapper,
26
        GameControllerInterface $game
27
    ): void {
28
        $game->setMacroInAjaxWindow('html/spacecraft/system/subspaceScanner.twig');
29
30
        $spacecraft = $wrapper->get();
31
32
        $isSubspaceScannerActive = $spacecraft->getSystemState(SpacecraftSystemTypeEnum::SUBSPACE_SCANNER);
33
        $isMatrixScannerHealthy = $spacecraft->isSystemHealthy(SpacecraftSystemTypeEnum::MATRIX_SCANNER);
34
35
        if ($isMatrixScannerHealthy && $isSubspaceScannerActive) {
36
37
            $subspaceSystemData = $wrapper->getSubspaceSystemData();
38
            if ($subspaceSystemData === null) {
39
                return;
40
            }
41
42
            $time = $this->stuTime->time();
43
44
            $this->setAnalyzedSignature($subspaceSystemData, $game);
45
            $this->setAnalyzeTime($time, $subspaceSystemData, $game);
46
            $this->setSignatures($wrapper, $time, $game);
47
        } else {
48
            $game->setTemplateVar('SYSTEMWARNING', true);
49
        }
50
51
        $game->setTemplateVar('USER', $game->getUser());
52
        $game->setTemplateVar('SPACECRAFT', $spacecraft);
53
        $game->setTemplateVar(
54
            'systemWrapper',
55
            $this->spacecraftSystemWrapperFactory->create($wrapper->get(), $systemType)
56
        );
57
    }
58
59
    private function setAnalyzedSignature(SubspaceSystemData $subspaceSystemData, GameControllerInterface $game): void
60
    {
61
        $flightSigId = $subspaceSystemData->getFlightSigId();
62
        if ($flightSigId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $flightSigId of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
63
            $flightSig = $this->flightSignatureRepository->find($flightSigId);
64
            $game->setTemplateVar('ANALYZED_SIGNATURE', $flightSig);
65
        }
66
    }
67
68
    private function setAnalyzeTime(int $time, SubspaceSystemData $subspaceSystemData, GameControllerInterface $game): void
69
    {
70
        $analyzeTime = $subspaceSystemData->getAnalyzeTime();
71
        if ($analyzeTime) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $analyzeTime of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
72
            $currentTime = $time;
73
            $maxTime = $analyzeTime + (10 * 60);
74
            $game->setTemplateVar('ANALYZE_TIME', $analyzeTime);
75
76
            if ($currentTime <= $maxTime) {
77
                $game->setTemplateVar('ANALYZE_TIME', $analyzeTime);
78
            }
79
        }
80
    }
81
82
    private function setSignatures(SpacecraftWrapperInterface $wrapper, int $time, GameControllerInterface $game): void
83
    {
84
        $spacecraft = $wrapper->get();
85
        $location = $spacecraft->getLocation();
86
87
        $layerId = $location->getLayer()?->getId();
88
        if ($layerId === null) {
89
            return;
90
        }
91
92
        $system = $spacecraft->getSystem();
93
        if ($system) {
94
            $cx = $system->getCx();
95
            $cy = $system->getCy();
96
        } else {
97
            $cx = $location->getCx();
98
            $cy = $location->getCy();
99
        }
100
101
        if ($cx && $cy && $layerId) {
102
103
            $timeThreshold = $time - (12 * 3600);
104
            $sensorRange = $wrapper->getLssSystemData()?->getSensorRange() ?? 0;
105
106
            $signatures = $this->flightSignatureRepository->getSignaturesInSensorRange(
107
                $game->getUser()->getId(),
108
                $cx,
109
                $cy,
110
                $layerId,
111
                $sensorRange,
112
                $timeThreshold
113
            );
114
            $game->setTemplateVar('SIGNATURES', $signatures);
115
        }
116
    }
117
}
118