Passed
Push — dev ( 2eb11f...3c2dab )
by Janko
09:11
created

AggregrationSystemSettings   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 71
ccs 36
cts 44
cp 0.8182
rs 10
c 0
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B setTemplateVariables() 0 64 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;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Faction\FactionEnum 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...
9
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
10
use Stu\Exception\SanityCheckException;
11
use Stu\Module\Commodity\CommodityTypeEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Commodity\CommodityTypeEnum 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
    public function setTemplateVariables(
26
        SpacecraftSystemTypeEnum $systemType,
27
        SpacecraftWrapperInterface $wrapper,
28
        GameControllerInterface $game
29
    ): void {
30
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('no aggregation system installed', null, 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...
44
        }
45
46 1
        $commodities = CommodityTypeEnum::COMMODITY_CONVERSIONS;
47 1
        $mode1Commodities = array_filter($commodities, fn($entry): bool => $entry[4] === 1);
48 1
        $mode2Commodities = array_filter($commodities, fn($entry): bool => $entry[4] === 2);
49
50
51 1
        $mode1Commodities = array_map(fn($entry): array => [
52 1
            $this->commodityRepository->find($entry[0]),
53 1
            $this->commodityRepository->find($entry[1]),
54 1
            $entry[2],
55 1
            $entry[3]
56 1
        ], $mode1Commodities);
57
58 1
        $mode2Commodities = array_map(fn($entry): array => [
59 1
            $this->commodityRepository->find($entry[0]),
60 1
            $this->commodityRepository->find($entry[1]),
61 1
            $entry[2],
62 1
            $entry[3]
63 1
        ], $mode2Commodities);
64
65 1
        if ($module && $module->getFactionId() == FactionEnum::FACTION_FERENGI) {
66
            foreach ($mode1Commodities as &$entry) {
67
                $entry[2] *= 2;
68
                $entry[3] *= 2;
69
            }
70
71
            foreach ($mode2Commodities as &$entry) {
72
                $entry[2] *= 2;
73
                $entry[3] *= 2;
74
            }
75
        }
76
77 1
        $mode1Commodities = array_filter($mode1Commodities, function ($entry) use ($userId): bool {
78 1
            return $entry[1] !== null && $this->buildingCommodityRepository->canProduceCommodity($userId, $entry[1]->getId());
79 1
        });
80
81 1
        $mode2Commodities = array_filter($mode2Commodities, function ($entry) use ($userId): bool {
82 1
            return $entry[1] !== null && $this->buildingCommodityRepository->canProduceCommodity($userId, $entry[1]->getId());
83 1
        });
84
85 1
        $chosencommodity = $aggregationsystem->getCommodityId();
86 1
        $game->setTemplateVar('MODE1_COMMODITIES', $mode1Commodities);
87 1
        $game->setTemplateVar('MODE2_COMMODITIES', $mode2Commodities);
88 1
        $game->setTemplateVar('CHOSENCOMMODITY', $chosencommodity);
89
    }
90
}
91