Passed
Push — dev ( 3cf0b9...747908 )
by Janko
06:05 queued 31s
created

CancelColonyBlockOrDefend::__construct()   A

Complexity

Conditions 1
Paths 1

Size

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