Passed
Push — dev ( c4f015...eeaa0f )
by Janko
27:51
created

TickManager::shipRepairEvents()   B

Complexity

Conditions 10
Paths 15

Size

Total Lines 48
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 26
nc 15
nop 1
dl 0
loc 48
ccs 0
cts 27
cp 0
crap 110
rs 7.6666
c 0
b 0
f 0

How to fix   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
namespace Stu\Module\Tick;
4
5
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...
6
use Stu\Component\Game\TimeConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Game\TimeConstants 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...
7
use Stu\Module\Logging\LoggerUtilFactoryInterface;
8
use Stu\Module\Logging\LoggerUtilInterface;
9
use Stu\Orm\Entity\GameTurnInterface;
10
use Stu\Orm\Repository\GameTurnRepositoryInterface;
11
use Stu\Orm\Repository\GameTurnStatsRepositoryInterface;
12
use Stu\Orm\Repository\KnPostRepositoryInterface;
13
use Stu\Orm\Repository\UserLockRepositoryInterface;
14
use Stu\Orm\Repository\UserRepositoryInterface;
15
16
final class TickManager implements TickManagerInterface
17
{
18
    private LoggerUtilInterface $loggerUtil;
19
20
    public function __construct(
21
        private GameTurnRepositoryInterface $gameTurnRepository,
22
        private UserLockRepositoryInterface $userLockRepository,
23
        private GameTurnStatsRepositoryInterface $gameTurnStatsRepository,
24
        private UserRepositoryInterface $userRepository,
25
        private KnPostRepositoryInterface $knPostRepository,
26
        LoggerUtilFactoryInterface $loggerUtilFactory
27
    ) {
28
        $this->loggerUtil = $loggerUtilFactory->getLoggerUtil();
29
    }
30
31
    #[Override]
32
    public function work(): void
33
    {
34
        $turn = $this->gameTurnRepository->getCurrent();
35
        $this->endTurn($turn);
0 ignored issues
show
Bug introduced by
It seems like $turn can also be of type null; however, parameter $turn of Stu\Module\Tick\TickManager::endTurn() does only seem to accept Stu\Orm\Entity\GameTurnInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
        $this->endTurn(/** @scrutinizer ignore-type */ $turn);
Loading history...
36
        $this->reduceUserLocks();
37
        $newTurn = $this->startTurn($turn);
0 ignored issues
show
Bug introduced by
It seems like $turn can also be of type null; however, parameter $turn of Stu\Module\Tick\TickManager::startTurn() does only seem to accept Stu\Orm\Entity\GameTurnInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
        $newTurn = $this->startTurn(/** @scrutinizer ignore-type */ $turn);
Loading history...
38
        $this->createGameTurnStats($newTurn);
39
    }
40
41
    private function endTurn(GameTurnInterface $turn): void
42
    {
43
        $turn->setEnd(time());
44
45
        $this->gameTurnRepository->save($turn);
46
    }
47
48
    private function startTurn(GameTurnInterface $turn): GameTurnInterface
49
    {
50
        $obj = $this->gameTurnRepository->prototype();
51
        $obj->setStart(time());
52
        $obj->setEnd(0);
53
        $obj->setTurn($turn->getTurn() + 1);
54
55
        $this->gameTurnRepository->save($obj);
56
57
        return $obj;
58
    }
59
60
    private function reduceUserLocks(): void
61
    {
62
        $locks = $this->userLockRepository->getActive();
63
64
        foreach ($locks as $lock) {
65
            $remainingTicks = $lock->getRemainingTicks();
66
67
            if ($remainingTicks === 1) {
68
                $userId = $lock->getUser()->getId();
69
70
                $lock->setUser(null);
71
                $lock->setUserId(null);
72
                $lock->setFormerUserId($userId);
73
                $lock->setRemainingTicks(0);
74
            } else {
75
                $lock->setRemainingTicks($remainingTicks - 1);
76
            }
77
78
            $this->userLockRepository->save($lock);
79
        }
80
    }
81
82
    private function createGameTurnStats(GameTurnInterface $turn): void
83
    {
84
        $stats = $this->gameTurnStatsRepository->prototype();
85
86
        $this->loggerUtil->log('setting stats values');
87
88
        $stats->setTurn($turn);
89
        $stats->setUserCount($this->userRepository->getActiveAmount());
90
        $stats->setLogins24h($this->userRepository->getActiveAmountRecentlyOnline(time() - TimeConstants::ONE_DAY_IN_SECONDS));
91
        $stats->setInactiveCount($this->userRepository->getInactiveAmount(14));
92
        $stats->setVacationCount($this->userRepository->getVacationAmount());
93
        $stats->setShipCount($this->gameTurnStatsRepository->getShipCount());
94
        $stats->setShipCountManned($this->gameTurnStatsRepository->getShipCountManned());
95
        $stats->setShipCountNpc($this->gameTurnStatsRepository->getShipCountNpc());
96
        $stats->setKnCount($this->knPostRepository->getAmount());
97
        $stats->setFlightSig24h($this->gameTurnStatsRepository->getFlightSigs24h());
98
        $stats->setFlightSigSystem24h($this->gameTurnStatsRepository->getFlightSigsSystem24h());
99
100
        $this->gameTurnStatsRepository->save($stats);
101
        $this->loggerUtil->log('saved stats');
102
    }
103
}
104