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

GatherResources::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 6
dl 0
loc 8
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Action\Mining;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Override;
9
use request;
10
use Stu\Component\Ship\ShipStateEnum;
11
use Stu\Component\Ship\System\ShipSystemTypeEnum;
12
use Stu\Exception\SanityCheckException;
13
use Stu\Module\Control\ActionControllerInterface;
14
use Stu\Module\Control\GameControllerInterface;
15
use Stu\Module\Ship\Lib\ActivatorDeactivatorHelperInterface;
16
use Stu\Module\Ship\Lib\ShipLoaderInterface;
17
use Stu\Module\Ship\Lib\ShipStateChangerInterface;
18
use Stu\Module\Ship\View\ShowShip\ShowShip;
19
use Stu\Orm\Repository\LocationMiningRepositoryInterface;
20
use Stu\Orm\Repository\MiningQueueRepositoryInterface;
21
22
final class GatherResources implements ActionControllerInterface
23
{
24
    public const string ACTION_IDENTIFIER = 'B_GATHER_RESOURCES';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 24 at column 24
Loading history...
25
26
    public function __construct(
27
        private ShipLoaderInterface $shipLoader,
28
        private ActivatorDeactivatorHelperInterface $helper,
29
        private MiningQueueRepositoryInterface $miningQueueRepository,
30
        private ShipStateChangerInterface $shipStateChanger,
31
        private EntityManagerInterface $entityManager,
32
        private LocationMiningRepositoryInterface $locationMiningRepository
33
    ) {}
34
35
    #[Override]
36
    public function handle(GameControllerInterface $game): void
37
    {
38
        $game->setView(ShowShip::VIEW_IDENTIFIER);
39
40
        $userId = $game->getUser()->getId();
41
        $shipId = request::indInt('id');
42
43
        $wrapper = $this->shipLoader->getWrapperByIdAndUser(
44
            $shipId,
45
            $userId
46
        );
47
48
        $ship = $wrapper->get();
49
        $bussardcollector = $wrapper->getBussardCollectorSystemData();
50
51
        if ($bussardcollector === null) {
52
            throw new SanityCheckException('collector = null ', self::ACTION_IDENTIFIER);
53
        }
54
55
        if ($ship->isWarped()) {
56
            $game->addInformation("Aktion nicht möglich, Schiff befindet sich im Warp");
57
            return;
58
        }
59
60
        $chosenLocationId = request::postInt('chosen');
61
62
        if ($chosenLocationId === 0) {
63
            if ($ship->isSystemHealthy(ShipSystemTypeEnum::SYSTEM_BUSSARD_COLLECTOR)) {
64
                $this->helper->deactivate($wrapper, ShipSystemTypeEnum::SYSTEM_BUSSARD_COLLECTOR, $game);
65
            }
66
67
            $miningQueue = $this->miningQueueRepository->getByShip($ship->getId());
68
            if ($miningQueue !== null) {
69
                $this->miningQueueRepository->truncateByShipId($ship->getId());
70
                $game->addInformation("Es werden keine Ressourcen mehr gesammelt");
71
            }
72
            $this->shipStateChanger->changeShipState($wrapper, ShipStateEnum::SHIP_STATE_NONE);
73
            return;
74
        } else {
75
76
            $locationMining = $this->locationMiningRepository->findById($chosenLocationId);
77
            if ($locationMining === null) {
78
                throw new SanityCheckException('Invalid location mining ID', self::ACTION_IDENTIFIER);
79
            }
80
81
82
            if (!$ship->getSystemState(ShipSystemTypeEnum::SYSTEM_BUSSARD_COLLECTOR)) {
83
                if (!$this->helper->activate($wrapper, ShipSystemTypeEnum::SYSTEM_BUSSARD_COLLECTOR, $game)) {
84
                    return;
85
                }
86
            } else {
87
                $miningQueue = $this->miningQueueRepository->getByShip($ship->getId());
88
                if ($miningQueue !== null) {
89
                    $this->miningQueueRepository->truncateByShipId($ship->getId());
90
                }
91
            }
92
            $this->shipStateChanger->changeShipState($wrapper, ShipStateEnum::SHIP_STATE_GATHER_RESOURCES);
93
94
95
            $miningqueue = $this->miningQueueRepository->prototype();
96
            $miningqueue->setShip($ship);
97
            $miningqueue->setLocationMining($locationMining);
98
            $this->miningQueueRepository->save($miningqueue);
99
100
            $this->entityManager->flush();
101
102
            $game->addInformationf(
103
                sprintf('%s wird gesammelt', $locationMining->getCommodity()->getName()),
104
            );
105
        }
106
    }
107
108
    #[Override]
109
    public function performSessionCheck(): bool
110
    {
111
        return true;
112
    }
113
}
114