Passed
Push — master ( 5c1ca2...a370b2 )
by Nico
11:43
created

SubspaceSystemData::getHighlightedFlightSig()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 23
c 0
b 0
f 0
nc 7
nop 1
dl 0
loc 36
ccs 0
cts 23
cp 0
crap 72
rs 8.4444
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Spacecraft\System\Data;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override 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\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
9
use Stu\Module\Template\StatusBarFactoryInterface;
10
use Stu\Orm\Entity\FlightSignature;
11
use Stu\Orm\Entity\Spacecraft;
12
use Stu\Orm\Repository\SpacecraftSystemRepositoryInterface;
13
use Stu\Orm\Repository\FlightSignatureRepositoryInterface;
14
15
class SubspaceSystemData extends AbstractSystemData
16
{
17
    public ?int $spacecraftId = null;
18
    public ?int $analyzeTime = null;
19
    public ?int $flightSigId = null;
20
21
    public function __construct(
22
        SpacecraftSystemRepositoryInterface $shipSystemRepository,
23
        StatusBarFactoryInterface $statusBarFactory,
24
        private readonly FlightSignatureRepositoryInterface $flightSignatureRepository
25
    ) {
26
        parent::__construct($shipSystemRepository, $statusBarFactory);
27
    }
28
29
    #[Override]
30
    public function getSystemType(): SpacecraftSystemTypeEnum
31
    {
32
        return SpacecraftSystemTypeEnum::SUBSPACE_SCANNER;
33
    }
34
35
    public function getSpacecraftId(): ?int
36
    {
37
        return $this->spacecraftId ?? null;
38
    }
39
40
    public function setSpacecraftId(?int $spacecraftId): SubspaceSystemData
41
    {
42
        $this->spacecraftId = $spacecraftId;
43
        return $this;
44
    }
45
46
    public function getAnalyzeTime(): ?int
47
    {
48
        return $this->analyzeTime ?? null;
49
    }
50
51
    public function setAnalyzeTime(?int $analyzeTime): SubspaceSystemData
52
    {
53
        $this->analyzeTime = $analyzeTime;
54
        return $this;
55
    }
56
57
    public function getFlightSigId(): ?int
58
    {
59
        return $this->flightSigId ?? null;
60
    }
61
62
    public function setFlightSigId(?int $flightSigId): SubspaceSystemData
63
    {
64
        $this->flightSigId = $flightSigId;
65
        return $this;
66
    }
67
68
    public function getHighlightedFlightSig(Spacecraft $spacecraft): ?FlightSignature
69
    {
70
        $isSubspaceScannerActive = $spacecraft->getSystemState(SpacecraftSystemTypeEnum::SUBSPACE_SCANNER);
71
        if (!$isSubspaceScannerActive) {
72
            return null;
73
        }
74
75
        $isMatrixScannerHealthy = $spacecraft->isSystemHealthy(SpacecraftSystemTypeEnum::MATRIX_SCANNER);
76
        if (!$isMatrixScannerHealthy) {
77
            return null;
78
        }
79
80
        $subspaceSystem = $spacecraft->getSpacecraftSystem(SpacecraftSystemTypeEnum::SUBSPACE_SCANNER);
81
        if ($subspaceSystem->getData() === null) {
82
            return null;
83
        }
84
85
        $analyzeTime = $this->getAnalyzeTime();
86
        if ($analyzeTime === null) {
87
            return null;
88
        }
89
90
        $currentTime = time();
91
        $minTime = $analyzeTime + (3 * 60);
92
        $maxTime = $analyzeTime + (10 * 60);
93
94
        if (!($currentTime >= $minTime && $currentTime <= $maxTime)) {
95
            return null;
96
        }
97
98
        $flightSigId = $this->getFlightSigId();
99
        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...
100
            $flightSig = $this->flightSignatureRepository->find($flightSigId);
101
            return $flightSig;
102
        } else {
103
            return null;
104
        }
105
    }
106
}
107