Passed
Push — master ( c539b9...a2853f )
by Nico
42:04 queued 28:25
created

ProceedStorage   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 4.76%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 67
ccs 2
cts 42
cp 0.0476
rs 10
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
B work() 0 59 11
A __construct() 0 5 1
1
<?php
2
3
namespace Stu\Module\Tick\Colony\Component;
4
5
use Stu\Lib\Information\InformationInterface;
6
use Stu\Lib\Transfer\Storage\StorageManagerInterface;
7
use Stu\Module\Commodity\Lib\CommodityCacheInterface;
8
use Stu\Orm\Entity\ColonyInterface;
9
use Stu\Orm\Repository\ColonyDepositMiningRepositoryInterface;
10
11
class ProceedStorage implements ColonyTickComponentInterface
12
{
13 1
    public function __construct(
14
        private readonly ColonyDepositMiningRepositoryInterface $colonyDepositMiningRepository,
15
        private readonly CommodityCacheInterface $commodityCache,
16
        private readonly StorageManagerInterface $storageManager
17 1
    ) {}
18
19
    public function work(ColonyInterface $colony, array &$production, InformationInterface $information): void
20
    {
21
        $sum = $colony->getStorageSum();
22
23
        //DECREASE
24
        foreach ($production as $commodityId => $obj) {
25
            $amount = $obj->getProduction();
26
            $commodity = $this->commodityCache->get($commodityId);
27
28
            if ($amount < 0) {
29
                $amount = abs($amount);
30
31
                if ($commodity->isSaveable()) {
32
                    // STANDARD
33
                    $this->storageManager->lowerStorage(
34
                        $colony,
35
                        $this->commodityCache->get($commodityId),
36
                        $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

36
                        /** @scrutinizer ignore-type */ $amount
Loading history...
37
                    );
38
                    $sum -= $amount;
39
                } else {
40
                    // EFFECTS
41
                    $depositMining = $this->colonyDepositMiningRepository->getCurrentUserDepositMinings($colony)[$commodityId];
42
43
                    $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

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