BussardCollectorHandler::handleSpacecraftTick()   C
last analyzed

Complexity

Conditions 12
Paths 51

Size

Total Lines 60
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 40
c 1
b 0
f 0
nc 51
nop 2
dl 0
loc 60
ccs 0
cts 39
cp 0
crap 156
rs 6.9666

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
namespace Stu\Module\Tick\Spacecraft\Handler;
4
5
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
6
use Stu\Lib\Information\InformationInterface;
7
use Stu\Lib\Transfer\Storage\StorageManagerInterface;
8
use Stu\Module\Control\StuRandom;
9
use Stu\Module\Ship\Lib\ShipWrapperInterface;
10
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
11
use Stu\Orm\Repository\LocationMiningRepositoryInterface;
12
13
class BussardCollectorHandler implements SpacecraftTickHandlerInterface
14
{
15 1
    public function __construct(
16
        private readonly LocationMiningRepositoryInterface $locationMiningRepository,
17
        private readonly StorageManagerInterface $storageManager,
18
        private readonly StuRandom $stuRandom
19 1
    ) {}
20
21
    #[\Override]
22
    public function handleSpacecraftTick(
23
        SpacecraftWrapperInterface $wrapper,
24
        InformationInterface $information
25
    ): void {
26
27
        if (!$wrapper instanceof ShipWrapperInterface) {
28
            return;
29
        }
30
31
        $ship = $wrapper->get();
32
        $bussard = $wrapper->getBussardCollectorSystemData();
33
        $miningqueue = $ship->getMiningQueue();
34
35
        if ($bussard === null) {
36
            return;
37
        }
38
39
        if ($miningqueue == null) {
40
            return;
41
        } else {
42
            $locationmining = $miningqueue->getLocationMining();
43
            $actualAmount = $locationmining->getActualAmount();
44
            $freeStorage = $ship->getMaxStorage() - $ship->getStorageSum();
45
            $module = $ship->getSpacecraftSystem(SpacecraftSystemTypeEnum::BUSSARD_COLLECTOR)->getModule();
46
            $gathercount = 0;
47
48
            if ($module !== null) {
49
                if ($module->getFactionId() == null) {
50
                    $gathercount =  (int) min(min(round($this->stuRandom->rand(95, 105)), $actualAmount), $freeStorage);
51
                } else {
52
                    $gathercount = (int) min(min(round($this->stuRandom->rand(190, 220)), $actualAmount), $freeStorage);
53
                }
54
            }
55
56
            $newAmount = $actualAmount - $gathercount;
57
            if ($gathercount > 0 && $locationmining->getDepletedAt() !== null) {
58
                $locationmining->setDepletedAt(null);
59
            }
60
            if ($newAmount == 0 && $actualAmount > 0) {
61
                $locationmining->setDepletedAt(time());
62
                $information->addInformationf(
63
                    'Es sind keine %s bei den Koordinaten %s|%s vorhanden!',
64
                    $locationmining->getCommodity()->getName(),
65
                    (string)$locationmining->getLocation()->getCx(),
66
                    (string)$locationmining->getLocation()->getCy()
67
                );
68
            }
69
            $locationmining->setActualAmount($newAmount);
70
71
            $this->locationMiningRepository->save($locationmining);
72
            if ($gathercount + $ship->getStorageSum() >= $ship->getMaxStorage()) {
73
                $information->addInformationf('Der Lagerraum des Schiffes wurde beim Sammeln von %s voll!', $locationmining->getCommodity()->getName());
74
            }
75
76
            if ($gathercount > 0) {
77
                $this->storageManager->upperStorage(
78
                    $ship,
79
                    $locationmining->getCommodity(),
80
                    $gathercount
81
                );
82
            }
83
        }
84
    }
85
}
86