FinishTholianWebs::handleFleetConstellations()   C
last analyzed

Complexity

Conditions 12
Paths 100

Size

Total Lines 65
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
cc 12
eloc 31
nc 100
nop 1
dl 0
loc 65
ccs 0
cts 32
cp 0
crap 156
rs 6.9666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
declare(strict_types=1);
4
5
namespace Stu\Module\Tick\Process;
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\System\SpacecraftSystemManagerInterface;
10
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
11
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
12
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
13
use Stu\Module\Ship\Lib\Fleet\LeaveFleetInterface;
14
use Stu\Module\Ship\Lib\TholianWebUtilInterface;
15
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperFactoryInterface;
16
use Stu\Orm\Entity\Ship;
17
use Stu\Orm\Entity\Spacecraft;
18
use Stu\Orm\Entity\TholianWeb;
19
use Stu\Orm\Entity\User;
20
use Stu\Orm\Repository\TholianWebRepositoryInterface;
21
22
final class FinishTholianWebs implements ProcessTickHandlerInterface
23
{
24 1
    public function __construct(
25
        private TholianWebRepositoryInterface $tholianWebRepository,
26
        private TholianWebUtilInterface $tholianWebUtil,
27
        private SpacecraftWrapperFactoryInterface $spacecraftWrapperFactory,
28
        private LeaveFleetInterface $leaveFleet,
29
        private SpacecraftSystemManagerInterface $spacecraftSystemManager,
30
        private PrivateMessageSenderInterface $privateMessageSender
31 1
    ) {}
32
33
    #[Override]
34
    public function work(): void
35
    {
36
        foreach ($this->tholianWebRepository->getFinishedWebs() as $web) {
37
            //remove captured ships from fleet
38
            $this->handleFleetConstellations($web);
39
40
            //cancel tractor beams
41
            $this->cancelTractorBeams($web);
42
43
            //free helper
44
            $this->tholianWebUtil->resetWebHelpers($web, $this->spacecraftWrapperFactory, true);
45
46
            //set finished
47
            $web->setFinishedTime(null);
48
            $this->tholianWebRepository->save($web);
49
        }
50
    }
51
52
    private function handleFleetConstellations(TholianWeb $web): void
53
    {
54
        /** @var array<int, array<Ship>> */
55
        $fleets = [];
56
57
        //determine constellations
58
        foreach ($web->getCapturedSpacecrafts() as $spacecraft) {
59
60
            if (!$spacecraft instanceof Ship) {
61
                continue;
62
            }
63
64
            $fleet = $spacecraft->getFleet();
65
            if ($fleet === null) {
66
                continue;
67
            }
68
69
            if (!array_key_exists($fleet->getId(), $fleets)) {
70
                $fleets[$fleet->getId()] = [];
71
            }
72
73
            $fleets[$fleet->getId()][] = $spacecraft;
74
        }
75
76
        $pms = [];
77
78
        //modify constellation
79
        foreach ($fleets as $shiplist) {
80
81
            $current = current($shiplist);
82
            $fleet = $current ? $current->getFleet() : null;
83
84
            if ($fleet === null) {
85
                throw new RuntimeException('this should not happen');
86
            }
87
88
            //all ships of fleet in web, nothing to do
89
            if ($fleet->getShipCount() === count($shiplist)) {
90
                continue;
91
            }
92
93
            $userId = $fleet->getUser()->getId();
94
            $fleetName = $fleet->getName();
95
96
            /**
97
             * @var Ship[] $shiplist
98
             */
99
            foreach ($shiplist as $ship) {
100
                $this->leaveFleet->leaveFleet($ship);
101
102
                if (!array_key_exists($userId, $pms)) {
103
                    $pms[$userId] = sprintf(_('Das Energienetz in Sektor %s wurde fertiggestellt') . "\n", $ship->getSectorString());
104
                }
105
106
                $pms[$userId] .= sprintf('Die %s hat die Flotte %s verlassen' . "\n", $ship->getName(), $fleetName);
107
            }
108
        }
109
110
        //notify target owners
111
        foreach ($pms as $recipientId => $pm) {
112
            $this->privateMessageSender->send(
113
                $web->getUser()->getId(),
114
                $recipientId,
115
                $pm,
116
                PrivateMessageFolderTypeEnum::SPECIAL_SHIP
117
            );
118
        }
119
    }
120
121
    private function cancelTractorBeams(TholianWeb $web): void
122
    {
123
        $webOwner = $web->getUser();
124
125
        foreach ($web->getCapturedSpacecrafts() as $spacecraft) {
126
            if ($spacecraft->isTractoring()) {
127
                $this->cancelTractorBeam($webOwner, $spacecraft);
128
            }
129
130
            $tractoringSpacecraft = $spacecraft instanceof Ship ? $spacecraft->getTractoringSpacecraft() : null;
131
            if ($tractoringSpacecraft !== null) {
132
                $this->cancelTractorBeam($webOwner, $tractoringSpacecraft);
133
            }
134
        }
135
    }
136
137
    private function cancelTractorBeam(User $webOwner, Spacecraft $spacecraft): void
138
    {
139
        $this->privateMessageSender->send(
140
            $webOwner->getId(),
141
            $spacecraft->getUser()->getId(),
142
            sprintf(
143
                'Der Traktorstrahl der %s auf die %s wurde in Sektor %s durch ein Energienetz unterbrochen',
144
                $spacecraft->getName(),
145
                $spacecraft->getTractoredShip(),
146
                $spacecraft->getSectorString()
147
            ),
148
            PrivateMessageFolderTypeEnum::SPECIAL_SHIP
149
        );
150
151
        $this->spacecraftSystemManager->deactivate(
152
            $this->spacecraftWrapperFactory->wrapSpacecraft($spacecraft),
153
            SpacecraftSystemTypeEnum::TRACTOR_BEAM,
154
            true
155
        ); //forced deactivation
156
    }
157
}
158