Passed
Pull Request — master (#2146)
by Nico
35:16 queued 24:41
created

PirateRoundManager::addUserStats()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 33
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 24
nc 3
nop 2
dl 0
loc 33
ccs 0
cts 24
cp 0
crap 12
rs 9.536
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Lib\Pirate\Component;
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 Doctrine\ORM\EntityManagerInterface;
7
use Stu\Module\Logging\LoggerUtilFactoryInterface;
8
use Stu\Module\Logging\PirateLoggerInterface;
9
use Stu\Orm\Entity\UserInterface;
10
use Stu\Orm\Repository\PirateRoundRepositoryInterface;
11
use Stu\Orm\Repository\UserPirateRoundRepositoryInterface;
12
13
class PirateRoundManager implements PirateRoundManagerInterface
14
{
15
    private PirateLoggerInterface $logger;
16
17 1
    public function __construct(
18
        private PirateRoundRepositoryInterface $pirateRoundRepository,
19
        private UserPirateRoundRepositoryInterface $userPirateRoundRepository,
20
        private EntityManagerInterface $entityManager,
21
        LoggerUtilFactoryInterface $loggerUtilFactory,
22
    ) {
23 1
        $this->logger = $loggerUtilFactory->getPirateLogger();
24
    }
25
26
    #[Override]
27
    public function decreasePrestige(int $amount): void
28
    {
29
        if ($amount < 1) {
30
            return;
31
        }
32
33
        $currentRound = $this->pirateRoundRepository->getCurrentActiveRound();
34
        if ($currentRound === null) {
35
            return;
36
        }
37
38
        $currentPrestige = $currentRound->getActualPrestige();
39
        $newPrestige = max(0, $currentPrestige - $amount);
40
41
        $currentRound->setActualPrestige($newPrestige);
42
43
        if ($newPrestige === 0) {
44
            $currentRound->setEndTime(time());
45
        }
46
47
        $this->pirateRoundRepository->save($currentRound);
48
49
        $this->logger->logf(
50
            'DECREASED pirate round prestige from %d to %d',
51
            $currentPrestige,
52
            $newPrestige
53
        );
54
    }
55
56
    #[Override]
57
    public function addUserStats(UserInterface $user, int $prestige): void
58
    {
59
        $currentRound = $this->pirateRoundRepository->getCurrentActiveRound();
60
        if ($currentRound === null) {
61
            return;
62
        }
63
64
        $userPirateRound = $this->userPirateRoundRepository->findByUserAndPirateRound(
65
            $user->getId(),
66
            $currentRound->getId()
67
        );
68
69
        if ($userPirateRound === null) {
70
            $userPirateRound = $this->userPirateRoundRepository->prototype();
71
            $userPirateRound->setUser($user);
72
            $userPirateRound->setPirateRound($currentRound);
73
            $userPirateRound->setDestroyedShips(1);
74
            $userPirateRound->setPrestige($prestige);
75
            $this->userPirateRoundRepository->save($userPirateRound);
76
            $this->entityManager->flush();
77
        } else {
78
            $userPirateRound->setDestroyedShips($userPirateRound->getDestroyedShips() + 1);
79
            $userPirateRound->setPrestige($userPirateRound->getPrestige() + $prestige);
80
            $this->userPirateRoundRepository->save($userPirateRound);
81
        }
82
83
84
        $this->logger->logf(
85
            'ADDED user stats for user %d: ships=%d, prestige=%d',
86
            $user->getId(),
87
            $userPirateRound->getDestroyedShips(),
88
            $userPirateRound->getPrestige()
89
        );
90
    }
91
}
92