AggregationSystemHandler   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 4.17%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 73
ccs 2
cts 48
cp 0.0417
rs 10
wmc 15

2 Methods

Rating   Name   Duplication   Size   Complexity  
C handleSpacecraftTick() 0 65 14
A __construct() 0 5 1
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