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