Passed
Push — dev ( 91d807...b97e85 )
by Janko
28:41
created

PirateWrathManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Stu\Lib\Pirate\Component;
4
5
use Stu\Lib\Pirate\PirateReactionTriggerEnum;
6
use Stu\Module\Logging\LoggerUtilFactoryInterface;
7
use Stu\Module\Logging\PirateLoggerInterface;
8
use Stu\Module\PlayerSetting\Lib\UserEnum;
9
use Stu\Orm\Entity\UserInterface;
10
use Stu\Orm\Repository\PirateWrathRepositoryInterface;
11
12
class PirateWrathManager implements PirateWrathManagerInterface
13
{
14
    public const MAX_WRATH = 200;
15
16
    private PirateLoggerInterface $logger;
17
18
    public function __construct(
19
        private PirateWrathRepositoryInterface $pirateWrathRepository,
20
        LoggerUtilFactoryInterface $loggerUtilFactory
21
    ) {
22
        $this->logger = $loggerUtilFactory->getPirateLogger();
23
    }
24
25
    public function increaseWrath(UserInterface $user, PirateReactionTriggerEnum $reactionTrigger): void
26
    {
27
        if (
28
            $user->isNpc()
29
            || $user->getId() === UserEnum::USER_NPC_KAZON
30
        ) {
31
            return;
32
        }
33
34
        $wrath = $user->getPirateWrath();
35
        if ($wrath === null) {
36
            $wrath = $this->pirateWrathRepository->prototype();
37
            $wrath->setUser($user);
38
            $user->setPirateWrath($wrath);
39
        }
40
41
        if ($wrath->getWrath() >= self::MAX_WRATH) {
42
            $this->logger->logf(
43
                '    user %d already reached MAX_WRATH = %d',
44
                $user->getId(),
45
                self::MAX_WRATH
46
            );
47
            return;
48
        }
49
50
        // reset protection timeout
51
        $timeout = $wrath->getProtectionTimeout();
52
        if (
53
            $timeout !== null
54
            && $timeout > time()
55
        ) {
56
            $this->logger->logf(
57
                '    reset protection timeout of user %d',
58
                $user->getId(),
59
            );
60
            $wrath->setProtectionTimeout(null);
61
        }
62
63
        // increase wrath
64
        $currentWrath = $wrath->getWrath();
65
        $wrath->setWrath($currentWrath + $reactionTrigger->getWrath());
66
67
        $this->logger->logf(
68
            '    increased wrath of user %d from %d to %d',
69
            $user->getId(),
70
            $currentWrath,
71
            $wrath->getWrath()
72
        );
73
    }
74
}
75