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

DropBuoy::handle()   C

Complexity

Conditions 17
Paths 20

Size

Total Lines 94
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
eloc 60
c 1
b 0
f 0
nc 20
nop 1
dl 0
loc 94
rs 5.2166

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 (mb_strlen($text) > 60) {
0 ignored issues
show
Bug introduced by
$text of type false is incompatible with the type string expected by parameter $string of mb_strlen(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        if (mb_strlen(/** @scrutinizer ignore-type */ $text) > 60) {
Loading history...
80
81
            $game->addInformation(_("Der Text darf nicht länger als 60 Zeichen sein"));
82
            return;
83
        }
84
85
        if (empty($text)) {
86
            $game->addInformation(_("Der Text darf nicht leer sein."));
87
            return;
88
        }
89
90
        $storage = $ship->getStorage();
91
92
        $commodity = $this->commodityRepository->find(CommodityTypeEnum::BASE_ID_BOUY);
93
        if ($commodity !== null && !$storage->containsKey($commodity->getId())) {
94
            $game->addInformationf(
95
                _('Es wird eine Boje benötigt')
96
            );
97
            return;
98
        }
99
100
        if ($epsSystem->getEps() < 1) {
101
            $game->addInformation(_('Es wird 1 Energie für den Start der Boje benötigt'));
102
            return;
103
        }
104
105
        if ($commodity !== null) {
106
            $this->shipStorageManager->lowerStorage(
107
                $ship,
108
                $commodity,
109
                1
110
            );
111
        }
112
113
        $buoy = $this->buoyRepository->prototype();
114
        $buoy->setUser($game->getUser());
115
        if ($text != false) {
116
            if (mb_strlen($text) > 60) {
117
118
                $game->addInformation(_("Der Text darf nicht länger als 60 Zeichen sein"));
119
                return;
120
            } else {
121
                $buoy->setText($text);
122
            }
123
        }
124
125
        if ($ship->getStarsystemMap() !== null) {
126
            $buoy->setSystemMap($ship->getStarsystemMap());
127
        } else {
128
            $buoy->setMap($ship->getMap());
129
        }
130
131
        $this->buoyRepository->save($buoy);
132
        $epsSystem->lowerEps(1)->update();
133
134
        $game->addInformation(_('Die Boje wurde erfolgreich platziert'));
135
    }
136
137
    public function performSessionCheck(): bool
138
    {
139
        return true;
140
    }
141
}