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