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

ProceedStorage   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 4.65%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
dl 0
loc 70
ccs 2
cts 43
cp 0.0465
rs 10
c 1
b 0
f 0
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
B work() 0 61 11
A __construct() 0 6 1
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