Passed
Push — master ( 288b46...98b0e3 )
by Nico
31:30 queued 08:00
created

CancelColonyBlockOrDefend   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 5.06%

Importance

Changes 0
Metric Value
eloc 71
c 0
b 0
f 0
dl 0
loc 106
ccs 4
cts 79
cp 0.0506
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
B work() 0 88 8
A __construct() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib;
6
7
use Stu\Lib\InformationWrapper;
8
use Stu\Module\Logging\LoggerUtilFactoryInterface;
9
use Stu\Module\Logging\LoggerUtilInterface;
10
use Stu\Module\Message\Lib\PrivateMessageFolderSpecialEnum;
11
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
12
use Stu\Module\PlayerSetting\Lib\UserEnum;
13
use Stu\Orm\Entity\ShipInterface;
14
use Stu\Orm\Repository\FleetRepositoryInterface;
15
16
final class CancelColonyBlockOrDefend implements CancelColonyBlockOrDefendInterface
17
{
18
    private FleetRepositoryInterface $fleetRepository;
19
20
    private PrivateMessageSenderInterface $privateMessageSender;
21
22
    private LoggerUtilInterface $loggerUtil;
23
24 2
    public function __construct(
25
        FleetRepositoryInterface $fleetRepository,
26
        PrivateMessageSenderInterface $privateMessageSender,
27
        LoggerUtilFactoryInterface $loggerUtilFactory
28
    ) {
29 2
        $this->fleetRepository = $fleetRepository;
30 2
        $this->privateMessageSender = $privateMessageSender;
31 2
        $this->loggerUtil = $loggerUtilFactory->getLoggerUtil();
32
    }
33
34
    public function work(ShipInterface $ship, InformationWrapper $informations, bool $isTraktor = false): void
35
    {
36
        $this->loggerUtil->log('A');
37
        $target = $isTraktor ? $ship->getTractoredShip() : $ship;
38
        $this->loggerUtil->log('B');
39
40
        if ($target === null || !$target->isFleetLeader()) {
41
            $this->loggerUtil->log('C');
42
            return;
43
        }
44
        $this->loggerUtil->log('D');
45
46
        $fleet = $target->getFleet();
47
48
        if ($fleet->getDefendedColony() !== null) {
49
            $this->loggerUtil->log('E');
50
            $colony = $fleet->getDefendedColony();
51
52
            if ($isTraktor) {
53
                $this->privateMessageSender->send(
54
                    $ship->getUser()->getId(),
55
                    $target->getUser()->getId(),
56
                    sprintf(
57
                        _('Die %s wurde mit dem Traktorstrahl gezogen, daher hat die Flotte %s die Verteidigung der Kolonie %s eingestellt'),
58
                        $target->getName(),
59
                        $fleet->getName(),
60
                        $colony->getName()
61
                    ),
62
                    PrivateMessageFolderSpecialEnum::PM_SPECIAL_SHIP
63
                );
64
            }
65
            $this->privateMessageSender->send(
66
                UserEnum::USER_NOONE,
67
                $colony->getUser()->getId(),
68
                sprintf(
69
                    _('Die Flotte %s hat von Spieler %s hat die Verteidigung der Kolonie %s aufgehoben'),
70
                    $fleet->getName(),
71
                    $fleet->getUser()->getName(),
72
                    $colony->getName()
73
                ),
74
                PrivateMessageFolderSpecialEnum::PM_SPECIAL_COLONY
75
            );
76
77
            $informations->addInformation(sprintf(
78
                _('Die Flotte der %s hat die Verteidigung der Kolonie %s abgebrochen'),
79
                $target->getName(),
80
                $colony->getName()
81
            ));
82
            $fleet->setDefendedColony(null);
83
            $this->fleetRepository->save($fleet);
0 ignored issues
show
Bug introduced by
It seems like $fleet can also be of type null; however, parameter $fleet of Stu\Orm\Repository\Fleet...sitoryInterface::save() does only seem to accept Stu\Orm\Entity\FleetInterface, 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

83
            $this->fleetRepository->save(/** @scrutinizer ignore-type */ $fleet);
Loading history...
84
        }
85
86
        if ($fleet->getBlockedColony() !== null) {
87
            $this->loggerUtil->log('F');
88
            $colony = $fleet->getBlockedColony();
89
90
            if ($isTraktor) {
91
                $this->privateMessageSender->send(
92
                    $ship->getUser()->getId(),
93
                    $target->getUser()->getId(),
94
                    sprintf(
95
                        _('Die %s wurde mit dem Traktorstrahl gezogen, daher hat die Flotte %s die Blockade der Kolonie %s eingestellt'),
96
                        $target->getName(),
97
                        $fleet->getName(),
98
                        $colony->getName()
99
                    ),
100
                    PrivateMessageFolderSpecialEnum::PM_SPECIAL_SHIP
101
                );
102
            }
103
104
            $this->privateMessageSender->send(
105
                UserEnum::USER_NOONE,
106
                $colony->getUser()->getId(),
107
                sprintf(
108
                    _('Die Flotte %s hat von Spieler %s hat die Blockade der Kolonie %s aufgehoben'),
109
                    $fleet->getName(),
110
                    $fleet->getUser()->getName(),
111
                    $colony->getName()
112
                ),
113
                PrivateMessageFolderSpecialEnum::PM_SPECIAL_COLONY
114
            );
115
            $informations->addInformation(sprintf(
116
                _('Die Flotte der %s hat die Blockade der Kolonie %s abgebrochen'),
117
                $target->getName(),
118
                $colony->getName()
119
            ));
120
            $fleet->setBlockedColony(null);
121
            $this->fleetRepository->save($fleet);
122
        }
123
    }
124
}
125