Passed
Push — dev ( 5b1d95...923b08 )
by Janko
23:33 queued 07:31
created

SpacecraftTick::checkForFinishedAstroMapping()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 56
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 31
nc 5
nop 2
dl 0
loc 56
ccs 0
cts 33
cp 0
crap 72
rs 8.1795
c 2
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\StuLogger;
9
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
10
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...
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\SpacecraftInterface;
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
    #[Override]
30
    public function work(): void
31
    {
32
        foreach ($this->spacecraftRepository->getPlayerSpacecraftsForTick() as $spacecraft) {
33
            $this->workSpacecraft($this->spacecraftWrapperFactory->wrapSpacecraft($spacecraft));
34
        }
35
    }
36
37
    #[Override]
38
    public function workSpacecraft(SpacecraftWrapperInterface $wrapper): void
39
    {
40
        $informationWrapper = $this->informationFactory->createInformationWrapper();
41
42
        $this->workSpacecraftInternal($wrapper, $informationWrapper);
43
44
        $this->sendMessage($wrapper->get(), $informationWrapper);
45
    }
46
47
    private function workSpacecraftInternal(SpacecraftWrapperInterface $wrapper, InformationWrapper $informationWrapper): void
48
    {
49
        $spacecraft = $wrapper->get();
50
51
        $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
            foreach ($this->handlers as $handler) {
56
                $startTime = microtime(true);
57
                $handler->handleSpacecraftTick($wrapper, $informationWrapper);
58
                $this->potentialLog(
59
                    $spacecraft,
60
                    $handler,
61
                    $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
                );
63
            }
64
        } catch (SpacecraftTickFinishedException) {
65
            $this->potentialLog(
66
                $spacecraft,
67
                $handler,
68
                $startTime
69
            );
70
        }
71
72
        $this->spacecraftRepository->save($spacecraft);
73
    }
74
75
    private function potentialLog(SpacecraftInterface $spacecraft, SpacecraftTickHandlerInterface $handler, float $startTime): void
76
    {
77
        $endTime = microtime(true);
78
79
        if (
80
            $endTime - $startTime > 0.01
81
        ) {
82
83
            $classParts = explode('\\', get_class($handler));
84
85
            StuLogger::logf(
86
                "\t\t\t%d: %s, seconds: %F",
87
                $spacecraft->getId(),
88
                end($classParts),
89
                $endTime - $startTime
90
            );
91
        }
92
    }
93
94
    private function sendMessage(SpacecraftInterface $ship, InformationWrapper $informationWrapper): void
95
    {
96
        if ($informationWrapper->isEmpty()) {
97
            return;
98
        }
99
100
        $text = sprintf("Tickreport der %s\n%s", $ship->getName(), $informationWrapper->getInformationsAsString());
101
102
        $this->privateMessageSender->send(
103
            UserEnum::USER_NOONE,
104
            $ship->getUser()->getId(),
105
            $text,
106
            $ship->getType()->getMessageFolderType(),
107
            $ship
108
        );
109
    }
110
}
111