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 Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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