Test Failed
Pull Request — dev (#1952)
by Nico
15:17
created

FlightSignatureCreator::getWarpSignature()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 10
nc 3
nop 1
dl 0
loc 15
ccs 11
cts 12
cp 0.9167
crap 5.0144
rs 9.6111
c 1
b 1
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\Map\DirectionEnum;
10
use Stu\Orm\Entity\FlightSignature;
11
use Stu\Orm\Entity\Location;
12
use Stu\Orm\Entity\Map;
13
use Stu\Orm\Entity\Spacecraft;
14
use Stu\Orm\Repository\FlightSignatureRepositoryInterface;
15
16
/**
17
 * Creates flight signatures for ship movements
18
 */
19
final class FlightSignatureCreator implements FlightSignatureCreatorInterface
20
{
21
    public function __construct(private FlightSignatureRepositoryInterface $flightSignatureRepository) {}
22
23
    #[Override]
24
    public function createSignatures(
25
        Spacecraft $spacecraft,
26 11
        DirectionEnum $direction,
27
        Location $currentLocation,
28
        Location $nextLocation
29
    ): void {
30
        if ($currentLocation instanceof Map !== $nextLocation instanceof Map) {
31 11
            throw new InvalidArgumentException('wayopints have different type');
32
        }
33 13
34
        $fromSignature = $this->createSignature($spacecraft);
35
        $fromSignature->setLocation($currentLocation);
36
37
        $toSignature = $this->createSignature($spacecraft);
38
        $toSignature->setLocation($nextLocation);
39
40 13
        $this->create(
41 2
            $direction,
42
            $fromSignature,
43
            $toSignature
44 11
        );
45 11
    }
46
47 11
    private function create(
48 11
        DirectionEnum $direction,
49
        FlightSignature $fromSignature,
50 11
        FlightSignature $toSignature
51 11
    ): void {
52 11
53 11
        $fromSignature->setToDirection($direction);
54 11
        $toSignature->setFromDirection($direction->getOpposite());
55
56
        $this->flightSignatureRepository->save($fromSignature);
57 11
        $this->flightSignatureRepository->save($toSignature);
58
    }
59
60
    private function createSignature(Spacecraft $spacecraft): FlightSignature
61
    {
62
        $signature = $this->flightSignatureRepository->prototype();
63 11
        $signature->setUserId($spacecraft->getUser()->getId());
64 11
        $signature->setShipId($spacecraft->getId());
65
        $signature->setSpacecraftName($spacecraft->getName());
66 11
        $signature->setRump($spacecraft->getRump());
67 11
        $signature->setIsCloaked($spacecraft->isCloaked());
68
        $signature->setTime(time());
69
70 11
        return $signature;
71
    }
72
}
73