Passed
Push — dev ( 216c8f...156d5c )
by Janko
15:03
created

ProceedStorage::work()   B

Complexity

Conditions 11
Paths 28

Size

Total Lines 61
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 38
nc 28
nop 3
dl 0
loc 61
ccs 0
cts 41
cp 0
crap 132
rs 7.3166
c 1
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
namespace Stu\Module\Tick\Colony\Component;
4
5
use Stu\Component\Player\Settings\UserSettingsProviderInterface;
6
use Stu\Lib\Information\InformationInterface;
7
use Stu\Lib\Transfer\Storage\StorageManagerInterface;
8
use Stu\Module\Commodity\Lib\CommodityCacheInterface;
9
use Stu\Orm\Entity\ColonyInterface;
10
use Stu\Orm\Repository\ColonyDepositMiningRepositoryInterface;
11
12
class ProceedStorage implements ColonyTickComponentInterface
13
{
14 1
    public function __construct(
15
        private readonly ColonyDepositMiningRepositoryInterface $colonyDepositMiningRepository,
16
        private readonly CommodityCacheInterface $commodityCache,
17
        private readonly StorageManagerInterface $storageManager,
18
        private readonly UserSettingsProviderInterface $userSettingsProvider
19 1
    ) {}
20
21
    public function work(ColonyInterface $colony, array &$production, InformationInterface $information): void
22
    {
23
        $sum = $colony->getStorageSum();
24
25
        //DECREASE
26
        foreach ($production as $commodityId => $obj) {
27
            $amount = $obj->getProduction();
28
            $commodity = $this->commodityCache->get($commodityId);
29
30
            if ($amount < 0) {
31
                $amount = abs($amount);
32
33
                if ($commodity->isSaveable()) {
34
                    // STANDARD
35
                    $this->storageManager->lowerStorage(
36
                        $colony,
37
                        $this->commodityCache->get($commodityId),
38
                        $amount
0 ignored issues
show
Bug introduced by
It seems like $amount can also be of type double; however, parameter $amount of Stu\Lib\Transfer\Storage...terface::lowerStorage() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
                        /** @scrutinizer ignore-type */ $amount
Loading history...
39
                    );
40
                    $sum -= $amount;
41
                } else {
42
                    // EFFECTS
43
                    $depositMining = $this->colonyDepositMiningRepository->getCurrentUserDepositMinings($colony)[$commodityId];
44
45
                    $depositMining->setAmountLeft($depositMining->getAmountLeft() - $amount);
0 ignored issues
show
Bug introduced by
$depositMining->getAmountLeft() - $amount of type double is incompatible with the type integer expected by parameter $amountLeft of Stu\Orm\Entity\ColonyDep...erface::setAmountLeft(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
                    $depositMining->setAmountLeft(/** @scrutinizer ignore-type */ $depositMining->getAmountLeft() - $amount);
Loading history...
46
                    $this->colonyDepositMiningRepository->save($depositMining);
47
                }
48
            }
49
        }
50
51
        foreach ($production as $commodityId => $obj) {
52
53
            $commodity = $this->commodityCache->get($commodityId);
54
            if ($obj->getProduction() <= 0 || !$commodity->isSaveable()) {
55
                continue;
56
            }
57
58
            $isStorageNotification = $this->userSettingsProvider->isStorageNotification($colony->getUser());
59
            if ($sum >= $colony->getMaxStorage()) {
60
                if ($isStorageNotification) {
61
                    $information->addInformation('Das Lager der Kolonie ist voll');
62
                }
63
                break;
64
            }
65
            if ($sum + $obj->getProduction() > $colony->getMaxStorage()) {
66
                $this->storageManager->upperStorage(
67
                    $colony,
68
                    $commodity,
69
                    $colony->getMaxStorage() - $sum
70
                );
71
                if ($isStorageNotification) {
72
                    $information->addInformation('Das Lager der Kolonie ist voll');
73
                }
74
                break;
75
            }
76
            $this->storageManager->upperStorage(
77
                $colony,
78
                $commodity,
79
                $obj->getProduction()
80
            );
81
            $sum += $obj->getProduction();
82
        }
83
    }
84
}
85