Passed
Push — dev ( eeaa0f...91a85a )
by Janko
26:10
created

ProceedCrewTraining::processUser()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 57
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 32
nc 8
nop 1
dl 0
loc 57
ccs 0
cts 34
cp 0
crap 72
rs 8.1635
c 0
b 0
f 0

How to fix   Long Method   

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
namespace Stu\Module\Tick\User\Component;
4
5
use Stu\Component\Building\BuildingEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Building\BuildingEnum 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...
6
use Stu\Component\Colony\ColonyFunctionManagerInterface;
7
use Stu\Component\Crew\CrewCountRetrieverInterface;
8
use Stu\Component\Player\CrewLimitCalculatorInterface;
9
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
10
use Stu\Module\Crew\Lib\CrewCreatorInterface;
11
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
12
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
13
use Stu\Module\PlayerSetting\Lib\UserEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserEnum 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...
14
use Stu\Module\Tick\User\UserTickComponentInterface;
15
use Stu\Orm\Entity\CrewTrainingInterface;
16
use Stu\Orm\Entity\UserInterface;
17
use Stu\Orm\Repository\CrewTrainingRepositoryInterface;
18
19
class ProceedCrewTraining implements UserTickComponentInterface
20
{
21
    public function __construct(
22
        private CrewTrainingRepositoryInterface $crewTrainingRepository,
23
        private CrewCountRetrieverInterface $crewCountRetriever,
24
        private ColonyLibFactoryInterface $colonyLibFactory,
25
        private ColonyFunctionManagerInterface $colonyFunctionManager,
26
        private CrewCreatorInterface $crewCreator,
27
        private CrewLimitCalculatorInterface $crewLimitCalculator,
28
        private PrivateMessageSenderInterface $privateMessageSender
29
    ) {
30
    }
31
    public function processUser(UserInterface $user): void
32
    {
33
        /** @var array<CrewTrainingInterface> */
34
        $crewTrainings = [];
35
36
        foreach ($user->getColonies() as $colony) {
37
            $crewTrainings = array_merge($crewTrainings, $colony->getCrewTrainings()->toArray());
38
        }
39
40
        $trainableCrewCount = $this->crewCountRetriever->getTrainableCount($user);
41
        $trainedCrewAmount = 0;
42
43
        foreach ($crewTrainings as $obj) {
44
            if ($trainedCrewAmount >= $trainableCrewCount) {
45
                break;
46
            }
47
            $colony = $obj->getColony();
48
49
            $freeAssignmentCount = $this->colonyLibFactory->createColonyPopulationCalculator(
50
                $colony
51
            )->getFreeAssignmentCount();
52
53
            //colony can't hold more crew
54
            if ($freeAssignmentCount === 0) {
55
                $this->crewTrainingRepository->delete($obj);
56
                continue;
57
            }
58
59
            $globalCrewLimit = $this->crewLimitCalculator->getGlobalCrewLimit($obj->getUser());
60
61
            //user has too much crew
62
            if ($globalCrewLimit - $this->crewCountRetriever->getAssignedCount($obj->getUser()) <= 0) {
63
                $this->crewTrainingRepository->delete($obj);
64
                continue;
65
            }
66
67
            //no academy online
68
            if (!$this->colonyFunctionManager->hasActiveFunction($colony, BuildingEnum::BUILDING_FUNCTION_ACADEMY)) {
69
                continue;
70
            }
71
            $this->crewCreator->create($obj->getUserId(), $colony);
72
73
            $this->crewTrainingRepository->delete($obj);
74
            $trainedCrewAmount++;
75
        }
76
77
        // send message for crew training
78
        if ($trainedCrewAmount > 0) {
79
80
            $this->privateMessageSender->send(
81
                UserEnum::USER_NOONE,
82
                $user->getId(),
83
                sprintf(
84
                    "Es wurden erfolgreich %d Crewman ausgebildet.",
85
                    $trainedCrewAmount
86
                ),
87
                PrivateMessageFolderTypeEnum::SPECIAL_COLONY
88
            );
89
        }
90
    }
91
}
92