TroopTransferStrategy::transfer()   B
last analyzed

Complexity

Conditions 11
Paths 35

Size

Total Lines 51
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
eloc 28
nc 35
nop 4
dl 0
loc 51
ccs 0
cts 31
cp 0
crap 132
rs 7.3166
c 0
b 0
f 0

How to fix   Long Method    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 Stu\Component\Spacecraft\Crew\SpacecraftCrewCalculatorInterface;
9
use Stu\Lib\Information\InformationInterface;
10
use Stu\Lib\Transfer\Wrapper\StorageEntityWrapperInterface;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Spacecraft\Lib\Crew\TroopTransferUtilityInterface;
13
use Stu\Orm\Entity\CrewAssignment;
14
use Stu\Orm\Entity\Ship;
15
use Stu\Orm\Entity\User;
16
17
class TroopTransferStrategy implements TransferStrategyInterface
18
{
19 1
    public function __construct(
20
        private SpacecraftCrewCalculatorInterface $shipCrewCalculator,
21
        private TroopTransferUtilityInterface $troopTransferUtility,
22 1
    ) {}
23
24 4
    #[\Override]
25
    public function setTemplateVariables(
26
        bool $isUnload,
27
        StorageEntityWrapperInterface $source,
28
        StorageEntityWrapperInterface $target,
29
        GameControllerInterface $game
30
    ): void {
31
32 4
        $user = $game->getUser();
33 4
        $targetEntity = $target->get();
34
35
        if (
36 4
            $targetEntity instanceof Ship
37 4
            && $targetEntity->getBuildplan() !== null
0 ignored issues
show
Bug introduced by
The method getBuildplan() does not exist on Stu\Lib\Transfer\EntityWithStorageInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
            && $targetEntity->/** @scrutinizer ignore-call */ getBuildplan() !== null

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
38
        ) {
39 2
            $game->setTemplateVar('SHOW_TARGET_CREW', true);
40 2
            $game->setTemplateVar('ACTUAL_TARGET_CREW', $targetEntity->getCrewCount());
0 ignored issues
show
Bug introduced by
The method getCrewCount() does not exist on Stu\Lib\Transfer\EntityWithStorageInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
            $game->setTemplateVar('ACTUAL_TARGET_CREW', $targetEntity->/** @scrutinizer ignore-call */ getCrewCount());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41 2
            $game->setTemplateVar('MINIMUM_TARGET_CREW', $targetEntity->getBuildplan()->getCrew());
42 2
            $game->setTemplateVar(
43 2
                'MAXIMUM_TARGET_CREW',
44 2
                $this->shipCrewCalculator->getMaxCrewCountByShip($targetEntity)
45 2
            );
46
        }
47
48 4
        $game->setTemplateVar('MAXIMUM', $this->getMaxTransferrableCrew($source, $target, $user, $isUnload));
49
    }
50
51 4
    private function getMaxTransferrableCrew(
52
        StorageEntityWrapperInterface $source,
53
        StorageEntityWrapperInterface $target,
54
        User $user,
55
        bool $isUnload
56
    ): int {
57 4
        return min(
58 4
            $isUnload ? $source->getMaxTransferrableCrew(false, $user) : $target->getMaxTransferrableCrew(true, $user),
59 4
            $isUnload ?  $target->getFreeCrewSpace($user) : $source->getFreeCrewSpace($user)
60 4
        );
61
    }
62
63
    #[\Override]
64
    public function transfer(
65
        bool $isUnload,
66
        StorageEntityWrapperInterface $source,
67
        StorageEntityWrapperInterface $target,
68
        InformationInterface $information
69
    ): void {
70
71
        $user = $source->getUser();
72
73
        $amount = min(
74
            request::postInt('crewcount'),
75
            $this->getMaxTransferrableCrew($source, $target, $user, $isUnload)
76
        );
77
78
        if ($amount < 1) {
79
            $information->addInformation('Es konnten keine Crewman transferiert werden');
80
            return;
81
        }
82
83
        if (!$source->checkCrewStorage($amount, $isUnload, $information)) {
84
            return;
85
        }
86
87
        if ($isUnload && !$target->acceptsCrewFrom($amount, $user, $information)) {
88
            return;
89
        }
90
91
        $destination = $isUnload ? $target : $source;
92
        $crewAssignments = $isUnload ? $source->get()->getCrewAssignments() : $target->get()->getCrewAssignments();
93
        $filteredByUser = $crewAssignments->filter(fn(CrewAssignment $crewAssignment): bool => $crewAssignment->getCrew()->getUser()->getId() === $source->getUser()->getId())->toArray();
94
        $slice = array_slice($filteredByUser, 0, $amount);
95
96
        foreach ($slice as $crewAssignment) {
97
            $this->troopTransferUtility->assignCrew($crewAssignment, $destination->get());
98
        }
99
100
        $information->addInformationf(
101
            'Die %s hat %d Crewman %s der %s transferiert.',
102
            $source->getName(),
103
            $amount,
104
            $isUnload ? 'zu' : 'von',
105
            $target->getName()
106
        );
107
108
        $foreignCrewChangeAmount = $source->getUser()->getId() !== $target->getUser()->getId()
109
            ? ($isUnload ? $amount : -$amount)
110
            : 0;
111
112
        $source->postCrewTransfer(0, $target, $information);
113
        $target->postCrewTransfer($foreignCrewChangeAmount, $source, $information);
114
    }
115
}
116