Passed
Push — master ( 6d8b29...d7e053 )
by Janko
27:40
created

LaunchEscapePods::fly4()   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\Ship\Lib\Crew;
6
7
use Doctrine\ORM\EntityManagerInterface;
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 RuntimeException;
10
use Stu\Component\Ship\ShipRumpEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Ship\ShipRumpEnum 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\Orm\Entity\ShipInterface;
12
use Stu\Orm\Repository\MapRepositoryInterface;
13
use Stu\Orm\Repository\ShipRepositoryInterface;
14
use Stu\Orm\Repository\ShipRumpRepositoryInterface;
15
use Stu\Orm\Repository\StarSystemMapRepositoryInterface;
16
use Stu\Orm\Repository\UserRepositoryInterface;
17
18
//TODO unit tests
19
final class LaunchEscapePods implements LaunchEscapePodsInterface
20
{
21 1
    public function __construct(private ShipRepositoryInterface $shipRepository, private UserRepositoryInterface $userRepository, private ShipRumpRepositoryInterface $shipRumpRepository, private StarSystemMapRepositoryInterface $starSystemMapRepository, private MapRepositoryInterface $mapRepository, private EntityManagerInterface $entityManager) {}
22
23
    #[Override]
24
    public function launch(ShipInterface $ship): ?ShipInterface
25
    {
26
        $shipRump = $this->shipRumpRepository->find($ship->getUser()->getFactionId() + ShipRumpEnum::SHIP_RUMP_BASE_ID_ESCAPE_PODS);
27
28
        // faction does not have escape pods
29
        if ($shipRump == null) {
30
            return null;
31
        }
32
33
        $pods = $this->shipRepository->prototype();
34
        $pods->setUser($this->userRepository->getFallbackUser());
35
        $pods->setRump($shipRump);
36
        $pods->setName(sprintf(_('Rettungskapseln von (%d)'), $ship->getId()));
37
        $pods->setHuell(1);
38
        $pods->setMaxHuell(1);
39
        $pods->setAlertStateGreen();
40
41
        $pods->setLocation($ship->getLocation());
42
43
        //return to save place
44
        $this->returnToSafety($pods, $ship);
45
46
        $this->shipRepository->save($pods);
47
        $this->entityManager->flush();
48
        return $pods;
49
    }
50
51
    private function returnToSafety(ShipInterface $pods, ShipInterface $ship): void
52
    {
53
        $field = $pods->getLocation();
54
55
        if ($field->getFieldType()->getSpecialDamage() !== 0) {
56
            $met = 'fly' . $ship->getFlightDirection();
57
            $newXY = $this->$met($pods);
58
59
            if ($pods->getSystem() !== null) {
60
                $field = $this->starSystemMapRepository->getByCoordinates(
61
                    $pods->getSystem()->getId(),
62
                    $newXY[0],
63
                    $newXY[1]
64
                );
65
            } else {
66
                $layer = $pods->getLayer();
67
                if ($layer === null) {
68
                    throw new RuntimeException('this should not happen');
69
                }
70
71
                $field = $this->mapRepository->getByCoordinates(
72
                    $layer,
73
                    $newXY[0],
74
                    $newXY[1]
75
                );
76
            }
77
            if ($field === null) {
78
                throw new RuntimeException('this should not happen');
79
            }
80
            $pods->setLocation($field);
81
        }
82
    }
83
84
    //flee upwards
85
    /**
86
     * @return array<int>
87
     */
88
    private function fly2(ShipInterface $pods): array
89
    {
90
        return [$pods->getPosX(), $pods->getPosY() - 1];
91
    }
92
93
    //flee downwards
94
    /**
95
     * @return array<int>
96
     */
97
    private function fly4(ShipInterface $pods): array
98
    {
99
        return [$pods->getPosX(), $pods->getPosY() + 1];
100
    }
101
102
    //flee right
103
    /**
104
     * @return array<int>
105
     */
106
    private function fly3(ShipInterface $pods): array
107
    {
108
        return [$pods->getPosX() - 1, $pods->getPosY()];
109
    }
110
111
    //flee left
112
    /**
113
     * @return array<int>
114
     */
115
    private function fly1(ShipInterface $pods): array
116
    {
117
        return [$pods->getPosX() + 1, $pods->getPosY()];
118
    }
119
}
120