Passed
Push — dev ( 831a1f...f61be0 )
by Janko
07:24
created

RubColonyBehaviour::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 9
dl 0
loc 12
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Stu\Lib\Pirate\Behaviour;
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\Game\ModuleEnum;
7
use Stu\Component\Spacecraft\System\SpacecraftSystemManagerInterface;
8
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
9
use Stu\Lib\Information\InformationWrapper;
10
use Stu\Lib\Map\DistanceCalculationInterface;
11
use Stu\Lib\Pirate\Component\PirateNavigationInterface;
12
use Stu\Lib\Pirate\PirateBehaviourEnum;
13
use Stu\Lib\Pirate\PirateReactionInterface;
14
use Stu\Lib\Pirate\PirateReactionMetadata;
15
use Stu\Lib\Transfer\CommodityTransferInterface;
16
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
17
use Stu\Module\Colony\View\ShowColony\ShowColony;
18
use Stu\Module\Control\StuRandom;
19
use Stu\Module\Logging\LoggerUtilFactoryInterface;
20
use Stu\Module\Logging\PirateLoggerInterface;
21
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
22
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
23
use Stu\Module\Ship\Lib\FleetWrapperInterface;
24
use Stu\Orm\Entity\Colony;
25
use Stu\Orm\Entity\Spacecraft;
26
use Stu\Orm\Entity\Storage;
27
use Stu\Orm\Repository\ColonyRepositoryInterface;
28
29
class RubColonyBehaviour implements PirateBehaviourInterface
30
{
31
    private PirateLoggerInterface $logger;
32
33 1
    public function __construct(
34
        private ColonyRepositoryInterface $colonyRepository,
35
        private DistanceCalculationInterface $distanceCalculation,
36
        private PirateNavigationInterface $pirateNavigation,
37
        private ColonyLibFactoryInterface $colonyLibFactory,
38
        private SpacecraftSystemManagerInterface $spacecraftSystemManager,
39
        private CommodityTransferInterface $commodityTransfer,
40
        private PrivateMessageSenderInterface $privateMessageSender,
41
        private StuRandom $stuRandom,
42
        LoggerUtilFactoryInterface $loggerUtilFactory
43
    ) {
44 1
        $this->logger = $loggerUtilFactory->getPirateLogger();
45
    }
46
47
    #[Override]
48
    public function action(
49
        FleetWrapperInterface $fleet,
50
        PirateReactionInterface $pirateReaction,
51
        PirateReactionMetadata $reactionMetadata,
52
        ?Spacecraft $triggerSpacecraft
53
    ): ?PirateBehaviourEnum {
54
55
        $leadWrapper = $fleet->getLeadWrapper();
56
        $leadShip = $leadWrapper->get();
57
58
        $targets = $this->colonyRepository->getPirateTargets($leadWrapper);
59
        if ($targets === []) {
60
            $this->logger->log('    no colony targets in reach');
61
            return PirateBehaviourEnum::FLY;
62
        }
63
64
        usort($targets, fn(Colony $a, Colony $b): int =>
65
        $this->distanceCalculation->spacecraftToColonyDistance($leadShip, $a) - $this->distanceCalculation->spacecraftToColonyDistance($leadShip, $b));
66
67
        $closestColony = current($targets);
68
69
        if ($this->pirateNavigation->navigateToTarget($fleet, $closestColony->getStarsystemMap())) {
70
            $this->logger->logf(
71
                '    reached colonyId %d at %s',
72
                $closestColony->getId(),
73
                $closestColony->getSectorString()
74
            );
75
            $this->rubColony($fleet, $closestColony);
76
        }
77
78
        return null;
79
    }
80
81
    private function rubColony(FleetWrapperInterface $fleetWrapper, Colony $colony): void
82
    {
83
        if ($this->colonyLibFactory->createColonyShieldingManager($colony)->isShieldingEnabled()) {
84
            $this->logger->log('    colony has shield on');
85
            return;
86
        }
87
88
        $pirateUser = $fleetWrapper->get()->getUser();
89
90
        $filteredColonyStorage = array_filter(
91
            $colony->getStorage()->toArray(),
92
            fn(Storage $storage): bool => $storage->getCommodity()->isBeamable($colony->getUser(), $pirateUser)
93
        );
94
95
        $allInformations = new InformationWrapper();
96
97
        foreach ($fleetWrapper->getShipWrappers() as $wrapper) {
98
99
            if ($filteredColonyStorage === []) {
100
                $this->logger->log('    no beamable storage on colony');
101
                return;
102
            }
103
104
            $this->spacecraftSystemManager->deactivate($wrapper, SpacecraftSystemTypeEnum::SHIELDS, true);
105
106
            $ship = $wrapper->get();
107
            $randomCommodityId = array_rand($filteredColonyStorage);
108
109
            $informations = new InformationWrapper();
110
111
            $hasStolen = $this->commodityTransfer->transferCommodity(
112
                $randomCommodityId,
113
                $this->stuRandom->rand(1, $wrapper->get()->getMaxStorage()),
114
                $wrapper,
115
                $colony,
116
                $wrapper->get(),
117
                $informations
118
            );
119
120
            if ($hasStolen) {
121
                $informations->addInformationArray([sprintf(
122
                    _('Die %s hat folgende Waren von der Kolonie %s gestohlen'),
123
                    $ship->getName(),
124
                    $colony->getName()
125
                )], true);
126
127
                $this->spacecraftSystemManager->activate($wrapper, SpacecraftSystemTypeEnum::SHIELDS, true);
128
            }
129
130
            $allInformations->addInformationWrapper($informations);
131
        }
132
133
        $this->privateMessageSender->send(
134
            $pirateUser->getId(),
135
            $colony->getUser()->getId(),
136
            $allInformations,
137
            PrivateMessageFolderTypeEnum::SPECIAL_TRADE,
138
            sprintf(
139
                '%s?%s=1&id=%d',
140
                ModuleEnum::COLONY->getPhpPage(),
141
                ShowColony::VIEW_IDENTIFIER,
142
                $colony->getId()
143
            )
144
        );
145
    }
146
}
147