Passed
Pull Request — master (#1930)
by Janko
14:58 queued 05:13
created

ShowAggregationSystem::handle()   B

Complexity

Conditions 8
Paths 3

Size

Total Lines 75
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 50
nc 3
nop 1
dl 0
loc 75
ccs 0
cts 54
cp 0
crap 72
rs 7.8464
c 2
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\View\ShowAggregationSystem;
6
7
use Override;
8
use request;
9
use Stu\Exception\SanityCheckException;
10
use Stu\Component\Faction\FactionEnum;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Control\ViewControllerInterface;
13
use Stu\Module\Commodity\CommodityTypeEnum;
14
use Stu\Module\Ship\Lib\ShipLoaderInterface;
15
use Stu\Orm\Repository\CommodityRepositoryInterface;
16
use Stu\Orm\Repository\BuildingCommodityRepositoryInterface;
17
use Stu\Component\Ship\System\ShipSystemTypeEnum;
18
19
final class ShowAggregationSystem implements ViewControllerInterface
20
{
21
    public const string VIEW_IDENTIFIER = 'SHOW_AGGREGATION_SYSTEM_AJAX';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 21 at column 24
Loading history...
22
23
    public function __construct(
24
        private ShipLoaderInterface $shipLoader,
25
        private CommodityRepositoryInterface $commodityRepository,
26
        private BuildingCommodityRepositoryInterface $buildingCommodityRepository
27
    ) {}
28
29
    #[Override]
30
    public function handle(GameControllerInterface $game): void
31
    {
32
        $user = $game->getUser();
33
        $userId = $user->getId();
34
35
        $ship = $this->shipLoader->getByIdAndUser(
36
            request::indInt('id'),
37
            $userId,
38
            false,
39
            false
40
        );
41
42
        $wrapper = $this->shipLoader->getWrapperByIdAndUser(
43
            request::indInt('id'),
44
            $userId,
45
            false,
46
            false
47
        );
48
        $module = $ship->getShipSystem(ShipSystemTypeEnum::SYSTEM_AGGREGATION_SYSTEM)->getModule();
49
50
        $game->setPageTitle(_('Aggregationssystem'));
51
        $game->setMacroInAjaxWindow('html/ship/aggregationsystem.twig');
52
53
        $game->setTemplateVar('WRAPPER', $wrapper);
54
55
        $aggregationsystem = $wrapper->getAggregationSystemSystemData();
56
        if ($aggregationsystem === null) {
57
            throw new SanityCheckException('no aggregation system installed', null, self::VIEW_IDENTIFIER);
58
        }
59
60
        $commodities = CommodityTypeEnum::COMMODITY_CONVERSIONS;
61
        $mode1Commodities = array_filter($commodities, fn($entry): bool => $entry[4] === 1);
62
        $mode2Commodities = array_filter($commodities, fn($entry): bool => $entry[4] === 2);
63
64
65
        $mode1Commodities = array_map(fn($entry): array => [
66
            $this->commodityRepository->find($entry[0]),
67
            $this->commodityRepository->find($entry[1]),
68
            $entry[2],
69
            $entry[3]
70
        ], $mode1Commodities);
71
72
        $mode2Commodities = array_map(fn($entry): array => [
73
            $this->commodityRepository->find($entry[0]),
74
            $this->commodityRepository->find($entry[1]),
75
            $entry[2],
76
            $entry[3]
77
        ], $mode2Commodities);
78
79
        if ($module && $module->getFactionId() == FactionEnum::FACTION_FERENGI) {
80
            foreach ($mode1Commodities as &$entry) {
81
                $entry[2] *= 2;
82
                $entry[3] *= 2;
83
            }
84
85
            foreach ($mode2Commodities as &$entry) {
86
                $entry[2] *= 2;
87
                $entry[3] *= 2;
88
            }
89
        }
90
91
92
        $mode1Commodities = array_filter($mode1Commodities, function ($entry) use ($userId): bool {
93
            return $entry[1] !== null && $this->buildingCommodityRepository->canProduceCommodity($userId, $entry[1]->getId());
94
        });
95
96
        $mode2Commodities = array_filter($mode2Commodities, function ($entry) use ($userId): bool {
97
            return $entry[1] !== null && $this->buildingCommodityRepository->canProduceCommodity($userId, $entry[1]->getId());
98
        });
99
100
        $chosencommodity = $aggregationsystem->getCommodityId();
101
        $game->setTemplateVar('MODE1_COMMODITIES', $mode1Commodities);
102
        $game->setTemplateVar('MODE2_COMMODITIES', $mode2Commodities);
103
        $game->setTemplateVar('CHOSENCOMMODITY', $chosencommodity);
104
    }
105
}
106