Passed
Push — master ( bf860f...1dd8f7 )
by Nico
57:55 queued 29:36
created

TransformToTrumfield   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 8.69%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 47
ccs 2
cts 23
cp 0.0869
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handleShipDestruction() 0 35 2
A __construct() 0 8 1
1
<?php
2
3
namespace Stu\Module\Ship\Lib\Destruction\Handler;
4
5
use RuntimeException;
6
use Stu\Component\Ship\ShipRumpEnum;
7
use Stu\Component\Ship\ShipStateEnum;
8
use Stu\Component\Ship\SpacecraftTypeEnum;
9
use Stu\Lib\Information\InformationInterface;
10
use Stu\Module\Ship\Lib\Auxiliary\ShipShutdownInterface;
11
use Stu\Module\Ship\Lib\Destruction\ShipDestroyerInterface;
12
use Stu\Module\Ship\Lib\Destruction\ShipDestructionCauseEnum;
13
use Stu\Module\Ship\Lib\ShipStateChangerInterface;
14
use Stu\Module\Ship\Lib\ShipWrapperInterface;
15
use Stu\Module\Ship\Lib\Torpedo\ClearTorpedoInterface;
16
use Stu\Orm\Repository\ShipRumpRepositoryInterface;
17
use Stu\Orm\Repository\ShipSystemRepositoryInterface;
18
use Stu\Orm\Repository\UserRepositoryInterface;
19
20
class TransformToTrumfield implements ShipDestructionHandlerInterface
21
{
22 1
    public function __construct(
23
        private ShipShutdownInterface $shipShutdown,
24
        private ShipRumpRepositoryInterface $shipRumpRepository,
25
        private ShipSystemRepositoryInterface $shipSystemRepository,
26
        private UserRepositoryInterface $userRepository,
27
        private ShipStateChangerInterface $shipStateChanger,
28
        private ClearTorpedoInterface $clearTorpedo
29
    ) {
30 1
    }
31
32
    public function handleShipDestruction(
33
        ?ShipDestroyerInterface $destroyer,
34
        ShipWrapperInterface $destroyedShipWrapper,
35
        ShipDestructionCauseEnum $cause,
36
        InformationInterface $informations
37
    ): void {
38
39
        $ship = $destroyedShipWrapper->get();
40
41
        $this->shipShutdown->shutdown($destroyedShipWrapper, true);
42
43
        $trumfieldRump = $this->shipRumpRepository->find(ShipRumpEnum::SHIP_CATEGORY_TRUMFIELD);
44
        if ($trumfieldRump === null) {
45
            throw new RuntimeException('trumfield rump missing');
46
        }
47
48
        $ship->setFormerRumpId($ship->getRump()->getId());
49
        $ship->setRump($trumfieldRump);
50
        $ship->setHuell((int) ceil($ship->getMaxHull() / 20));
51
        $ship->setUser($this->userRepository->getFallbackUser());
52
        $ship->setBuildplan(null);
53
        $ship->setSpacecraftType(SpacecraftTypeEnum::SPACECRAFT_TYPE_OTHER);
54
        $ship->setShield(0);
55
        $ship->setAlertStateGreen();
56
        $ship->setInfluenceArea(null);
57
        $ship->setName(_('Trümmer'));
58
        $ship->setIsDestroyed(true);
59
        $this->shipStateChanger->changeShipState($destroyedShipWrapper, ShipStateEnum::SHIP_STATE_DESTROYED);
60
61
        // delete ship systems
62
        $this->shipSystemRepository->truncateByShip($ship->getId());
63
        $ship->getSystems()->clear();
64
65
        // delete torpedo storage
66
        $this->clearTorpedo->clearTorpedoStorage($destroyedShipWrapper);
67
    }
68
}
69