Passed
Pull Request — dev (#2304)
by
unknown
05:16
created

SpacecraftTick   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 85.11%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 11
eloc 43
c 3
b 2
f 0
dl 0
loc 90
ccs 40
cts 47
cp 0.8511
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A work() 0 5 2
A workSpacecraftInternal() 0 26 3
A workSpacecraft() 0 8 1
A potentialLog() 0 16 2
A sendMessage() 0 14 2
1
<?php
2
3
namespace Stu\Module\Tick\Spacecraft;
4
5
use Stu\Lib\Information\InformationFactoryInterface;
6
use Stu\Lib\Information\InformationWrapper;
7
use Stu\Module\Logging\LogTypeEnum;
8
use Stu\Module\Logging\StuLogger;
9
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
10
use Stu\Module\PlayerSetting\Lib\UserConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserConstants 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...
11
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperFactoryInterface;
12
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
13
use Stu\Module\Tick\Spacecraft\Handler\SpacecraftTickHandlerInterface;
14
use Stu\Module\Tick\Spacecraft\ManagerComponent\ManagerComponentInterface;
15
use Stu\Orm\Entity\Spacecraft;
16
use Stu\Orm\Repository\SpacecraftRepositoryInterface;
17
18
final class SpacecraftTick implements SpacecraftTickInterface, ManagerComponentInterface
19
{
20
    /** @param array<SpacecraftTickHandlerInterface> $handlers */
21 1
    public function __construct(
22
        private readonly SpacecraftWrapperFactoryInterface $spacecraftWrapperFactory,
23
        private readonly PrivateMessageSenderInterface $privateMessageSender,
24
        private readonly SpacecraftRepositoryInterface $spacecraftRepository,
25
        private readonly InformationFactoryInterface $informationFactory,
26
        private readonly array $handlers
27 1
    ) {}
28
29 1
    #[\Override]
30
    public function work(): void
31
    {
32 1
        foreach ($this->spacecraftRepository->getPlayerSpacecraftsForTick() as $spacecraft) {
33 1
            $this->workSpacecraft($this->spacecraftWrapperFactory->wrapSpacecraft($spacecraft));
34
        }
35
    }
36
37 1
    #[\Override]
38
    public function workSpacecraft(SpacecraftWrapperInterface $wrapper): void
39
    {
40 1
        $informationWrapper = $this->informationFactory->createInformationWrapper();
41
42 1
        $this->workSpacecraftInternal($wrapper, $informationWrapper);
43
44 1
        $this->sendMessage($wrapper->get(), $informationWrapper);
45
    }
46
47 1
    private function workSpacecraftInternal(SpacecraftWrapperInterface $wrapper, InformationWrapper $informationWrapper): void
48
    {
49 1
        $spacecraft = $wrapper->get();
50
51 1
        $startTime = microtime(true);
0 ignored issues
show
Unused Code introduced by
The assignment to $startTime is dead and can be removed.
Loading history...
52
53
        try {
54
55 1
            foreach ($this->handlers as $handler) {
56 1
                $startTime = microtime(true);
57 1
                $handler->handleSpacecraftTick($wrapper, $informationWrapper);
58 1
                $this->potentialLog(
59 1
                    $spacecraft,
60 1
                    $handler,
61 1
                    $startTime
0 ignored issues
show
Bug introduced by
It seems like $startTime can also be of type string; however, parameter $startTime of Stu\Module\Tick\Spacecra...aftTick::potentialLog() does only seem to accept double, 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

61
                    /** @scrutinizer ignore-type */ $startTime
Loading history...
62 1
                );
63
            }
64 1
        } catch (SpacecraftTickFinishedException) {
65 1
            $this->potentialLog(
66 1
                $spacecraft,
67 1
                $handler,
68 1
                $startTime
69 1
            );
70
        }
71
72 1
        $this->spacecraftRepository->save($spacecraft);
73
    }
74
75 1
    private function potentialLog(Spacecraft $spacecraft, SpacecraftTickHandlerInterface $handler, float $startTime): void
76
    {
77 1
        $endTime = microtime(true);
78
79
        if (
80 1
            $endTime - $startTime > 0.1
81
        ) {
82
83
            $classParts = explode('\\', get_class($handler));
84
85
            StuLogger::log(sprintf(
86
                "\t\t\t%d: %s, seconds: %F",
87
                $spacecraft->getId(),
88
                end($classParts),
89
                $endTime - $startTime
90
            ), LogTypeEnum::TICK);
91
        }
92
    }
93
94 1
    private function sendMessage(Spacecraft $ship, InformationWrapper $informationWrapper): void
95
    {
96 1
        if ($informationWrapper->isEmpty()) {
97 1
            return;
98
        }
99
100 1
        $text = sprintf("Tickreport der %s\n%s", $ship->getName(), $informationWrapper->getInformationsAsString());
101
102 1
        $this->privateMessageSender->send(
103 1
            UserConstants::USER_NOONE,
104 1
            $ship->getUser()->getId(),
105 1
            $text,
106 1
            $ship->getType()->getMessageFolderType(),
107 1
            $ship
108 1
        );
109
    }
110
}
111