Passed
Push — master ( bf860f...1dd8f7 )
by Nico
57:55 queued 29:36
created

TradepostDestruction::handleShipDestruction()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 42
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 18
nc 5
nop 4
dl 0
loc 42
ccs 0
cts 20
cp 0
crap 30
rs 9.3554
c 1
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Ship\Lib\Destruction\Handler;
4
5
use Stu\Component\Ship\Storage\ShipStorageManagerInterface;
6
use Stu\Lib\Information\InformationInterface;
7
use Stu\Module\Ship\Lib\Destruction\ShipDestroyerInterface;
8
use Stu\Module\Ship\Lib\Destruction\ShipDestructionCauseEnum;
9
use Stu\Module\Ship\Lib\ShipWrapperInterface;
10
use Stu\Orm\Repository\StorageRepositoryInterface;
11
use Stu\Orm\Repository\TradePostRepositoryInterface;
12
13
class TradepostDestruction implements ShipDestructionHandlerInterface
14
{
15 1
    public function __construct(
16
        private StorageRepositoryInterface $storageRepository,
17
        private TradePostRepositoryInterface $tradePostRepository,
18
        private ShipStorageManagerInterface $shipStorageManager
19
    ) {
20 1
    }
21
22
    public function handleShipDestruction(
23
        ?ShipDestroyerInterface $destroyer,
24
        ShipWrapperInterface $destroyedShipWrapper,
25
        ShipDestructionCauseEnum $cause,
26
        InformationInterface $informations
27
    ): void {
28
29
        $tradePost = $destroyedShipWrapper->get()->getTradePost();
30
31
        if ($tradePost === null) {
32
            return;
33
        }
34
35
        //salvage offers and storage
36
        $storages = $this->storageRepository->getByTradePost($tradePost->getId());
37
        foreach ($storages as $storage) {
38
            //only 50% off all storages
39
            if (random_int(0, 1) === 0) {
40
                $this->storageRepository->delete($storage);
41
                continue;
42
            }
43
44
            //only 0 to 50% of the specific amount
45
            $amount = (int)ceil($storage->getAmount() / 100 * random_int(0, 50));
46
47
            if ($amount === 0) {
48
                $this->storageRepository->delete($storage);
49
                continue;
50
            }
51
52
            //add to trumfield storage
53
            $this->shipStorageManager->upperStorage(
54
                $tradePost->getShip(),
55
                $storage->getCommodity(),
56
                $amount
57
            );
58
59
            $this->storageRepository->delete($storage);
60
        }
61
62
        //remove tradepost and cascading stuff
63
        $this->tradePostRepository->delete($tradePost);
64
    }
65
}
66