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

ProceedCrewTraining   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 69
ccs 0
cts 36
cp 0
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B processUser() 0 57 8
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