Test Failed
Push — dev ( 985fdf...79dec8 )
by Nico
14:06
created

TakeBuoy::handle()   B

Complexity

Conditions 10
Paths 11

Size

Total Lines 70
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 46
c 1
b 0
f 0
nc 11
nop 1
dl 0
loc 70
rs 7.3115

How to fix   Long Method    Complexity   

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\Action\TakeBuoy;
6
7
use request;
8
use Stu\Module\Control\ActionControllerInterface;
9
use Stu\Module\Control\GameControllerInterface;
10
use Stu\Orm\Repository\BuoyRepositoryInterface;
11
use Stu\Module\Ship\Lib\ShipLoaderInterface;
12
use Stu\Module\Ship\View\ShowShip\ShowShip;
13
use Stu\Component\Ship\Storage\ShipStorageManagerInterface;
14
use Stu\Module\Message\Lib\PrivateMessageFolderSpecialEnum;
15
use Stu\Orm\Repository\CommodityRepositoryInterface;
16
use Stu\Module\Commodity\CommodityTypeEnum;
17
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
18
19
20
21
final class TakeBuoy implements ActionControllerInterface
22
{
23
    public const ACTION_IDENTIFIER = 'B_TAKE_BUOY';
24
25
    private ShipLoaderInterface $shipLoader;
26
    private BuoyRepositoryInterface $buoyRepository;
27
    private CommodityRepositoryInterface $commodityRepository;
28
    private ShipStorageManagerInterface $shipStorageManager;
29
    private PrivateMessageSenderInterface $privateMessageSender;
30
31
    public function __construct(
32
        ShipLoaderInterface $shipLoader,
33
        BuoyRepositoryInterface $buoyRepository,
34
        CommodityRepositoryInterface $commodityRepository,
35
        ShipStorageManagerInterface $shipStorageManager,
36
        PrivateMessageSenderInterface $privateMessageSender
37
    ) {
38
        $this->shipLoader = $shipLoader;
39
        $this->buoyRepository = $buoyRepository;
40
        $this->commodityRepository = $commodityRepository;
41
        $this->shipStorageManager = $shipStorageManager;
42
        $this->privateMessageSender = $privateMessageSender;
43
    }
44
45
    public function handle(GameControllerInterface $game): void
46
    {
47
        $game->setView(ShowShip::VIEW_IDENTIFIER);
48
        $userId = $game->getUser()->getId();
49
        $wrapper = $this->shipLoader->getWrapperByIdAndUser(
50
            request::indInt('id'),
51
            $userId
52
        );
53
        $ship = $wrapper->get();
54
        $buoyId = request::indInt('buoy_id');
55
        $epsSystem = $wrapper->getEpsSystemData();
56
57
        if ($epsSystem === null || $epsSystem->getEps() == 0) {
58
            $game->addInformation(_("Keine Energie vorhanden"));
59
            return;
60
        }
61
        if ($ship->getCloakState()) {
62
            $game->addInformation(_("Die Tarnung ist aktiviert"));
63
            return;
64
        }
65
        if ($ship->getWarpState()) {
66
            $game->addInformation(_("Der Warpantrieb ist aktiviert"));
67
            return;
68
        }
69
        if ($ship->getShieldState()) {
70
            $game->addInformation(_("Die Schilde sind aktiviert"));
71
            return;
72
        }
73
74
        if ($epsSystem->getEps() < 1) {
75
            $game->addInformation(_('Es wird 1 Energie für den Start der Boje benötigt'));
76
            return;
77
        }
78
79
        $commodity = $this->commodityRepository->find(CommodityTypeEnum::BASE_ID_BOUY);
80
81
        if ($commodity !== null) {
82
            $this->shipStorageManager->upperStorage(
83
                $ship,
84
                $commodity,
85
                1
86
            );
87
        }
88
89
        $buoy = $this->buoyRepository->find($buoyId);
90
        if ($buoy === null) {
91
            $game->addInformation(_("Die Boje existiert nicht"));
92
            return;
93
        }
94
95
        if ($buoy->getUserId() !== $userId) {
96
            $this->privateMessageSender->send(
97
                $game->getUser()->getId(),
98
                $buoy->getUserId(),
99
                sprintf(
100
                    _('Deine Boje %s wurde von der %s bei %s aufgebracht.'),
101
                    $buoy->getText(),
102
                    $ship->getName(),
103
                    $ship->getSectorString()
104
                ),
105
                PrivateMessageFolderSpecialEnum::PM_SPECIAL_SHIP,
106
                null
107
            );
108
        }
109
110
        $this->buoyRepository->delete($buoy);
111
112
        $epsSystem->lowerEps(1)->update();
113
114
        $game->addInformation(_('Die Boje wurde erfolgreich eingesammelt'));
115
    }
116
117
    public function performSessionCheck(): bool
118
    {
119
        return true;
120
    }
121
}