Passed
Pull Request — dev (#2264)
by
unknown
05:09
created

SpacecraftTick::work()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Tick\Spacecraft;
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\Lib\Information\InformationFactoryInterface;
7
use Stu\Lib\Information\InformationWrapper;
8
use Stu\Module\Logging\LogTypeEnum;
9
use Stu\Module\Logging\StuLogger;
10
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
11
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...
12
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperFactoryInterface;
13
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
14
use Stu\Module\Tick\Spacecraft\Handler\SpacecraftTickHandlerInterface;
15
use Stu\Module\Tick\Spacecraft\ManagerComponent\ManagerComponentInterface;
16
use Stu\Orm\Entity\Spacecraft;
17
use Stu\Orm\Repository\SpacecraftRepositoryInterface;
18
19
final class SpacecraftTick implements SpacecraftTickInterface, ManagerComponentInterface
20
{
21
    /** @param array<SpacecraftTickHandlerInterface> $handlers */
22 1
    public function __construct(
23
        private readonly SpacecraftWrapperFactoryInterface $spacecraftWrapperFactory,
24
        private readonly PrivateMessageSenderInterface $privateMessageSender,
25
        private readonly SpacecraftRepositoryInterface $spacecraftRepository,
26
        private readonly InformationFactoryInterface $informationFactory,
27
        private readonly array $handlers
28 1
    ) {}
29
30 1
    #[Override]
31
    public function work(): void
32
    {
33 1
        foreach ($this->spacecraftRepository->getPlayerSpacecraftsForTick() as $spacecraft) {
34 1
            $this->workSpacecraft($this->spacecraftWrapperFactory->wrapSpacecraft($spacecraft));
35
        }
36
    }
37
38 1
    #[Override]
39
    public function workSpacecraft(SpacecraftWrapperInterface $wrapper): void
40
    {
41 1
        $informationWrapper = $this->informationFactory->createInformationWrapper();
42
43 1
        $this->workSpacecraftInternal($wrapper, $informationWrapper);
44
45 1
        $this->sendMessage($wrapper->get(), $informationWrapper);
46
    }
47
48 1
    private function workSpacecraftInternal(SpacecraftWrapperInterface $wrapper, InformationWrapper $informationWrapper): void
49
    {
50 1
        $spacecraft = $wrapper->get();
51
52 1
        $startTime = microtime(true);
0 ignored issues
show
Unused Code introduced by
The assignment to $startTime is dead and can be removed.
Loading history...
53
54
        try {
55
56 1
            foreach ($this->handlers as $handler) {
57 1
                $startTime = microtime(true);
58 1
                $handler->handleSpacecraftTick($wrapper, $informationWrapper);
59 1
                $this->potentialLog(
60 1
                    $spacecraft,
61 1
                    $handler,
62 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

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