Passed
Push — dev ( 3fd410...aa33df )
by Janko
13:32
created

LaunchEscapePods   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Test Coverage

Coverage 4.08%

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 111
ccs 2
cts 49
cp 0.0408
rs 10
c 0
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A fly1() 0 3 1
A launch() 0 26 2
A fly2() 0 3 1
B returnToSafety() 0 34 6
A fly4() 0 3 1
A fly3() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib\Crew;
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 RuntimeException;
9
use Stu\Component\Map\DirectionEnum;
10
use Stu\Component\Spacecraft\SpacecraftRumpEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Spacecraft\SpacecraftRumpEnum 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\Spacecraft\Lib\Creation\SpacecraftFactoryInterface;
12
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperFactoryInterface;
13
use Stu\Orm\Entity\SpacecraftInterface;
14
use Stu\Orm\Repository\MapRepositoryInterface;
15
use Stu\Orm\Repository\SpacecraftRumpRepositoryInterface;
16
use Stu\Orm\Repository\SpacecraftRepositoryInterface;
17
use Stu\Orm\Repository\StarSystemMapRepositoryInterface;
18
use Stu\Orm\Repository\UserRepositoryInterface;
19
20
//TODO unit tests
21
final class LaunchEscapePods implements LaunchEscapePodsInterface
22
{
23 1
    public function __construct(
24
        private readonly SpacecraftRepositoryInterface $spacecraftRepository,
25
        private readonly UserRepositoryInterface $userRepository,
26
        private readonly SpacecraftRumpRepositoryInterface $spacecraftRumpRepository,
27
        private readonly StarSystemMapRepositoryInterface $starSystemMapRepository,
28
        private readonly MapRepositoryInterface $mapRepository,
29
        private readonly SpacecraftFactoryInterface $spacecraftFactory,
30
        private readonly SpacecraftWrapperFactoryInterface $spacecraftWrapperFactory
31 1
    ) {}
32
33
    #[Override]
34
    public function launch(SpacecraftInterface $spacecraft): ?SpacecraftInterface
35
    {
36
        $shipRump = $this->spacecraftRumpRepository->find($spacecraft->getUser()->getFactionId() + SpacecraftRumpEnum::SHIP_RUMP_BASE_ID_ESCAPE_PODS);
37
38
        // faction does not have escape pods
39
        if ($shipRump == null) {
40
            return null;
41
        }
42
43
        $pods = $this->spacecraftFactory->create($shipRump);
44
        $pods->setUser($this->userRepository->getFallbackUser());
45
        $pods->setRump($shipRump);
46
        $pods->setName(sprintf(_('Rettungskapseln von (%d)'), $spacecraft->getId()));
47
        $pods->setHuell(1);
48
        $pods->setMaxHuell(1);
49
50
        $pods->setLocation($spacecraft->getLocation());
51
52
        //return to save place
53
        $this->returnToSafety($pods, $spacecraft);
54
55
        $this->spacecraftRepository->save($pods);
56
        //$this->entityManager->flush(); //TODO really neccessary?
57
58
        return $pods;
59
    }
60
61
    private function returnToSafety(SpacecraftInterface $pods, SpacecraftInterface $spacecraft): void
62
    {
63
        $field = $pods->getLocation();
64
65
        if ($field->getFieldType()->getSpecialDamage() !== 0) {
66
            $flightDirection = $this->spacecraftWrapperFactory->wrapSpacecraft($spacecraft)->getComputerSystemDataMandatory()->getFlightDirection();
67
            while ($flightDirection === DirectionEnum::NON) {
68
                $flightDirection = DirectionEnum::from(random_int(1, 4));
69
            }
70
            $met = 'fly' . $flightDirection->value;
71
            $newXY = $this->$met($pods);
72
73
            if ($pods->getSystem() !== null) {
74
                $field = $this->starSystemMapRepository->getByCoordinates(
75
                    $pods->getSystem()->getId(),
76
                    $newXY[0],
77
                    $newXY[1]
78
                );
79
            } else {
80
                $layer = $pods->getLayer();
81
                if ($layer === null) {
82
                    throw new RuntimeException('this should not happen');
83
                }
84
85
                $field = $this->mapRepository->getByCoordinates(
86
                    $layer,
87
                    $newXY[0],
88
                    $newXY[1]
89
                );
90
            }
91
            if ($field === null) {
92
                throw new RuntimeException('this should not happen');
93
            }
94
            $pods->setLocation($field);
95
        }
96
    }
97
98
    //flee upwards
99
    /**
100
     * @return array<int>
101
     */
102
    private function fly2(SpacecraftInterface $pods): array
103
    {
104
        return [$pods->getPosX(), $pods->getPosY() - 1];
105
    }
106
107
    //flee downwards
108
    /**
109
     * @return array<int>
110
     */
111
    private function fly4(SpacecraftInterface $pods): array
112
    {
113
        return [$pods->getPosX(), $pods->getPosY() + 1];
114
    }
115
116
    //flee right
117
    /**
118
     * @return array<int>
119
     */
120
    private function fly3(SpacecraftInterface $pods): array
121
    {
122
        return [$pods->getPosX() - 1, $pods->getPosY()];
123
    }
124
125
    //flee left
126
    /**
127
     * @return array<int>
128
     */
129
    private function fly1(SpacecraftInterface $pods): array
130
    {
131
        return [$pods->getPosX() + 1, $pods->getPosY()];
132
    }
133
}
134