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

handleSpacecraftDestruction()   B

Complexity

Conditions 7
Paths 40

Size

Total Lines 56
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 31
nc 40
nop 4
dl 0
loc 56
ccs 0
cts 31
cp 0
crap 56
rs 8.4906
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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