Passed
Pull Request — master (#1969)
by Janko
22:34 queued 10:03
created

TroopTransferStrategy::transfer()   B

Complexity

Conditions 11
Paths 68

Size

Total Lines 52
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 68
nop 4
dl 0
loc 52
ccs 0
cts 32
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 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 Stu\Component\Spacecraft\Crew\SpacecraftCrewCalculatorInterface;
10
use Stu\Lib\Information\InformationInterface;
11
use Stu\Lib\Transfer\Wrapper\StorageEntityWrapperInterface;
12
use Stu\Module\Control\GameControllerInterface;
13
use Stu\Module\Spacecraft\Lib\Crew\TroopTransferUtilityInterface;
14
use Stu\Orm\Entity\CrewAssignmentInterface;
15
use Stu\Orm\Entity\ShipInterface;
16
use Stu\Orm\Entity\UserInterface;
17
18
class TroopTransferStrategy implements TransferStrategyInterface
19
{
20 1
    public function __construct(
21
        private SpacecraftCrewCalculatorInterface $shipCrewCalculator,
22
        private TroopTransferUtilityInterface $troopTransferUtility,
23 1
    ) {}
24
25 4
    #[Override]
26
    public function setTemplateVariables(
27
        bool $isUnload,
28
        StorageEntityWrapperInterface $source,
29
        StorageEntityWrapperInterface $target,
30
        GameControllerInterface $game
31
    ): void {
32
33 4
        $user = $game->getUser();
34 4
        $targetEntity = $target->get();
35
36
        if (
37 4
            $targetEntity instanceof ShipInterface
38 4
            && $targetEntity->getBuildplan() !== null
39
        ) {
40 2
            $game->setTemplateVar('SHOW_TARGET_CREW', true);
41 2
            $game->setTemplateVar('ACTUAL_TARGET_CREW', $targetEntity->getCrewCount());
42 2
            $game->setTemplateVar('MINIMUM_TARGET_CREW', $targetEntity->getBuildplan()->getCrew());
43 2
            $game->setTemplateVar(
44 2
                'MAXIMUM_TARGET_CREW',
45 2
                $this->shipCrewCalculator->getMaxCrewCountByShip($targetEntity)
46 2
            );
47
        }
48
49 4
        $game->setTemplateVar('MAXIMUM', $this->getMaxTransferrableCrew($source, $target, $user, $isUnload));
50
    }
51
52 4
    private function getMaxTransferrableCrew(
53
        StorageEntityWrapperInterface $source,
54
        StorageEntityWrapperInterface $target,
55
        UserInterface $user,
56
        bool $isUnload
57
    ): int {
58 4
        return min(
59 4
            $isUnload ? $source->getMaxTransferrableCrew(false, $user) : $target->getMaxTransferrableCrew(true, $user),
60 4
            $isUnload ?  $target->getFreeCrewSpace($user) : $source->getFreeCrewSpace($user)
61 4
        );
62
    }
63
64
    #[Override]
65
    public function transfer(
66
        bool $isUnload,
67
        StorageEntityWrapperInterface $source,
68
        StorageEntityWrapperInterface $target,
69
        InformationInterface $information
70
    ): void {
71
72
        $user = $source->getUser();
73
74
        $amount = min(
75
            request::postInt('crewcount'),
76
            $this->getMaxTransferrableCrew($source, $target, $user, $isUnload)
77
        );
78
79
        if ($amount < 1) {
80
            $information->addInformation('Es konnten keine Crewman transferiert werden');
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(CrewAssignmentInterface $crewAssignment): bool => $crewAssignment->getCrew()->getUser() === $source->getUser())->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->addInformation(
101
            sprintf(
102
                _('Die %s hat %d Crewman %s der %s transferiert'),
103
                $source->getName(),
104
                $amount,
105
                $isUnload ? 'zu' : 'von',
106
                $target->getName()
107
            )
108
        );
109
110
        $foreignCrewChangeAmount = $source->getUser() !== $target->getUser()
111
            ? ($isUnload ? $amount : -$amount)
112
            : 0;
113
114
        $source->postCrewTransfer($foreignCrewChangeAmount, $target, $information);
115
        $target->postCrewTransfer($foreignCrewChangeAmount, $source, $information);
116
    }
117
}
118