Passed
Push — master ( 4158cc...85b774 )
by Nico
17:22 queued 07:50
created

FirstColony::getCommodity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Maindesk\Action\FirstColony;
6
7
use InvalidArgumentException;
8
use RuntimeException;
9
use Stu\Module\Colony\Lib\PlanetColonizationInterface;
10
use Stu\Module\Control\ActionControllerInterface;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\PlayerSetting\Lib\UserEnum;
13
use Stu\Orm\Repository\BuildingRepositoryInterface;
14
use Stu\Orm\Repository\ColonyRepositoryInterface;
15
use Stu\Orm\Repository\UserRepositoryInterface;
16
use Stu\Component\Colony\Storage\ColonyStorageManagerInterface;
17
use Stu\Module\Commodity\CommodityTypeEnum;
18
use Stu\Orm\Entity\CommodityInterface;
19
use Stu\Orm\Repository\CommodityRepositoryInterface;
20
21
final class FirstColony implements ActionControllerInterface
22
{
23
    public const ACTION_IDENTIFIER = 'B_FIRST_COLONY';
24
25
    private FirstColonyRequestInterface $firstColonyRequest;
26
27
    private ColonyStorageManagerInterface $colonyStorageManager;
28
29
    private CommodityRepositoryInterface $commodityRepository;
30
31
    private BuildingRepositoryInterface $buildingRepository;
32
33
    private PlanetColonizationInterface $planetColonization;
34
35
    private ColonyRepositoryInterface $colonyRepository;
36
37
    private UserRepositoryInterface $userRepository;
38
39
    public function __construct(
40
        FirstColonyRequestInterface $firstColonyRequest,
41
        BuildingRepositoryInterface $buildingRepository,
42
        PlanetColonizationInterface $planetColonization,
43
        ColonyRepositoryInterface $colonyRepository,
44
        ColonyStorageManagerInterface $colonyStorageManager,
45
        CommodityRepositoryInterface $commodityRepository,
46
        UserRepositoryInterface $userRepository
47
    ) {
48
        $this->firstColonyRequest = $firstColonyRequest;
49
        $this->buildingRepository = $buildingRepository;
50
        $this->planetColonization = $planetColonization;
51
        $this->colonyRepository = $colonyRepository;
52
        $this->colonyStorageManager = $colonyStorageManager;
53
        $this->commodityRepository = $commodityRepository;
54
        $this->userRepository = $userRepository;
55
    }
56
57
    public function handle(GameControllerInterface $game): void
58
    {
59
        $user = $game->getUser();
60
61
        if ($user->getState() !== UserEnum::USER_STATE_UNCOLONIZED) {
62
            $game->addInformation(_('Es ist bereits eine Kolonie kolonisiert'));
63
            return;
64
        }
65
66
        $planetId = $this->firstColonyRequest->getPlanetId();
67
68
        $colony = $this->colonyRepository->find($planetId);
69
70
        if ($colony === null || !$colony->isFree()) {
71
            $game->addInformation(_('Dieser Planet wurde bereits besiedelt'));
72
            return;
73
        }
74
        $colonyList = $this->colonyRepository->getStartingByFaction((int) $user->getFactionId());
75
76
        if (!array_key_exists($planetId, $colonyList)) {
77
            return;
78
        }
79
80
        $faction = $user->getFaction();
81
        if ($faction === null) {
82
            return;
83
        }
84
85
        $startingBuilding =  $this->buildingRepository->find($faction->getStartBuildingId());
86
        if ($startingBuilding === null) {
87
            throw new RuntimeException(sprintf('buildingId %d not found', $faction->getStartBuildingId()));
88
        }
89
        $this->planetColonization->colonize(
90
            $colony,
91
            $user->getId(),
92
            $startingBuilding
93
        );
94
95
        $this->colonyStorageManager->upperStorage(
96
            $colony,
97
            $this->getCommodity(CommodityTypeEnum::COMMODITY_BUILDING_MATERIALS),
98
            150
99
        );
100
        $this->colonyStorageManager->upperStorage(
101
            $colony,
102
            $this->getCommodity(CommodityTypeEnum::COMMODITY_TRANSPARENT_ALUMINIUM),
103
            150
104
        );
105
        $this->colonyStorageManager->upperStorage(
106
            $colony,
107
            $this->getCommodity(CommodityTypeEnum::COMMODITY_DURANIUM),
108
            150
109
        );
110
        $this->colonyStorageManager->upperStorage(
111
            $colony,
112
            $this->getCommodity(CommodityTypeEnum::COMMODITY_DEUTERIUM),
113
            100
114
        );
115
116
        $user->setState(UserEnum::USER_STATE_TUTORIAL1);
117
118
        $this->userRepository->save($user);
119
120
        // Database entries for colonyclass
121
        $game->checkDatabaseItem($colony->getColonyClass()->getDatabaseId());
122
123
        $game->redirectTo('./colony.php?id=' . $colony->getId());
124
    }
125
126
    private function getCommodity(int $commodityId): CommodityInterface
127
    {
128
        $commodity = $this->commodityRepository->find(CommodityTypeEnum::COMMODITY_DEUTERIUM);
129
        if ($commodity === null) {
130
            throw new InvalidArgumentException(sprintf('commodityId %d does not exist', $commodityId));
131
        }
132
133
        return $commodity;
134
    }
135
136
    public function performSessionCheck(): bool
137
    {
138
        return false;
139
    }
140
}
141