Passed
Push — dev ( d85471...98511f )
by Janko
18:01
created

ShowTradeMenu::handle()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 61
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 7.4822

Importance

Changes 0
Metric Value
cc 7
eloc 43
nc 14
nop 1
dl 0
loc 61
ccs 33
cts 42
cp 0.7856
crap 7.4822
rs 8.2986
c 0
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\Spacecraft\View\ShowTradeMenu;
6
7
use Override;
8
use request;
9
use Stu\Exception\AccessViolationException;
10
use Stu\Module\Control\GameControllerInterface;
11
use Stu\Module\Control\ViewContextTypeEnum;
12
use Stu\Module\Control\ViewControllerInterface;
13
use Stu\Module\Spacecraft\Lib\Interaction\InteractionCheckerInterface;
14
use Stu\Module\Spacecraft\Lib\SpacecraftLoaderInterface;
15
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
16
use Stu\Module\Trade\Lib\TradeLibFactoryInterface;
17
use Stu\Orm\Repository\CommodityRepositoryInterface;
18
use Stu\Orm\Repository\TradeLicenseInfoRepositoryInterface;
19
use Stu\Orm\Repository\TradeLicenseRepositoryInterface;
20
use Stu\Orm\Repository\TradePostRepositoryInterface;
21
22
final class ShowTradeMenu implements ViewControllerInterface
23
{
24
    public const string VIEW_IDENTIFIER = 'SHOW_TRADEMENU';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 24 at column 24
Loading history...
25
26
    /**
27
     * @param SpacecraftLoaderInterface<SpacecraftWrapperInterface> $spaceCraftLoader
28
     */
29 1
    public function __construct(
30
        private TradeLicenseRepositoryInterface $tradeLicenseRepository,
31
        private TradeLicenseInfoRepositoryInterface $TradeLicenseInfoRepository,
32
        private TradeLibFactoryInterface $tradeLibFactory,
33
        private TradePostRepositoryInterface $tradePostRepository,
34
        private CommodityRepositoryInterface $commodityRepository,
35
        private InteractionCheckerInterface $interactionChecker,
36
        private SpacecraftLoaderInterface $spaceCraftLoader
37 1
    ) {}
38
39 1
    #[Override]
40
    public function handle(GameControllerInterface $game): void
41
    {
42 1
        $userId = $game->getUser()->getId();
43
44 1
        $spacecraft = $this->spaceCraftLoader->getByIdAndUser(request::getIntFatal('id'), $userId);
45
46 1
        $tradepost = $this->tradePostRepository->find(request::indInt('postid'));
47 1
        if ($tradepost === null) {
48
            return;
49
        }
50
51 1
        if (!$this->interactionChecker->checkPosition($spacecraft, $tradepost->getStation())) {
52
            throw new AccessViolationException();
53
        }
54
55 1
        $game->setPageTitle(_('Handelstransfermenü'));
56 1
        if ($game->getViewContext(ViewContextTypeEnum::NO_AJAX) === true) {
57
            $game->showMacro('html/spacecraft/trademenu.twig');
58
        } else {
59 1
            $game->setMacroInAjaxWindow('html/spacecraft/trademenu.twig');
60
        }
61
62 1
        $databaseEntryId = $tradepost->getStation()->getDatabaseId();
63
64 1
        if ($databaseEntryId > 0) {
65
            $game->checkDatabaseItem($databaseEntryId);
66
        }
67 1
        $licenseInfo = $this->TradeLicenseInfoRepository->getLatestLicenseInfo($tradepost->getId());
68
69 1
        if ($licenseInfo !== null) {
70 1
            $commodityId = $licenseInfo->getCommodityId();
71 1
            $commodity = $this->commodityRepository->find($commodityId);
72 1
            $commodityName = $commodity !== null ? $commodity->getName() : '';
73 1
            $licensecost = $licenseInfo->getAmount();
74 1
            $licensedays = $licenseInfo->getDays();
75
        } else {
76
            $commodityId = 1;
77
            $commodityName = 'Keine Ware';
78
            $licensecost = 0;
79
            $licensedays = 0;
80
        }
81
82 1
        $game->setTemplateVar('TRADEPOST', $this->tradeLibFactory->createTradeAccountWrapper($tradepost, $userId));
83 1
        $game->setTemplateVar('SHIP', $spacecraft);
84 1
        $game->setTemplateVar(
85 1
            'HAS_LICENSE',
86 1
            $this->tradeLicenseRepository->hasLicenseByUserAndTradePost($userId, $tradepost->getId())
87 1
        );
88 1
        $game->setTemplateVar(
89 1
            'CAN_BUY_LICENSE',
90 1
            $licenseInfo !== null
91 1
        );
92 1
        $game->setTemplateVar('LICENSECOMMODITY', $commodityId);
93 1
        $game->setTemplateVar('LICENSECOMMODITYNAME', $commodityName);
94 1
        $game->setTemplateVar('LICENSECOST', $licensecost);
95 1
        $game->setTemplateVar('LICENSEDAYS', $licensedays);
96
    }
97
}
98