Test Failed
Pull Request — dev (#1952)
by Janko
02:59
created

ColonyTickManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 12
dl 0
loc 15
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Tick\Colony;
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 Stu\Component\Building\BuildingFunctionEnum;
9
use Stu\Component\Colony\ColonyFunctionManagerInterface;
10
use Stu\Component\Crew\CrewCountRetrieverInterface;
11
use Stu\Component\Player\CrewLimitCalculatorInterface;
12
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
13
use Stu\Module\Crew\Lib\CrewCreatorInterface;
14
use Stu\Module\Logging\LoggerEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Logging\LoggerEnum 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...
15
use Stu\Module\Logging\LoggerUtilFactoryInterface;
16
use Stu\Module\Logging\LoggerUtilInterface;
17
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
18
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
19
use Stu\Module\PlayerSetting\Lib\UserConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserConstants 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...
20
use Stu\Module\Tick\AbstractTickManager;
21
use Stu\Module\Tick\Lock\LockManagerInterface;
22
use Stu\Module\Tick\Lock\LockTypeEnum;
23
use Stu\Orm\Repository\ColonyRepositoryInterface;
24
use Stu\Orm\Repository\CrewTrainingRepositoryInterface;
25
use Ubench;
26 2
27
final class ColonyTickManager extends AbstractTickManager implements ColonyTickManagerInterface
28
{
29
    private LoggerUtilInterface $loggerUtil;
30
31
    public function __construct(
32
        private ColonyTickInterface $colonyTick,
33
        private CrewCreatorInterface $crewCreator,
34
        private CrewTrainingRepositoryInterface $crewTrainingRepository,
35
        private ColonyRepositoryInterface $colonyRepository,
36
        private PrivateMessageSenderInterface $privateMessageSender,
37
        private CrewCountRetrieverInterface $crewCountRetriever,
38 2
        private ColonyFunctionManagerInterface $colonyFunctionManager,
39
        private CrewLimitCalculatorInterface $crewLimitCalculator,
40 2
        private ColonyLibFactoryInterface $colonyLibFactory,
41
        private LockManagerInterface $lockManager,
42
        LoggerUtilFactoryInterface $loggerUtilFactory,
43 2
        private Ubench $benchmark
44
    ) {
45 2
        $this->loggerUtil = $loggerUtilFactory->getLoggerUtil();
46 1
    }
47
48 1
    #[Override]
49
    public function work(int $batchGroup, int $batchGroupCount): void
50 2
    {
51
        $this->setLock($batchGroup);
52
        try {
53
            $entityCount = $this->colonyLoop($batchGroup, $batchGroupCount);
54 2
            $this->proceedCrewTraining($batchGroup, $batchGroupCount);
55
56 2
            $this->loggerUtil->init(sprintf(
57
                'COLOTICK_%dof%d',
58 1
                $batchGroup,
59 1
                $batchGroupCount
60
            ), LoggerEnum::LEVEL_WARNING);
61
            $this->logBenchmarkResult($entityCount);
62 1
        } finally {
63 1
            $this->clearLock($batchGroup);
64
        }
65
    }
66 1
67
    private function colonyLoop(int $batchGroup, int $batchGroupCount): int
68
    {
69 1
        $colonyList = $this->colonyRepository->getByBatchGroup($batchGroup, $batchGroupCount);
70
71
        $entityCount = 0;
72 1
        foreach ($colonyList as $colony) {
73
            //echo "Processing Colony ".$colony->getId()." at ".microtime()."\n";
74 1
75
            //handle colony only if vacation mode not active
76 1
            if (!$colony->getUser()->isVacationRequestOldEnough()) {
77
                $this->colonyTick->work($colony);
78
            }
79
80
            $entityCount++;
81
        }
82
83
        return $entityCount;
84
    }
85
86
    private function proceedCrewTraining(int $batchGroup, int $batchGroupCount): void
87
    {
88
        $user = [];
89
90
        foreach ($this->crewTrainingRepository->getByBatchGroup($batchGroup, $batchGroupCount) as $obj) {
91
            if (!isset($user[$obj->getUserId()])) {
92
                $user[$obj->getUserId()] = 0;
93
            }
94
            if ($user[$obj->getUserId()] >= $this->crewCountRetriever->getTrainableCount($obj->getUser())) {
95
                continue;
96
            }
97
            $colony = $obj->getColony();
98
99
            $freeAssignmentCount = $this->colonyLibFactory->createColonyPopulationCalculator(
100
                $colony
101
            )->getFreeAssignmentCount();
102
103
            //colony can't hold more crew
104
            if ($freeAssignmentCount === 0) {
105
                $this->crewTrainingRepository->delete($obj);
106
                continue;
107
            }
108
109
            $globalCrewLimit = $this->crewLimitCalculator->getGlobalCrewLimit($obj->getUser());
110
111
            //user has too much crew
112
            if ($globalCrewLimit - $this->crewCountRetriever->getAssignedCount($obj->getUser()) <= 0) {
113
                $this->crewTrainingRepository->delete($obj);
114 1
                continue;
115
            }
116
117
            //no academy online
118
            if (!$this->colonyFunctionManager->hasActiveFunction($colony, BuildingFunctionEnum::ACADEMY)) {
119
                continue;
120
            }
121
            $this->crewCreator->create($obj->getUserId(), $colony);
122
123
            $this->crewTrainingRepository->delete($obj);
124
            $user[$obj->getUserId()]++;
125
        }
126
127
        // send message for crew training
128
        foreach ($user as $userId => $count) {
129
            if ($count === 0) {
130
                continue;
131 2
            }
132
133 2
            $this->privateMessageSender->send(
134
                UserConstants::USER_NOONE,
135
                $userId,
136 2
                sprintf(
137
                    "Es wurden erfolgreich %d Crewman ausgebildet.",
138 2
                    $count
139
                ),
140
                PrivateMessageFolderTypeEnum::SPECIAL_COLONY
141 1
            );
142
        }
143
    }
144 1
145
    private function setLock(int $batchGroupId): void
146
    {
147
        $this->lockManager->setLock($batchGroupId, LockTypeEnum::COLONY_GROUP);
148
    }
149
150
    private function clearLock(int $batchGroupId): void
151
    {
152
        $this->lockManager->clearLock($batchGroupId, LockTypeEnum::COLONY_GROUP);
153
    }
154
155
    #[Override]
156
    protected function getBenchmark(): Ubench
157
    {
158
        return $this->benchmark;
159
    }
160
161
    #[Override]
162
    protected function getLoggerUtil(): LoggerUtilInterface
163
    {
164
        return $this->loggerUtil;
165
    }
166
}
167