AggregationSystemHandler::handleSpacecraftTick()   C
last analyzed

Complexity

Conditions 14
Paths 40

Size

Total Lines 65
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 210

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 45
c 1
b 0
f 0
nc 40
nop 2
dl 0
loc 65
ccs 0
cts 46
cp 0
crap 210
rs 6.2666

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\Faction\FactionEnum;
6
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
7
use Stu\Lib\Information\InformationInterface;
8
use Stu\Lib\Transfer\Storage\StorageManagerInterface;
9
use Stu\Module\Commodity\CommodityTypeConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Commodity\CommodityTypeConstants 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...
10
use Stu\Module\Commodity\Lib\CommodityCacheInterface;
11
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
12
use Stu\Module\Station\Lib\StationWrapperInterface;
13
use Stu\Orm\Repository\StorageRepositoryInterface;
14
15
class AggregationSystemHandler implements SpacecraftTickHandlerInterface
16
{
17 1
    public function __construct(
18
        private CommodityCacheInterface $commodityCache,
19
        private StorageRepositoryInterface $storageRepository,
20
        private StorageManagerInterface $storageManager
21 1
    ) {}
22
23
    #[\Override]
24
    public function handleSpacecraftTick(
25
        SpacecraftWrapperInterface $wrapper,
26
        InformationInterface $information
27
    ): void {
28
29
        if (!$wrapper instanceof StationWrapperInterface) {
30
            return;
31
        }
32
33
        $station = $wrapper->get();
34
        $aggsys = $wrapper->getAggregationSystemSystemData();
35
36
        if ($aggsys === null) {
37
            return;
38
        } else {
39
            $module = $station->getSpacecraftSystem(SpacecraftSystemTypeEnum::AGGREGATION_SYSTEM)->getModule();
40
            $producedAmount = 0;
41
            $usedAmount = 0;
42
            $usedCommodity = null;
43
            $producedCommodity = null;
44
45
46
            if ($module !== null) {
47
                $commodity = $aggsys->getCommodityId();
48
                $commodities = CommodityTypeConstants::COMMODITY_CONVERSIONS;
49
50
                if ($commodity > 0) {
51
                    foreach ($commodities as $entry) {
52
                        if ($entry[0] === $commodity) {
53
                            $producedCommodityId = $entry[1];
54
                            $producedCommodity = $this->commodityCache->get($producedCommodityId);
55
                            $usedCommodity = $this->commodityCache->get($entry[0]);
56
                            $usedAmount = $entry[2];
57
                            $producedAmount = $entry[3];
58
                            break;
59
                        }
60
                    }
61
62
                    if ($module->getFactionId() == FactionEnum::FACTION_FERENGI) {
63
                        $producedAmount *= 2;
64
                        $usedAmount *= 2;
65
                    }
66
                    $storage = $this->storageRepository->findOneBy([
67
                        'commodity' => $usedCommodity,
68
                        'spacecraft' => $station
69
                    ]);
70
                    if (!$storage && $usedCommodity) {
71
                        $information->addInformationf('Es ist kein %s vorhanden!', $usedCommodity->getName());
72
                    }
73
74
                    if ($storage && $producedCommodity && $usedCommodity) {
75
                        if ($storage->getAmount() >= $usedAmount) {
76
                            $this->storageManager->lowerStorage(
77
                                $station,
78
                                $usedCommodity,
79
                                $usedAmount
80
                            );
81
                            $this->storageManager->upperStorage(
82
                                $station,
83
                                $producedCommodity,
84
                                $producedAmount
85
                            );
86
                        } else {
87
                            $information->addInformationf('Nicht genügend %s vorhanden!', $usedCommodity->getName());
88
                        }
89
                    }
90
                }
91
            }
92
        }
93
    }
94
}
95