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

ColonyTickManager::proceedCrewTraining()   B

Complexity

Conditions 9
Paths 33

Size

Total Lines 55
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 32
c 0
b 0
f 0
nc 33
nop 2
dl 0
loc 55
ccs 0
cts 34
cp 0
crap 90
rs 8.0555

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
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\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...
9
use Stu\Module\Logging\LoggerUtilFactoryInterface;
10
use Stu\Module\Logging\LoggerUtilInterface;
11
use Stu\Module\Tick\AbstractTickManager;
12
use Stu\Module\Tick\Lock\LockManagerInterface;
13
use Stu\Module\Tick\Lock\LockTypeEnum;
14
use Stu\Orm\Repository\ColonyRepositoryInterface;
15
use Ubench;
16
17
final class ColonyTickManager extends AbstractTickManager implements ColonyTickManagerInterface
18
{
19
    private LoggerUtilInterface $loggerUtil;
20
21 1
    public function __construct(
22
        private ColonyTickInterface $colonyTick,
23
        private ColonyRepositoryInterface $colonyRepository,
24
        private LockManagerInterface $lockManager,
25
        LoggerUtilFactoryInterface $loggerUtilFactory,
26
        private Ubench $benchmark
27
    ) {
28 1
        $this->loggerUtil = $loggerUtilFactory->getLoggerUtil();
29
    }
30
31 1
    #[Override]
32
    public function work(int $batchGroup, int $batchGroupCount): void
33
    {
34 1
        $this->setLock($batchGroup);
35
        try {
36 1
            $entityCount = $this->colonyLoop($batchGroup, $batchGroupCount);
37
38
            $this->loggerUtil->init(sprintf(
39
                'COLOTICK_%dof%d',
40
                $batchGroup,
41
                $batchGroupCount
42
            ), LoggerEnum::LEVEL_WARNING);
43
            $this->logBenchmarkResult($entityCount);
44
        } finally {
45 1
            $this->clearLock($batchGroup);
46
        }
47
    }
48
49 1
    private function colonyLoop(int $batchGroup, int $batchGroupCount): int
50
    {
51 1
        $colonyList = $this->colonyRepository->getByBatchGroup($batchGroup, $batchGroupCount);
52
53
        $entityCount = 0;
54
        foreach ($colonyList as $colony) {
55
            //echo "Processing Colony ".$colony->getId()." at ".microtime()."\n";
56
57
            //handle colony only if vacation mode not active
58
            if (!$colony->getUser()->isVacationRequestOldEnough()) {
59
                $this->colonyTick->work($colony);
60
            }
61
62
            $entityCount++;
63
        }
64
65
        return $entityCount;
66
    }
67
68 1
    private function setLock(int $batchGroupId): void
69
    {
70 1
        $this->lockManager->setLock($batchGroupId, LockTypeEnum::COLONY_GROUP);
71
    }
72
73 1
    private function clearLock(int $batchGroupId): void
74
    {
75 1
        $this->lockManager->clearLock($batchGroupId, LockTypeEnum::COLONY_GROUP);
76
    }
77
78
    #[Override]
79
    protected function getBenchmark(): Ubench
80
    {
81
        return $this->benchmark;
82
    }
83
84
    #[Override]
85
    protected function getLoggerUtil(): LoggerUtilInterface
86
    {
87
        return $this->loggerUtil;
88
    }
89
}
90