Passed
Pull Request — master (#1933)
by Janko
21:56 queued 11:22
created

InterceptShipCore::intercept()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 51
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 6.288

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 31
nc 12
nop 3
dl 0
loc 51
ccs 24
cts 30
cp 0.8
crap 6.288
rs 8.8017
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib\Interaction;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ORM\EntityManagerInterface;
10
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...
11
use Stu\Component\Ship\System\Exception\AlreadyOffException;
12
use Stu\Component\Ship\System\ShipSystemManagerInterface;
13
use Stu\Component\Ship\System\ShipSystemTypeEnum;
14
use Stu\Lib\Information\InformationInterface;
15
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
16
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
17
use Stu\Module\Ship\Lib\Battle\AlertDetection\AlertReactionFacadeInterface;
18
use Stu\Module\Ship\Lib\ShipWrapperInterface;
19
use Stu\Module\Ship\View\ShowShip\ShowShip;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Ship\View\ShowShip\ShowShip 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...
20
use Stu\Orm\Repository\ShipRepositoryInterface;
21
22
final class InterceptShipCore implements InterceptShipCoreInterface
23
{
24 1
    public function __construct(
25
        private ShipRepositoryInterface $shipRepository,
26
        private ShipSystemManagerInterface $shipSystemManager,
27
        private AlertReactionFacadeInterface $alertReactionFacade,
28
        private PrivateMessageSenderInterface $privateMessageSender,
29
        private EntityManagerInterface $entityManager
30 1
    ) {}
31
32 1
    #[Override]
33
    public function intercept(
34
        ShipWrapperInterface $wrapper,
35
        ShipWrapperInterface $targetWrapper,
36
        InformationInterface $informations
37
    ): void {
38
39 1
        $ship = $wrapper->get();
40 1
        $target = $targetWrapper->get();
41 1
        $userId = $ship->getUser()->getId();
42
43 1
        $wrappersToToggleAlertReaction = new ArrayCollection([$targetWrapper]);
44
45 1
        $targetFleetWrapper = $targetWrapper->getFleetWrapper();
46 1
        if ($targetFleetWrapper !== null) {
47
            foreach ($targetFleetWrapper->getShipWrappers() as $fleetWrapper) {
48
                $this->deactivateWarpdrive($fleetWrapper, $wrappersToToggleAlertReaction);
49
            }
50
51
            $informations->addInformationf("Die Flotte %s wurde abgefangen", $targetFleetWrapper->get()->getName());
52
            $pm = "Die Flotte " . $targetFleetWrapper->get()->getName() . " wurde von der " . $ship->getName() . " abgefangen";
53
        } else {
54 1
            $this->deactivateWarpdrive($targetWrapper, $wrappersToToggleAlertReaction);
55
56 1
            $informations->addInformationf("Die %s wurde abgefangen", $target->getName());
57 1
            $pm = "Die " . $target->getName() . " wurde von der " . $ship->getName() . " abgefangen";
58
        }
59
60 1
        $href = sprintf('ship.php?%s=1&id=%d', ShowShip::VIEW_IDENTIFIER, $target->getId());
61
62 1
        $this->privateMessageSender->send(
63 1
            $userId,
64 1
            $target->getUser()->getId(),
65 1
            $pm,
66 1
            PrivateMessageFolderTypeEnum::SPECIAL_SHIP,
67 1
            $href
68 1
        );
69
70 1
        $fleetWrapper = $wrapper->getFleetWrapper();
71 1
        if ($fleetWrapper !== null) {
72
            foreach ($fleetWrapper->getShipWrappers() as $fleetWrapper) {
73
                $this->deactivateWarpdrive($fleetWrapper, $wrappersToToggleAlertReaction);
74
            }
75
        } else {
76 1
            $this->deactivateWarpdrive($wrapper, $wrappersToToggleAlertReaction, true);
77
        }
78 1
        $this->entityManager->flush();
79
80
        // alert reaction check
81 1
        foreach ($wrappersToToggleAlertReaction as $wrapper) {
82 1
            $this->alertReactionFacade->doItAll($wrapper, $informations);
83
        }
84
    }
85
86
    /** @param ArrayCollection<int, ShipWrapperInterface> $wrappersToToggleAlertReaction */
87 1
    private function deactivateWarpdrive(ShipWrapperInterface $wrapper, Collection $wrappersToToggleAlertReaction, bool $addSelfAsToggle = false): void
88
    {
89
        try {
90 1
            $this->shipSystemManager->deactivate($wrapper, ShipSystemTypeEnum::SYSTEM_WARPDRIVE);
91 1
            $this->shipRepository->save($wrapper->get());
92
93 1
            $tractoredWrapper = $wrapper->getTractoredShipWrapper();
94 1
            if ($tractoredWrapper !== null) {
95 1
                $wrappersToToggleAlertReaction->add($tractoredWrapper);
96
            }
97 1
            if ($addSelfAsToggle) {
98 1
                $wrappersToToggleAlertReaction->add($wrapper);
99
            }
100
        } catch (AlreadyOffException) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
101
        }
102
    }
103
}
104