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

TransformResources   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 44
dl 0
loc 84
ccs 0
cts 46
cp 0
rs 10
c 2
b 0
f 0
wmc 14
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Action\Mining;
6
7
use Override;
8
use request;
9
use Stu\Component\Ship\System\ShipSystemTypeEnum;
10
use Stu\Exception\SanityCheckException;
11
use Stu\Module\Control\ActionControllerInterface;
12
use Stu\Module\Control\GameControllerInterface;
13
use Stu\Module\Ship\Lib\ActivatorDeactivatorHelperInterface;
14
use Stu\Module\Ship\Lib\ShipLoaderInterface;
15
use Stu\Module\Ship\View\ShowShip\ShowShip;
16
use Stu\Module\Commodity\CommodityTypeEnum;
17
use Stu\Orm\Repository\BuildingCommodityRepositoryInterface;
18
use Stu\Orm\Repository\CommodityRepositoryInterface;
19
20
final class TransformResources implements ActionControllerInterface
21
{
22
    public const string ACTION_IDENTIFIER = 'B_TRANSFORM_RESOURCES';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 22 at column 24
Loading history...
23
24
    public function __construct(
25
        private ShipLoaderInterface $shipLoader,
26
        private ActivatorDeactivatorHelperInterface $helper,
27
        private BuildingCommodityRepositoryInterface $buildingCommodityRepository,
28
        private CommodityRepositoryInterface $commodityRepository
29
    ) {}
30
31
    #[Override]
32
    public function handle(GameControllerInterface $game): void
33
    {
34
        $game->setView(ShowShip::VIEW_IDENTIFIER);
35
36
        $userId = $game->getUser()->getId();
37
        $shipId = request::indInt('id');
38
39
        $wrapper = $this->shipLoader->getWrapperByIdAndUser(
40
            $shipId,
41
            $userId
42
        );
43
44
        $ship = $wrapper->get();
45
        $aggregationsystem = $wrapper->getAggregationSystemSystemData();
46
47
        if ($aggregationsystem === null) {
48
            throw new SanityCheckException('collector = null ', self::ACTION_IDENTIFIER);
49
        }
50
51
        $commodityId = request::postInt('chosen');
52
53
54
55
        if ($commodityId === 0) {
56
            if ($ship->isSystemHealthy(ShipSystemTypeEnum::SYSTEM_AGGREGATION_SYSTEM)) {
57
                $this->helper->deactivate($wrapper, ShipSystemTypeEnum::SYSTEM_AGGREGATION_SYSTEM, $game);
58
                $aggregationsystem->setCommodityId($commodityId)->update();
59
            }
60
            return;
61
        } else {
62
63
            if (
64
                !$ship->getSystemState(ShipSystemTypeEnum::SYSTEM_AGGREGATION_SYSTEM)
65
                && !$this->helper->activate($wrapper, ShipSystemTypeEnum::SYSTEM_AGGREGATION_SYSTEM, $game)
66
            ) {
67
                return;
68
            }
69
70
            $canProduce = false;
71
            foreach (CommodityTypeEnum::COMMODITY_CONVERSIONS as $conversion) {
72
                if ($conversion[0] === $commodityId) {
73
                    $targetCommodityId = $conversion[1];
74
                    if ($this->buildingCommodityRepository->canProduceCommodity($userId, $targetCommodityId)) {
75
                        $canProduce = true;
76
                        $sourceCommodity = $this->commodityRepository->find($conversion[0]);
77
                        $targetCommodity = $this->commodityRepository->find($conversion[1]);
78
                        break;
79
                    }
80
                }
81
            }
82
83
            if (!$canProduce) {
84
                $game->addInformation("Diese Ressource kann nicht produziert werden");
85
                return;
86
            }
87
88
            $aggregationsystem->setCommodityId($commodityId)->update();
89
            if ($sourceCommodity &&  $targetCommodity) {
90
                $game->addInformationf(
91
                    sprintf(
92
                        "%s wird in %s umgewandelt",
93
                        $sourceCommodity->getName(),
94
                        $targetCommodity->getName()
95
                    )
96
                );
97
            }
98
        }
99
    }
100
101
    #[Override]
102
    public function performSessionCheck(): bool
103
    {
104
        return true;
105
    }
106
}
107