Passed
Push — dev ( 593427...b1d6e5 )
by Janko
09:17
created

TransformToTrumfield   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 6.06%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 69
ccs 2
cts 33
cp 0.0606
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handleSpacecraftDestruction() 0 56 7
A __construct() 0 10 1
1
<?php
2
3
namespace Stu\Module\Spacecraft\Lib\Destruction\Handler;
4
5
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...
6
use Stu\Component\Spacecraft\SpacecraftStateEnum;
7
use Stu\Lib\Information\InformationInterface;
8
use Stu\Module\Spacecraft\Lib\Auxiliary\ShipShutdownInterface;
9
use Stu\Module\Spacecraft\Lib\Destruction\SpacecraftDestroyerInterface;
10
use Stu\Module\Spacecraft\Lib\Destruction\SpacecraftDestructionCauseEnum;
11
use Stu\Module\Spacecraft\Lib\SpacecraftStateChangerInterface;
12
use Stu\Module\Spacecraft\Lib\Torpedo\ClearTorpedoInterface;
13
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
14
use Stu\Orm\Entity\StationInterface;
15
use Stu\Orm\Repository\ConstructionProgressRepositoryInterface;
16
use Stu\Orm\Repository\SpacecraftSystemRepositoryInterface;
17
use Stu\Orm\Repository\StorageRepositoryInterface;
18
use Stu\Orm\Repository\TrumfieldRepositoryInterface;
19
use Stu\Orm\Repository\UserRepositoryInterface;
20
21
class TransformToTrumfield implements SpacecraftDestructionHandlerInterface
22
{
23 1
    public function __construct(
24
        private TrumfieldRepositoryInterface $trumfieldRepository,
25
        private SpacecraftSystemRepositoryInterface $shipSystemRepository,
26
        private StorageRepositoryInterface $storageRepository,
27
        private UserRepositoryInterface $userRepository,
28
        private ShipShutdownInterface $shipShutdown,
29
        private SpacecraftStateChangerInterface $spacecraftStateChanger,
30
        private ClearTorpedoInterface $clearTorpedo,
31
        private ConstructionProgressRepositoryInterface $constructionProgressRepository,
32 1
    ) {}
33
34
    #[Override]
35
    public function handleSpacecraftDestruction(
36
        ?SpacecraftDestroyerInterface $destroyer,
37
        SpacecraftWrapperInterface $destroyedSpacecraftWrapper,
38
        SpacecraftDestructionCauseEnum $cause,
39
        InformationInterface $informations
40
    ): void {
41
42
        $nobody = $this->userRepository->getFallbackUser();
43
        $spacecraft = $destroyedSpacecraftWrapper->get();
44
45
        $this->shipShutdown->shutdown($destroyedSpacecraftWrapper, true);
46
        $spacecraft->setIsDestroyed(true);
47
48
        // create trumfield entity
49
        $trumfield = $this->trumfieldRepository->prototype();
50
        $trumfield->setFormerRumpId($spacecraft->getRump()->getId());
51
        $trumfield->setHuell((int) ceil($spacecraft->getMaxHull() / 20));
52
        $trumfield->setLocation($spacecraft->getLocation());
53
        $this->trumfieldRepository->save($trumfield);
54
55
        foreach ($spacecraft->getBeamableStorage() as $storage) {
56
            $storage->setSpacecraft(null)
57
                ->setTrumfield($trumfield)
58
                ->setUser($nobody);
59
60
            $this->storageRepository->save($storage);
61
            $spacecraft->getStorage()->removeElement($storage);
62
        }
63
        foreach ($spacecraft->getStorage() as $storage) {
64
            $this->storageRepository->delete($storage);
65
        }
66
67
        $this->spacecraftStateChanger->changeShipState($destroyedSpacecraftWrapper, SpacecraftStateEnum::SHIP_STATE_DESTROYED);
68
69
        // delete ship systems
70
        foreach ($spacecraft->getSystems() as $system) {
71
            $this->shipSystemRepository->delete($system);
72
        }
73
        $spacecraft->getSystems()->clear();
74
75
        // delete torpedo storage
76
        $this->clearTorpedo->clearTorpedoStorage($destroyedSpacecraftWrapper);
77
78
        if (!$spacecraft instanceof StationInterface) {
79
            return;
80
        }
81
82
        $influenceArea = $spacecraft->getInfluenceArea();
83
        if ($influenceArea !== null) {
84
            $influenceArea->unsetStation();
85
        }
86
87
        $constructionProgress = $spacecraft->getConstructionProgress();
88
        if ($constructionProgress !== null) {
89
            $this->constructionProgressRepository->delete($constructionProgress);
90
        }
91
    }
92
}
93