ReactorUtil::loadReactor()   C
last analyzed

Complexity

Conditions 13
Paths 163

Size

Total Lines 95
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
cc 13
eloc 55
nc 163
nop 4
dl 0
loc 95
ccs 0
cts 57
cp 0
crap 182
rs 6.0916
c 0
b 0
f 0

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
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib;
6
7
use Doctrine\Common\Collections\Collection;
8
use RuntimeException;
9
use Stu\Lib\Transfer\Storage\StorageManagerInterface;
10
use Stu\Lib\SpacecraftManagement\Provider\ManagerProviderInterface;
11
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
12
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
13
use Stu\Orm\Entity\Spacecraft;
14
15
//TODO create unit test
16
final class ReactorUtil implements ReactorUtilInterface
17
{
18 1
    public function __construct(
19
        private StorageManagerInterface $storageManager,
20
        private PrivateMessageSenderInterface $privateMessageSender
21 1
    ) {}
22
23
    #[\Override]
24
    public function storageContainsNeededCommodities(Collection $storages, ReactorWrapperInterface $reactor): bool
25
    {
26
        foreach ($reactor->get()->getLoadCost() as $commodityId => $loadCost) {
27
            $storage = $storages->get($commodityId);
28
29
            if ($storage === null) {
30
                return false;
31
            }
32
            if ($storage->getAmount() < $loadCost) {
33
                return false;
34
            }
35
        }
36
37
        return true;
38
    }
39
40
    #[\Override]
41
    public function loadReactor(
42
        Spacecraft $spacecraft,
43
        int $additionalLoad,
44
        ?ManagerProviderInterface $managerProvider,
45
        ReactorWrapperInterface $reactor
46
    ): ?string {
47
        if ($reactor->getLoad() >= $reactor->getCapacity()) {
48
            return null;
49
        }
50
51
        $capaPerLoad = $reactor->get()->getLoadUnits();
52
53
        //check for core limitation
54
        $loadUnits = ceil($additionalLoad /  $capaPerLoad);
55
        if ($loadUnits *  $capaPerLoad > $reactor->getCapacity() - $reactor->getLoad()) {
56
            $loadUnits = ceil(($reactor->getCapacity() - $reactor->getLoad()) /  $capaPerLoad);
57
        }
58
59
        $loadUnits = (int) $loadUnits;
60
61
        if ($loadUnits < 1) {
62
            return null;
63
        }
64
65
        $storage = $managerProvider !== null ? $managerProvider->getStorage() : $spacecraft->getStorage();
66
67
        // check for ressource limitation
68
        $costs = $reactor->get()->getLoadCost();
69
        foreach ($costs as $commodityId => $loadUnitsCost) {
70
            $storageElement = $storage->get($commodityId);
71
72
            if ($storageElement === null) {
73
                throw new RuntimeException('storageContainsNeededCommodities should be called first');
74
            }
75
76
            if ($storageElement->getAmount() < ($loadUnits * $loadUnitsCost)) {
77
                $loadUnits = (int) ($storageElement->getAmount() / $loadUnitsCost);
78
            }
79
        }
80
81
        //consume ressources
82
        foreach ($costs as $commodityId => $loadCost) {
83
            $storageElement = $storage->get($commodityId);
84
85
            if ($storageElement === null) {
86
                throw new RuntimeException('storageContainsNeededCommodities should be called first');
87
            }
88
89
            if ($managerProvider !== null) {
90
                $managerProvider->lowerStorage(
91
                    $storageElement->getCommodity(),
92
                    $loadCost * $loadUnits
93
                );
94
            } else {
95
                $this->storageManager->lowerStorage(
96
                    $spacecraft,
97
                    $storageElement->getCommodity(),
98
                    $loadCost * $loadUnits
99
                );
100
            }
101
        }
102
103
        //truncate output
104
        if ($reactor->getLoad() + $loadUnits *  $capaPerLoad > $reactor->getCapacity()) {
105
            $loadUnits = $reactor->getCapacity() - $reactor->getLoad();
106
        } else {
107
            $loadUnits *= $capaPerLoad;
108
        }
109
        $reactor->changeLoad($loadUnits);
110
111
        $systemName = $reactor->get()->getSystemType()->getDescription();
112
113
        if ($managerProvider !== null) {
114
            $this->privateMessageSender->send(
115
                $managerProvider->getUser()->getId(),
116
                $spacecraft->getUser()->getId(),
117
                sprintf(
118
                    _('Die %s hat in Sektor %s den %s der %s um %d Einheiten aufgeladen'),
119
                    $managerProvider->getName(),
120
                    $spacecraft->getSectorString(),
121
                    $systemName,
122
                    $spacecraft->getName(),
123
                    $loadUnits
124
                ),
125
                PrivateMessageFolderTypeEnum::SPECIAL_TRADE,
126
                $spacecraft
127
            );
128
        }
129
130
        return sprintf(
131
            _('%s: Der %s wurde um %d Einheiten aufgeladen'),
132
            $spacecraft->getName(),
133
            $systemName,
134
            $loadUnits
135
        );
136
    }
137
}
138