Passed
Push — master ( 66afc8...ff3c83 )
by Janko
09:01
created

LaunchEscapePods::fly1()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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\Spacecraft;
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(Spacecraft $spacecraft): ?Spacecraft
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->setMaxHuell(1);
48
        $pods->getCondition()->setHull(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
57
        return $pods;
58
    }
59
60
    private function returnToSafety(Spacecraft $pods, Spacecraft $spacecraft): void
61
    {
62
        $field = $pods->getLocation();
63
64
        if ($field->getFieldType()->getSpecialDamage() !== 0) {
65
            $flightDirection = $this->spacecraftWrapperFactory->wrapSpacecraft($spacecraft)->getComputerSystemDataMandatory()->getFlightDirection();
66
            while ($flightDirection === DirectionEnum::NON) {
67
                $flightDirection = DirectionEnum::from(random_int(1, 4));
68
            }
69
70
            $newXY = match ($flightDirection) {
71
                DirectionEnum::LEFT => $this->fly1($pods),
72
                DirectionEnum::BOTTOM => $this->fly2($pods),
73
                DirectionEnum::RIGHT => $this->fly3($pods),
74
                DirectionEnum::TOP => $this->fly4($pods),
75
            };
76
77
            if ($pods->getSystem() !== null) {
78
                $field = $this->starSystemMapRepository->getByCoordinates(
79
                    $pods->getSystem()->getId(),
80
                    $newXY[0],
81
                    $newXY[1]
82
                );
83
            } else {
84
                $layer = $pods->getLayer();
85
                if ($layer === null) {
86
                    throw new RuntimeException('this should not happen');
87
                }
88
89
                $field = $this->mapRepository->getByCoordinates(
90
                    $layer,
91
                    $newXY[0],
92
                    $newXY[1]
93
                );
94
            }
95
            if ($field === null) {
96
                throw new RuntimeException('this should not happen');
97
            }
98
            $pods->setLocation($field);
99
        }
100
    }
101
102
    //flee upwards
103
    /**
104
     * @return array<int>
105
     */
106
    private function fly2(Spacecraft $pods): array
107
    {
108
        return [$pods->getPosX(), $pods->getPosY() - 1];
109
    }
110
111
    //flee downwards
112
    /**
113
     * @return array<int>
114
     */
115
    private function fly4(Spacecraft $pods): array
116
    {
117
        return [$pods->getPosX(), $pods->getPosY() + 1];
118
    }
119
120
    //flee right
121
    /**
122
     * @return array<int>
123
     */
124
    private function fly3(Spacecraft $pods): array
125
    {
126
        return [$pods->getPosX() - 1, $pods->getPosY()];
127
    }
128
129
    //flee left
130
    /**
131
     * @return array<int>
132
     */
133
    private function fly1(Spacecraft $pods): array
134
    {
135
        return [$pods->getPosX() + 1, $pods->getPosY()];
136
    }
137
}
138