Passed
Push — dev ( 058c01...b54028 )
by Janko
05:20
created

SpacecraftLeaver::evacuate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.5923

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
nc 3
nop 1
dl 0
loc 21
ccs 8
cts 12
cp 0.6667
crap 4.5923
rs 9.8666
c 1
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\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...
10
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
11
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
12
use Stu\Module\Spacecraft\Lib\Auxiliary\SpacecraftShutdownInterface;
13
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
14
use Stu\Orm\Entity\CrewAssignment;
15
use Stu\Orm\Entity\Spacecraft;
16
use Stu\Orm\Entity\User;
17
use Stu\Orm\Repository\CrewRepositoryInterface;
18
use Stu\Orm\Repository\CrewAssignmentRepositoryInterface;
19
use Stu\Orm\Repository\SpacecraftRumpRepositoryInterface;
20
21
//TODO unit tests
22
final class SpacecraftLeaver implements SpacecraftLeaverInterface
23
{
24 1
    public function __construct(
25
        private CrewAssignmentRepositoryInterface $shipCrewRepository,
26
        private SpacecraftRumpRepositoryInterface $spacecraftRumpRepository,
27
        private CrewRepositoryInterface $crewRepository,
28
        private LaunchEscapePodsInterface $launchEscapePods,
29
        private SpacecraftShutdownInterface $spacecraftShutdown,
30
        private PrivateMessageSenderInterface $privateMessageSender
31 1
    ) {}
32
33 1
    #[Override]
34
    public function evacuate(SpacecraftWrapperInterface $wrapper): string
35
    {
36 1
        $ship = $wrapper->get();
37 1
        $this->spacecraftShutdown->shutdown($wrapper);
38
39 1
        if ($ship->getRump()->isEscapePods()) {
40
            $this->letCrewDie($ship);
41
            return '-- Die Rettungskapseln wurden zerstört, die Crew ist daher verstorben!';
42
        }
43
44 1
        $podRump = $this->spacecraftRumpRepository->find($ship->getUser()->getFactionId() + SpacecraftRumpEnum::SHIP_RUMP_BASE_ID_ESCAPE_PODS);
45
46 1
        if ($podRump === null || $ship->getUser()->isNpc()) {
47
            $this->letCrewDie($ship);
48
            return '-- Keine Rettungskapseln vorhanden, die Crew ist daher verstorben!';
49
        }
50
51 1
        $this->escapeIntoPods($ship);
52
53 1
        return '-- Die Crew hat das Schiff in den Rettungskapseln verlassen!';
54
    }
55
56 1
    private function escapeIntoPods(Spacecraft $spacecraft): void
57
    {
58
        //create pods entity
59 1
        $pods = $this->launchEscapePods->launch($spacecraft);
60
61
        //transfer crew into pods
62
        //TODO not all...! depends on race config
63 1
        $crewList = $spacecraft->getCrewAssignments();
64 1
        foreach ($crewList as $shipCrew) {
65 1
            $shipCrew->setSpacecraft($pods);
66 1
            $shipCrew->setSlot(null);
67 1
            $this->shipCrewRepository->save($shipCrew);
68
        }
69
    }
70
71 1
    #[Override]
72
    public function dumpCrewman(CrewAssignment $crewAssignment, string $message): string
73
    {
74 1
        $spacecraft = $crewAssignment->getSpacecraft();
75 1
        if ($spacecraft === null) {
76
            throw new RuntimeException('can only dump crewman on ship');
77
        }
78
79
        //create pods entity
80 1
        $pods = $this->launchEscapePods->launch($spacecraft);
81
82 1
        if ($pods == null) {
83
            $crew = $crewAssignment->getCrew();
84
            $this->shipCrewRepository->delete($crewAssignment);
85
            $this->crewRepository->delete($crew);
86
87
            $survivalMessage = _('Der Crewman wurde exekutiert!');
88
        } else {
89
90 1
            $spacecraft->getCrewAssignments()->removeElement($crewAssignment);
91 1
            $crewAssignment->setSpacecraft($pods);
92 1
            $crewAssignment->setSlot(null);
93
94 1
            $this->shipCrewRepository->save($crewAssignment);
95 1
            $survivalMessage = _('Der Crewman hat das Schiff in einer Rettungskapsel verlassen!');
96
        }
97
98 1
        $this->sendPmToOwner(
99 1
            $spacecraft->getUser(),
100 1
            $crewAssignment->getUser(),
101 1
            $message,
102 1
            $survivalMessage
103 1
        );
104
105 1
        return $survivalMessage;
106
    }
107
108 1
    private function sendPmToOwner(
109
        User $sender,
110
        User $owner,
111
        string $message,
112
        string $survivalMessage
113
    ): void {
114 1
        $this->privateMessageSender->send(
115 1
            $sender->getId(),
116 1
            $owner->getId(),
117 1
            sprintf(
118 1
                "%s\n%s",
119 1
                $message,
120 1
                $survivalMessage
121 1
            ),
122 1
            PrivateMessageFolderTypeEnum::SPECIAL_SYSTEM
123 1
        );
124
    }
125
126
    private function letCrewDie(Spacecraft $spacecraft): void
127
    {
128
        foreach ($spacecraft->getCrewAssignments() as $shipCrew) {
129
            $this->crewRepository->delete($shipCrew->getCrew());
130
            $this->shipCrewRepository->delete($shipCrew);
131
        }
132
133
        $spacecraft->getCrewAssignments()->clear();
134
    }
135
}
136