TorpedoTransferStrategy::transfer()   C
last analyzed

Complexity

Conditions 12
Paths 16

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
cc 12
eloc 27
nc 16
nop 4
dl 0
loc 47
ccs 0
cts 28
cp 0
crap 156
rs 6.9666
c 0
b 0
f 0

How to fix   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
declare(strict_types=1);
4
5
namespace Stu\Lib\Transfer\Strategy;
6
7
use request;
8
use RuntimeException;
9
use Stu\Lib\Information\InformationInterface;
10
use Stu\Lib\Transfer\Wrapper\StorageEntityWrapperInterface;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Orm\Entity\Colony;
13
14
class TorpedoTransferStrategy implements TransferStrategyInterface
15
{
16
    #[\Override]
17
    public function setTemplateVariables(
18
        bool $isUnload,
19
        StorageEntityWrapperInterface $source,
20
        StorageEntityWrapperInterface $target,
21
        GameControllerInterface $game
22
    ): void {
23
24
        if ($target instanceof Colony) {
25
            throw new RuntimeException('this should not happen');
26
        }
27
28
        if ($isUnload) {
29
            $max = min(
30
                $target->getMaxTorpedos(),
31
                $source->getTorpedoCount()
32
            );
33
            $commodityId = $source->getTorpedo() === null ? null : $source->getTorpedo()->getCommodityId();
34
        } else {
35
            $max = $target->getTorpedoCount();
36
            $commodityId = $target->getTorpedo() === null ? null : $target->getTorpedo()->getCommodityId();
37
        }
38
39
        $game->setTemplateVar('MAXIMUM', $max);
40
        $game->setTemplateVar('COMMODITY_ID', $commodityId);
41
    }
42
43
    #[\Override]
44
    public function transfer(
45
        bool $isUnload,
46
        StorageEntityWrapperInterface $source,
47
        StorageEntityWrapperInterface $target,
48
        InformationInterface $information
49
    ): void {
50
51
        $torpedoType = $isUnload ? $source->getTorpedo() : $target->getTorpedo();
52
        if ($torpedoType === null) {
53
            throw new RuntimeException('this should not happen');
54
        }
55
56
        if (!$source->canTransferTorpedos($information)) {
57
            return;
58
        }
59
60
        $destination = $isUnload ? $target : $source;
61
        if (!$destination->canStoreTorpedoType($torpedoType, $information)) {
62
            return;
63
        }
64
65
        //TODO use energy to transfer
66
        $requestedTransferCount = request::postInt('tcount');
67
68
        $amount = min(
69
            $requestedTransferCount,
70
            $isUnload ? $source->getTorpedoCount() : $target->getTorpedoCount(),
71
            $isUnload ? $target->getMaxTorpedos() - $target->getTorpedoCount() : $source->getMaxTorpedos() - $source->getTorpedoCount()
72
        );
73
74
        if ($amount < 1) {
75
            $information->addInformation('Es konnten keine Torpedos transferiert werden');
76
            return;
77
        }
78
79
        $target->changeTorpedo($isUnload ? $amount : -$amount, $torpedoType);
80
        $source->changeTorpedo($isUnload ? -$amount : $amount, $torpedoType);
81
82
        $information->addInformation(
83
            sprintf(
84
                'Die %s hat in Sektor %s %d Torpedos %s %s transferiert',
85
                $source->getName(),
86
                $source->getLocation()->getSectorString(),
87
                $amount,
88
                $isUnload ? 'zur' : 'von der',
89
                $target->getName()
90
            ),
91
        );
92
    }
93
}
94