Passed
Push — master ( c961ec...bd397e )
by Nico
26:15 queued 16:56
created

FlightSignatureCreator::getWarpSignature()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.3073

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 3
nop 1
dl 0
loc 16
ccs 10
cts 13
cp 0.7692
crap 5.3073
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib\Movement\Component;
6
7
use InvalidArgumentException;
8
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...
9
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
10
use Stu\Component\Map\DirectionEnum;
11
use Stu\Module\Control\StuTime;
12
use Stu\Module\Ship\Lib\ShipLoaderInterface;
13
use Stu\Orm\Entity\FlightSignature;
14
use Stu\Orm\Entity\Location;
15
use Stu\Orm\Entity\Map;
16
use Stu\Orm\Entity\Spacecraft;
17
use Stu\Orm\Entity\SpacecraftRump;
18
use Stu\Orm\Repository\FlightSignatureRepositoryInterface;
19
use Stu\Orm\Repository\SpacecraftRumpRepositoryInterface;
20
21
/**
22
 * Creates flight signatures for ship movements
23
 */
24
final class FlightSignatureCreator implements FlightSignatureCreatorInterface
25
{
26 11
    public function __construct(
27
        private FlightSignatureRepositoryInterface $flightSignatureRepository,
28
        private StuTime $stuTime,
29
        private ShipLoaderInterface $shipLoader,
30
        private SpacecraftRumpRepositoryInterface $spacecraftRumpRepository
31 11
    ) {}
32
33 10
    #[Override]
34
    public function createSignatures(
35
        Spacecraft $spacecraft,
36
        DirectionEnum $direction,
37
        Location $currentLocation,
38
        Location $nextLocation
39
    ): void {
40 10
        if ($currentLocation instanceof Map !== $nextLocation instanceof Map) {
41 2
            throw new InvalidArgumentException('wayopints have different type');
42
        }
43
44 8
        $fromSignature = $this->createSignature($spacecraft);
45 8
        $fromSignature->setLocation($currentLocation);
46
47 8
        $toSignature = $this->createSignature($spacecraft);
48 8
        $toSignature->setLocation($nextLocation);
49
50 8
        $this->create(
51 8
            $direction,
52 8
            $fromSignature,
53 8
            $toSignature
54 8
        );
55
    }
56
57 8
    private function create(
58
        DirectionEnum $direction,
59
        FlightSignature $fromSignature,
60
        FlightSignature $toSignature
61
    ): void {
62
63 8
        $fromSignature->setToDirection($direction);
64 8
        $toSignature->setFromDirection($direction->getOpposite());
65
66 8
        $this->flightSignatureRepository->save($fromSignature);
67 8
        $this->flightSignatureRepository->save($toSignature);
68
    }
69
70 8
    private function createSignature(Spacecraft $spacecraft): FlightSignature
71
    {
72 8
        $signature = $this->flightSignatureRepository->prototype();
73 8
        $signature->setUserId($spacecraft->getUser()->getId());
74 8
        $signature->setShipId($spacecraft->getId());
75 8
        $signature->setSpacecraftName($spacecraft->getName());
76 8
        $signature->setRump($this->getWarpSignature($spacecraft));
77 8
        $signature->setIsCloaked($spacecraft->isCloaked());
78 8
        $signature->setTime(time());
79
80 8
        return $signature;
81
    }
82
83 8
    private function getWarpSignature(Spacecraft $spacecraft): SpacecraftRump
84
    {
85 8
        $rump = $spacecraft->getRump();
86 8
        $wrapper = $this->shipLoader->getWrapperByIdAndUser(
87 8
            $spacecraft->getId(),
88 8
            $spacecraft->getUser()->getId()
89 8
        );
90 8
        $warpsystem = $wrapper->getWarpDriveSystemData();
91 8
        $system =  $spacecraft->getSpacecraftSystem(SpacecraftSystemTypeEnum::WARPDRIVE);
92 8
        if ($system->getMode()->isActivated() && $warpsystem) {
93
            $signaturetime = $warpsystem->getWarpSignatureTimer();
94
            if ($signaturetime + 300 >= $this->stuTime->time() && $this->spacecraftRumpRepository->find($warpsystem->getWarpSignature())) {
95
                $rump = $this->spacecraftRumpRepository->find($warpsystem->getWarpSignature());
96
            }
97
        }
98 8
        return $rump;
99
    }
100
}