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

UpdatePirateRoundPrestige   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 7.69%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 52
ccs 2
cts 26
cp 0.0769
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B handleSpacecraftDestruction() 0 40 8
1
<?php
2
3
namespace Stu\Module\Spacecraft\Lib\Destruction\Handler;
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 RuntimeException;
7
use Stu\Lib\Information\InformationInterface;
8
use Stu\Lib\Pirate\Component\PirateRoundManagerInterface;
9
use Stu\Module\Logging\LoggerUtilFactoryInterface;
10
use Stu\Module\Logging\PirateLoggerInterface;
11
use Stu\Module\PlayerSetting\Lib\UserEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserEnum 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...
12
use Stu\Module\Spacecraft\Lib\Destruction\SpacecraftDestroyerInterface;
13
use Stu\Module\Spacecraft\Lib\Destruction\SpacecraftDestructionCauseEnum;
14
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
15
use Stu\Orm\Entity\ShipInterface;
16
use Stu\Orm\Repository\UserRepositoryInterface;
17
18
class UpdatePirateRoundPrestige implements SpacecraftDestructionHandlerInterface
19
{
20
    private PirateLoggerInterface $logger;
21
22 1
    public function __construct(
23
        private PirateRoundManagerInterface $pirateRoundManager,
24
        private UserRepositoryInterface $userRepository,
25
        LoggerUtilFactoryInterface $loggerUtilFactory
26
    ) {
27 1
        $this->logger = $loggerUtilFactory->getPirateLogger();
28
    }
29
30
    public function handleSpacecraftDestruction(
31
        ?SpacecraftDestroyerInterface $destroyer,
32
        SpacecraftWrapperInterface $destroyedSpacecraftWrapper,
33
        SpacecraftDestructionCauseEnum $cause,
34
        InformationInterface $informations
35
    ): void {
36
37
        $spacecraft = $destroyedSpacecraftWrapper->get();
38
        $userOfDestroyed = $spacecraft->getUser();
39
40
        if (
41
            $userOfDestroyed->getId() === UserEnum::USER_NPC_KAZON
42
            && $spacecraft instanceof ShipInterface
43
            && $destroyer !== null
44
        ) {
45
            $destroyerUser = $this->userRepository->find($destroyer->getUserId());
46
            if ($destroyerUser === null) {
47
                throw new RuntimeException('this should not happen');
48
            }
49
50
            if ($destroyerUser->isNpc()) {
51
                return;
52
            }
53
54
            $targetPrestige = $spacecraft->getRump()->getPrestige();
55
            if ($targetPrestige < 1) {
56
                return;
57
            }
58
59
            $fleet = $spacecraft->getFleet();
60
            $this->logger->log(sprintf(
61
                '    %s (%s) of fleet %d got destroyed, decreasing pirate round prestige by %d',
62
                $spacecraft->getName(),
63
                $spacecraft->getRump()->getName(),
64
                $fleet === null ? 0 : $fleet->getId(),
65
                $targetPrestige
66
            ));
67
68
            $this->pirateRoundManager->decreasePrestige($targetPrestige);
69
            $this->pirateRoundManager->addUserStats($destroyerUser, $targetPrestige);
70
        }
71
    }
72
}
73