Passed
Pull Request — dev (#2303)
by Janko
05:56
created

AggregrationSystemSettings::setTemplateVariables()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 69
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 10.2655

Importance

Changes 0
Metric Value
cc 9
eloc 45
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 69
ccs 36
cts 48
cp 0.75
crap 10.2655
rs 7.6444

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\ShowSystemSettings;
6
7
use RuntimeException;
8
use Stu\Component\Faction\FactionEnum;
9
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
10
use Stu\Exception\SanityCheckException;
11
use Stu\Module\Commodity\CommodityTypeConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Commodity\CommodityTypeConstants was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Stu\Module\Control\GameControllerInterface;
13
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
14
use Stu\Module\Station\Lib\StationWrapperInterface;
15
use Stu\Orm\Repository\BuildingCommodityRepositoryInterface;
16
use Stu\Orm\Repository\CommodityRepositoryInterface;
17
18
class AggregrationSystemSettings implements SystemSettingsProviderInterface
19
{
20 1
    public function __construct(
21
        private CommodityRepositoryInterface $commodityRepository,
22
        private BuildingCommodityRepositoryInterface $buildingCommodityRepository
23 1
    ) {}
24
25 1
    #[\Override]
26
    public function setTemplateVariables(
27
        SpacecraftSystemTypeEnum $systemType,
28
        SpacecraftWrapperInterface $wrapper,
29
        GameControllerInterface $game
30
    ): void {
31 1
        $userId = $game->getUser()->getId();
32 1
        $spacecraft = $wrapper->get();
33 1
        $module = $spacecraft->getSpacecraftSystem($systemType)->getModule();
34
35 1
        $game->setMacroInAjaxWindow('html/ship/aggregationsystem.twig');
36
37 1
        if (!$wrapper instanceof StationWrapperInterface) {
38
            throw new RuntimeException('this should not happen');
39
        }
40
41 1
        $aggregationsystem = $wrapper->getAggregationSystemSystemData();
42 1
        if ($aggregationsystem === null) {
43
            throw new SanityCheckException(
44
                'no aggregation system installed',
45
                null,
46
                ShowSystemSettings::VIEW_IDENTIFIER
0 ignored issues
show
Bug introduced by
The type Stu\Module\Spacecraft\Vi...ings\ShowSystemSettings was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
47
            );
48
        }
49
50 1
        $commodities = CommodityTypeConstants::COMMODITY_CONVERSIONS;
51 1
        $mode1Commodities = array_filter($commodities, fn($entry): bool => $entry[4] === 1);
52 1
        $mode2Commodities = array_filter($commodities, fn($entry): bool => $entry[4] === 2);
53
54 1
        $mode1Commodities = array_map(fn($entry): array => [
55 1
            $this->commodityRepository->find($entry[0]),
56 1
            $this->commodityRepository->find($entry[1]),
57 1
            $entry[2],
58 1
            $entry[3]
59 1
        ], $mode1Commodities);
60
61 1
        $mode2Commodities = array_map(fn($entry): array => [
62 1
            $this->commodityRepository->find($entry[0]),
63 1
            $this->commodityRepository->find($entry[1]),
64 1
            $entry[2],
65 1
            $entry[3]
66 1
        ], $mode2Commodities);
67
68 1
        if ($module && $module->getFactionId() == FactionEnum::FACTION_FERENGI) {
69
            foreach (array_keys($mode1Commodities) as $key) {
70
                $mode1Commodities[$key][2] *= 2;
71
                $mode1Commodities[$key][3] *= 2;
72
            }
73
74
            foreach (array_keys($mode2Commodities) as $key) {
75
                $mode2Commodities[$key][2] *= 2;
76
                $mode2Commodities[$key][3] *= 2;
77
            }
78
        }
79
80 1
        $mode1Commodities = array_filter($mode1Commodities, function (array $entry) use ($userId): bool {
81 1
            return $entry[1] !== null
82 1
            && $this->buildingCommodityRepository->canProduceCommodity($userId, $entry[1]->getId());
83 1
        });
84
85 1
        $mode2Commodities = array_filter($mode2Commodities, function (array $entry) use ($userId): bool {
86 1
            return $entry[1] !== null
87 1
            && $this->buildingCommodityRepository->canProduceCommodity($userId, $entry[1]->getId());
88 1
        });
89
90 1
        $chosencommodity = $aggregationsystem->getCommodityId();
91 1
        $game->setTemplateVar('MODE1_COMMODITIES', $mode1Commodities);
92 1
        $game->setTemplateVar('MODE2_COMMODITIES', $mode2Commodities);
93 1
        $game->setTemplateVar('CHOSENCOMMODITY', $chosencommodity);
94
    }
95
}
96