Passed
Push — dev ( 7914d2...bbc331 )
by Janko
10:29
created

CancelColonyBlockOrDefend::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib;
6
7
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...
8
use Stu\Exception\SanityCheckException;
9
use Stu\Lib\Information\InformationInterface;
10
use Stu\Module\Logging\LoggerUtilFactoryInterface;
11
use Stu\Module\Logging\LoggerUtilInterface;
12
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
13
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
14
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...
15
use Stu\Orm\Entity\Ship;
16
use Stu\Orm\Entity\Spacecraft;
17
use Stu\Orm\Repository\FleetRepositoryInterface;
18
19
final class CancelColonyBlockOrDefend implements CancelColonyBlockOrDefendInterface
20
{
21
    private LoggerUtilInterface $loggerUtil;
22
23 2
    public function __construct(
24
        private FleetRepositoryInterface $fleetRepository,
25
        private PrivateMessageSenderInterface $privateMessageSender,
26
        LoggerUtilFactoryInterface $loggerUtilFactory
27
    ) {
28 2
        $this->loggerUtil = $loggerUtilFactory->getLoggerUtil();
29
    }
30
31
    #[Override]
32
    public function work(Spacecraft $spacecraft, InformationInterface $informations, bool $isTraktor = false): void
33
    {
34
        $this->loggerUtil->log('A');
35
        $target = $isTraktor ? $spacecraft->getTractoredShip() : $spacecraft;
36
        $this->loggerUtil->log('B');
37
38
        if ($target === null || !$target instanceof Ship || !$target->isFleetLeader()) {
39
            $this->loggerUtil->log('C');
40
            return;
41
        }
42
        $this->loggerUtil->log('D');
43
44
        $fleet = $target->getFleet();
45
        if ($fleet === null) {
46
            throw new SanityCheckException(sprintf('spacecraftId %d not in fleet', $target->getId()));
47
        }
48
49
        if ($fleet->getDefendedColony() !== null) {
50
            $this->loggerUtil->log('E');
51
            $colony = $fleet->getDefendedColony();
52
53
            if ($isTraktor) {
54
                $this->privateMessageSender->send(
55
                    $spacecraft->getUser()->getId(),
56
                    $target->getUser()->getId(),
57
                    sprintf(
58
                        _('Die %s wurde mit dem Traktorstrahl gezogen, daher hat die Flotte %s die Verteidigung der Kolonie %s eingestellt'),
59
                        $target->getName(),
60
                        $fleet->getName(),
61
                        $colony->getName()
62
                    ),
63
                    PrivateMessageFolderTypeEnum::SPECIAL_SHIP
64
                );
65
            }
66
            $this->privateMessageSender->send(
67
                UserEnum::USER_NOONE,
68
                $colony->getUser()->getId(),
69
                sprintf(
70
                    _('Die Flotte %s hat von Spieler %s hat die Verteidigung der Kolonie %s aufgehoben'),
71
                    $fleet->getName(),
72
                    $fleet->getUser()->getName(),
73
                    $colony->getName()
74
                ),
75
                PrivateMessageFolderTypeEnum::SPECIAL_COLONY
76
            );
77
78
            $informations->addInformation(sprintf(
79
                _('Die Flotte der %s hat die Verteidigung der Kolonie %s abgebrochen'),
80
                $target->getName(),
81
                $colony->getName()
82
            ));
83
            $fleet->setDefendedColony(null);
84
            $this->fleetRepository->save($fleet);
85
        }
86
87
        if ($fleet->getBlockedColony() !== null) {
88
            $this->loggerUtil->log('F');
89
            $colony = $fleet->getBlockedColony();
90
91
            if ($isTraktor) {
92
                $this->privateMessageSender->send(
93
                    $spacecraft->getUser()->getId(),
94
                    $target->getUser()->getId(),
95
                    sprintf(
96
                        _('Die %s wurde mit dem Traktorstrahl gezogen, daher hat die Flotte %s die Blockade der Kolonie %s eingestellt'),
97
                        $target->getName(),
98
                        $fleet->getName(),
99
                        $colony->getName()
100
                    ),
101
                    PrivateMessageFolderTypeEnum::SPECIAL_SHIP
102
                );
103
            }
104
105
            $this->privateMessageSender->send(
106
                UserEnum::USER_NOONE,
107
                $colony->getUser()->getId(),
108
                sprintf(
109
                    _('Die Flotte %s hat von Spieler %s hat die Blockade der Kolonie %s aufgehoben'),
110
                    $fleet->getName(),
111
                    $fleet->getUser()->getName(),
112
                    $colony->getName()
113
                ),
114
                PrivateMessageFolderTypeEnum::SPECIAL_COLONY
115
            );
116
            $informations->addInformation(sprintf(
117
                _('Die Flotte der %s hat die Blockade der Kolonie %s abgebrochen'),
118
                $target->getName(),
119
                $colony->getName()
120
            ));
121
            $fleet->setBlockedColony(null);
122
            $this->fleetRepository->save($fleet);
123
        }
124
    }
125
}
126