Test Failed
Pull Request — master (#1764)
by Nico
12:03
created

DropBuoy::handle()   C

Complexity

Conditions 16
Paths 14

Size

Total Lines 87
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 55
c 1
b 0
f 0
nc 14
nop 1
dl 0
loc 87
rs 5.5666

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\DropBuoy;
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\Component\Ship\System\ShipSystemTypeEnum;
15
use Stu\Orm\Repository\CommodityRepositoryInterface;
16
use Stu\Module\Commodity\CommodityTypeEnum;
17
18
19
20
final class DropBuoy implements ActionControllerInterface
21
{
22
    public const ACTION_IDENTIFIER = 'B_DROP_BOUY';
23
24
    private ShipLoaderInterface $shipLoader;
25
    private BuoyRepositoryInterface $buoyRepository;
26
    private CommodityRepositoryInterface $commodityRepository;
27
    private ShipStorageManagerInterface $shipStorageManager;
28
29
    public function __construct(
30
        ShipLoaderInterface $shipLoader,
31
        BuoyRepositoryInterface $buoyRepository,
32
        CommodityRepositoryInterface $commodityRepository,
33
        ShipStorageManagerInterface $shipStorageManager
34
    ) {
35
        $this->shipLoader = $shipLoader;
36
        $this->buoyRepository = $buoyRepository;
37
        $this->commodityRepository = $commodityRepository;
38
        $this->shipStorageManager = $shipStorageManager;
39
    }
40
41
    public function handle(GameControllerInterface $game): void
42
    {
43
        $game->setView(ShowShip::VIEW_IDENTIFIER);
44
        $userId = $game->getUser()->getId();
45
        $wrapper = $this->shipLoader->getWrapperByIdAndUser(
46
            request::indInt('id'),
47
            $userId
48
        );
49
        $ship = $wrapper->get();
50
51
        if (!$ship->isSystemHealthy(ShipSystemTypeEnum::SYSTEM_TORPEDO)) {
52
            $game->addInformation(_("Die Torpedorampe ist zerstört"));
53
            return;
54
        }
55
        $epsSystem = $wrapper->getEpsSystemData();
56
        if ($epsSystem === null || $epsSystem->getEps() == 0) {
57
            $game->addInformation(_("Keine Energie vorhanden"));
58
            return;
59
        }
60
        if ($ship->getCloakState()) {
61
            $game->addInformation(_("Die Tarnung ist aktiviert"));
62
            return;
63
        }
64
        if ($ship->getWarpState()) {
65
            $game->addInformation(_("Der Warpantrieb ist aktiviert"));
66
            return;
67
        }
68
        if ($ship->getShieldState()) {
69
            $game->addInformation(_("Die Schilde sind aktiviert"));
70
            return;
71
        }
72
        if (count($this->buoyRepository->findByUserId($userId)) >= 16) {
73
            $game->addInformation(_("Es können nicht mehr als 16 Bojen platziert werden"));
74
            return;
75
        }
76
77
        $text = request::postString('text');
78
79
        if ($text === false || mb_strlen($text) > 60) {
0 ignored issues
show
introduced by
The condition $text === false is always true.
Loading history...
80
            $game->addInformation(_("Der Text darf nicht länger als 60 Zeichen sein"));
81
            return;
82
        }
83
84
        if (empty($text)) {
85
            $game->addInformation(_("Der Text darf nicht leer sein"));
86
            return;
87
        }
88
89
        $storage = $ship->getStorage();
90
91
        $commodity = $this->commodityRepository->find(CommodityTypeEnum::BASE_ID_BUOY);
92
        if ($commodity !== null && !$storage->containsKey($commodity->getId())) {
93
            $game->addInformationf(
94
                _('Es wird eine Boje benötigt')
95
            );
96
            return;
97
        }
98
99
        if ($epsSystem->getEps() < 1) {
100
            $game->addInformation(_('Es wird 1 Energie für den Start der Boje benötigt'));
101
            return;
102
        }
103
104
        if ($commodity !== null) {
105
            $this->shipStorageManager->lowerStorage(
106
                $ship,
107
                $commodity,
108
                1
109
            );
110
        }
111
112
        $buoy = $this->buoyRepository->prototype();
113
        $buoy->setUser($game->getUser());
114
        $buoy->setText($text);
115
116
117
        if ($ship->getStarsystemMap() !== null) {
118
            $buoy->setSystemMap($ship->getStarsystemMap());
119
        } else {
120
            $buoy->setMap($ship->getMap());
121
        }
122
123
124
        $this->buoyRepository->save($buoy);
125
        $epsSystem->lowerEps(1)->update();
126
127
        $game->addInformation(_('Die Boje wurde erfolgreich platziert'));
128
    }
129
130
    public function performSessionCheck(): bool
131
    {
132
        return true;
133
    }
134
}