Passed
Pull Request — master (#1961)
by Nico
16:28 queued 07:39
created

ShipLeaver::evacuate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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