Passed
Push — master ( f544cb...b3a3d9 )
by Nico
36:43 queued 09:10
created

CreateSandbox::performSessionCheck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Admin\Action\Sandbox;
6
7
use request;
8
use Stu\Module\Admin\View\Sandbox\ShowColonySandbox;
9
use Stu\Module\Control\ActionControllerInterface;
10
use Stu\Module\Control\GameControllerInterface;
11
use Stu\Orm\Entity\ColonyInterface;
12
use Stu\Orm\Repository\ColonySandboxRepositoryInterface;
13
use Stu\Orm\Repository\PlanetFieldRepositoryInterface;
14
15
final class CreateSandbox implements ActionControllerInterface
16
{
17
    public const ACTION_IDENTIFIER = 'B_CREATE_SANDBOX';
18
19
    private ColonySandboxRepositoryInterface $colonySandboxRepository;
20
21
    private PlanetFieldRepositoryInterface $planetFieldRepository;
22
23
    public function __construct(
24
        ColonySandboxRepositoryInterface $colonySandboxRepository,
25
        PlanetFieldRepositoryInterface $planetFieldRepository
26
    ) {
27
        $this->colonySandboxRepository = $colonySandboxRepository;
28
        $this->planetFieldRepository = $planetFieldRepository;
29
    }
30
31
    public function handle(GameControllerInterface $game): void
32
    {
33
        $game->setView(ShowColonySandbox::VIEW_IDENTIFIER);
34
35
        if (!$game->isAdmin()) {
36
            $game->addInformation(_('[b][color=#ff2626]Aktion nicht möglich, Spieler ist kein Admin![/color][/b]'));
37
            return;
38
        }
39
40
        $colonyId = request::postIntFatal('cid');
41
42
        /** @var ColonyInterface|null */
43
        $colony = $game->getUser()->getColonies()->get($colonyId);
44
        if ($colony === null) {
45
            return;
46
        }
47
48
        $sandboxName = request::postStringFatal('name');
49
50
        $sandbox = $this->colonySandboxRepository->prototype();
51
        $sandbox
52
            ->setColony($colony)
53
            ->setName($sandboxName)
54
            ->setMaxBev($colony->getMaxBev())
55
            ->setMaxEps($colony->getMaxEps())
56
            ->setMaxStorage($colony->getMaxStorage())
57
            ->setWorkers($colony->getWorkers())
58
            ->setMask($colony->getMask());
59
        $this->colonySandboxRepository->save($sandbox);
60
61
        foreach ($colony->getPlanetFields() as $fieldId => $field) {
62
            $building = $field->getBuilding();
63
64
            $sandboxField = $this->planetFieldRepository->prototype();
65
            $sandboxField
66
                ->setColonySandbox($sandbox)
67
                ->setFieldId($fieldId)
68
                ->setBuilding($building)
69
                ->setIntegrity($building === null ? 0 : $building->getIntegrity())
70
                ->setFieldType($field->getFieldType())
71
                ->setActive($field->getActive());
72
73
            $this->planetFieldRepository->save($sandboxField);
74
75
            $sandbox->getPlanetFields()->set($fieldId, $sandboxField);
76
        }
77
78
        $game->addInformationf(_('Sandbox %s wurde erstellt'), $sandboxName);
79
80
        $game->setView(ShowColonySandbox::VIEW_IDENTIFIER, ['HOST' => $sandbox]);
81
    }
82
83
    public function performSessionCheck(): bool
84
    {
85
        return true;
86
    }
87
}
88