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

TradepostDestruction   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 9.09%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 51
ccs 2
cts 22
cp 0.0909
rs 10
c 1
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handleShipDestruction() 0 42 5
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