Passed
Push — master ( 92a2e9...9d73f4 )
by Nico
41:55
created

ShowAggregationSystem::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 3
dl 0
loc 5
ccs 0
cts 2
cp 0
crap 2
rs 10
c 2
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\View\ShowAggregationSystem;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override 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...
8
use request;
9
use Stu\Exception\SanityCheckException;
10
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...
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Control\ViewControllerInterface;
13
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...
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 VIEW_IDENTIFIER = 'SHOW_AGGREGATION_SYSTEM_AJAX';
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) => $entry[4] === 1);
62
        $mode2Commodities = array_filter($commodities, fn($entry) => $entry[4] === 2);
63
64
65
        $mode1Commodities = array_map(fn($entry) => [
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) => [
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) {
93
            return $entry[1] !== null && $this->buildingCommodityRepository->canProduceCommodity($userId, $entry[1]->getId());
94
        });
95
96
        $mode2Commodities = array_filter($mode2Commodities, function ($entry) use ($userId) {
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