Passed
Push — dev ( 0dbbcc...f29cfd )
by Janko
10:15
created

SpacecraftCorrector::sendPmToOwner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 2
dl 0
loc 13
ccs 0
cts 12
cp 0
crap 2
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Spacecraft\Lib\Creation;
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\Module\Logging\StuLogger;
7
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
8
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
9
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...
10
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperFactoryInterface;
11
use Stu\Orm\Entity\SpacecraftBuildplanInterface;
12
use Stu\Orm\Entity\SpacecraftInterface;
13
use Stu\Orm\Repository\ConstructionProgressRepositoryInterface;
14
use Stu\Orm\Repository\SpacecraftBuildplanRepositoryInterface;
15
16
class SpacecraftCorrector implements SpacecraftCorrectorInterface
17
{
18 1
    public function __construct(
19
        private SpacecraftBuildplanRepositoryInterface $spacecraftBuildplanRepository,
20
        private ConstructionProgressRepositoryInterface $constructionProgressRepository,
21
        private SpacecraftWrapperFactoryInterface $spacecraftWrapperFactory,
22
        private PrivateMessageSenderInterface $privateMessageSender
23 1
    ) {}
24
25
    #[Override]
26
    public function correctAllSpacecrafts(): void
27
    {
28
        $count = 0;
29
30
        // correct all ships
31
        foreach ($this->spacecraftBuildplanRepository->getAllNonNpcBuildplans() as $buildplan) {
32
33
            $spacecrafts = $buildplan->getSpacecraftList();
34
            if ($spacecrafts->isEmpty()) {
35
                continue;
36
            }
37
38
            foreach ($spacecrafts as $spacecraft) {
39
                if ($this->correctSpacecraft($spacecraft, $buildplan)) {
40
                    $count++;
41
                }
42
            }
43
        }
44
45
        // correct all stations
46
        foreach ($this->constructionProgressRepository->findAll() as $progress) {
47
48
            $station = $progress->getStation();
49
            $buildplan = $station->getBuildplan();
50
            if (
51
                $buildplan === null
52
                || $progress->getRemainingTicks() !== 0
53
            ) {
54
                continue;
55
            }
56
57
            if ($this->correctSpacecraft(
58
                $progress->getStation(),
59
                $buildplan
60
            )) {
61
                $count++;
62
            }
63
        }
64
65
        if ($count > 0) {
66
            StuLogger::logf('corrected %d spacecrafts.', $count);
67
        }
68
    }
69
70
    private function correctSpacecraft(SpacecraftInterface $spacecraft, SpacecraftBuildplanInterface $buildplan): bool
71
    {
72
        $rump = $buildplan->getRump();
73
        $wrapper = $this->spacecraftWrapperFactory->wrapSpacecraft($spacecraft);
74
        $toStringBefore = $wrapper->__toString();
75
76
        foreach ($buildplan->getModules() as $buildplanModule) {
77
            $moduleType = $buildplanModule->getModuleType();
78
            $moduleRumpWrapper = $moduleType->getModuleRumpWrapperCallable()($rump, $buildplan);
79
            $moduleRumpWrapper->apply($wrapper);
80
        }
81
82
        $toStringAfter = $wrapper->__toString();
83
        $diff = xdiff_string_diff($toStringBefore, $toStringAfter);
84
85
        if ($diff !== '') {
86
            StuLogger::logf(
87
                'spacecraftId %d corrected: %s',
88
                $spacecraft->getId(),
89
                $diff
0 ignored issues
show
Bug introduced by
It seems like $diff can also be of type false; however, parameter $args of Stu\Module\Logging\StuLogger::logf() does only seem to accept double|integer|string, 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

89
                /** @scrutinizer ignore-type */ $diff
Loading history...
90
            );
91
92
            $this->sendPmToOwner($spacecraft, $diff);
0 ignored issues
show
Bug introduced by
It seems like $diff can also be of type false; however, parameter $diff of Stu\Module\Spacecraft\Li...rector::sendPmToOwner() does only seem to accept string, 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

92
            $this->sendPmToOwner($spacecraft, /** @scrutinizer ignore-type */ $diff);
Loading history...
93
94
            return true;
95
        }
96
97
        return false;
98
    }
99
100
    private function sendPmToOwner(SpacecraftInterface $spacecraft, string $diff): void
101
    {
102
        $this->privateMessageSender
103
            ->send(
104
                UserEnum::USER_NOONE,
105
                $spacecraft->getUser()->getId(),
106
                sprintf(
107
                    "Die Werte der %s wurden automatisch wie folgt korrigiert:\n\n%s",
108
                    $spacecraft->getName(),
109
                    $diff
110
                ),
111
                PrivateMessageFolderTypeEnum::SPECIAL_SYSTEM,
112
                $spacecraft->getHref()
113
            );
114
    }
115
}
116