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

AggregrationSystemSettings   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 76%

Importance

Changes 0
Metric Value
eloc 46
c 0
b 0
f 0
dl 0
loc 76
ccs 38
cts 50
cp 0.76
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B setTemplateVariables() 0 69 9
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