Passed
Push — master ( aa3b71...5e2219 )
by Nico
55:07 queued 26:20
created

InterceptShipCore::performSessionCheck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib\Interaction;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Stu\Component\Ship\System\Exception\AlreadyOffException;
9
use Stu\Component\Ship\System\ShipSystemManagerInterface;
10
use Stu\Component\Ship\System\ShipSystemTypeEnum;
11
use Stu\Lib\Information\InformationInterface;
12
use Stu\Module\Message\Lib\PrivateMessageFolderSpecialEnum;
13
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
14
use Stu\Module\Ship\Lib\Battle\AlertRedHelperInterface;
15
use Stu\Module\Ship\Lib\ShipWrapperFactoryInterface;
16
use Stu\Module\Ship\View\ShowShip\ShowShip;
17
use Stu\Orm\Entity\ShipInterface;
18
use Stu\Orm\Repository\ShipRepositoryInterface;
19
20
final class InterceptShipCore implements InterceptShipCoreInterface
21
{
22
    public function __construct(
23
        private ShipRepositoryInterface $shipRepository,
24
        private PrivateMessageSenderInterface $privateMessageSender,
25
        private ShipSystemManagerInterface $shipSystemManager,
26
        private AlertRedHelperInterface $alertRedHelper,
27
        private ShipWrapperFactoryInterface $shipWrapperFactory,
28
        private EntityManagerInterface $entityManager
29
    ) {
30
    }
31
32
    public function intercept(ShipInterface $ship, ShipInterface $target, InformationInterface $informations): void
33
    {
34
        $userId = $ship->getUser()->getId();
35
        $wrapper = $this->shipWrapperFactory->wrapShip($ship);
36
        $targetWrapper = $this->shipWrapperFactory->wrapShip($target);
37
38
        if ($ship->getDockedTo() !== null) {
39
            $informations->addInformation('Das Schiff hat abgedockt');
40
            $ship->setDockedTo(null);
41
        }
42
        if ($target->getFleet() !== null) {
43
            foreach ($target->getFleet()->getShips() as $fleetShip) {
44
                try {
45
                    $this->shipSystemManager->deactivate($this->shipWrapperFactory->wrapShip($fleetShip), ShipSystemTypeEnum::SYSTEM_WARPDRIVE);
46
                } catch (AlreadyOffException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
47
                }
48
                $this->shipRepository->save($fleetShip);
49
            }
50
51
            $informations->addInformationf("Die Flotte %s wurde abgefangen", $target->getFleet()->getName());
52
            $pm = "Die Flotte " . $target->getFleet()->getName() . " wurde von der " . $ship->getName() . " abgefangen";
53
        } else {
54
            $this->shipSystemManager->deactivate($targetWrapper, ShipSystemTypeEnum::SYSTEM_WARPDRIVE);
55
56
            $informations->addInformationf("Die %s wurde abgefangen", $target->getName());
57
            $pm = "Die " . $target->getName() . " wurde von der " . $ship->getName() . " abgefangen";
58
59
            $this->shipRepository->save($target);
60
        }
61
62
        $href = sprintf('ship.php?%s=1&id=%d', ShowShip::VIEW_IDENTIFIER, $target->getId());
63
64
        $this->privateMessageSender->send(
65
            $userId,
66
            $target->getUser()->getId(),
67
            $pm,
68
            PrivateMessageFolderSpecialEnum::PM_SPECIAL_SHIP,
69
            $href
70
        );
71
        $interceptorLeftWarp = false;
72
        if ($ship->getFleet() !== null) {
73
            foreach ($ship->getFleet()->getShips() as $fleetShip) {
74
                try {
75
                    $this->shipSystemManager->deactivate($this->shipWrapperFactory->wrapShip($fleetShip), ShipSystemTypeEnum::SYSTEM_WARPDRIVE);
76
                    $interceptorLeftWarp = true;
77
                } catch (AlreadyOffException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
78
                }
79
                $this->shipRepository->save($fleetShip);
80
            }
81
        } else {
82
            try {
83
                $this->shipSystemManager->deactivate($wrapper, ShipSystemTypeEnum::SYSTEM_WARPDRIVE);
84
                $interceptorLeftWarp = true;
85
            } catch (AlreadyOffException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
86
            }
87
88
            $this->shipRepository->save($ship);
89
        }
90
        $this->entityManager->flush();
91
92
        //Alert red check for the target(s)
93
        $this->alertRedHelper->doItAll($target, $informations);
94
95
        //Alert red check for the interceptor(s)
96
        if ($interceptorLeftWarp) {
97
            $this->alertRedHelper->doItAll($ship, $informations);
98
        }
99
    }
100
101
    public function performSessionCheck(): bool
102
    {
103
        return true;
104
    }
105
}
106