Passed
Push — dev ( 766009...bcc49c )
by Janko
09:42
created

ManualColonyTick::executeTickForSingleColony()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Admin\Action\Ticks\Colony;
6
7
use Stu\Module\Admin\View\Ticks\ShowTicks;
8
use Stu\Module\Config\Model\ColonySettings;
9
use Stu\Module\Config\StuConfigInterface;
10
use Stu\Module\Control\ActionControllerInterface;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Tick\Colony\ColonyTickInterface;
13
use Stu\Module\Tick\Colony\ColonyTickManagerInterface;
14
use Stu\Orm\Repository\ColonyRepositoryInterface;
15
16
final class ManualColonyTick implements ActionControllerInterface
17
{
18
    public const ACTION_IDENTIFIER = 'B_COLONY_TICK';
19
20
    private ManualColonyTickRequestInterface $request;
21
22
    private ColonyTickManagerInterface $colonyTickManager;
23
24
    private ColonyTickInterface $colonyTick;
25
26
    private ColonyRepositoryInterface $colonyRepository;
27
28
    private StuConfigInterface $config;
29
30 5
    public function __construct(
31
        ManualColonyTickRequestInterface $request,
32
        ColonyTickManagerInterface $colonyTickManager,
33
        ColonyTickInterface $colonyTick,
34
        ColonyRepositoryInterface $colonyRepository,
35
        StuConfigInterface $config
36
    ) {
37 5
        $this->request = $request;
38 5
        $this->colonyTickManager = $colonyTickManager;
39 5
        $this->colonyTick = $colonyTick;
40 5
        $this->colonyRepository = $colonyRepository;
41 5
        $this->config = $config;
42
    }
43
44 5
    public function handle(GameControllerInterface $game): void
45
    {
46 5
        $game->setView(ShowTicks::VIEW_IDENTIFIER);
47
48
        // only Admins can trigger ticks
49 5
        if (!$game->isAdmin()) {
50 1
            $game->addInformation(_('[b][color=#ff2626]Aktion nicht möglich, Spieler ist kein Admin![/color][/b]'));
51 1
            return;
52
        }
53
54 4
        $colonyId = $this->request->getColonyId();
55
56
        //check if single or multiple colonies
57 4
        if ($colonyId === null) {
58 2
            $this->executeTickForMultipleColonies($game);
59
        } else {
60 2
            $this->executeTickForSingleColony($colonyId, $game);
61
        }
62
    }
63
64 2
    private function executeTickForMultipleColonies(GameControllerInterface $game): void
65
    {
66 2
        $groupId = $this->request->getGroupId();
67
68 2
        if ($groupId !== null) {
69 1
            $groupCount = $this->getGroupCount();
70 1
            $this->colonyTickManager->work($groupId, $groupCount);
71
72 1
            $game->addInformationf("Der Kolonie-Tick für die Kolonie-Gruppe %d/%d wurde durchgeführt!", $groupId, $groupCount);
73
        } else {
74 1
            $this->colonyTickManager->work(1, ColonySettings::SETTING_TICK_WORKER_DEFAULT);
75 1
            $game->addInformation("Der Kolonie-Tick für alle Kolonien wurde durchgeführt!");
76
        }
77
    }
78
79 1
    private function getGroupCount(): int
80
    {
81 1
        return $this->config->getGameSettings()->getColonySettings()->getTickWorker();
82
    }
83
84 2
    private function executeTickForSingleColony(int $colonyId, GameControllerInterface $game): void
85
    {
86 2
        $colony = $this->colonyRepository->find($colonyId);
87
88 2
        if ($colony === null) {
89 1
            $game->addInformationf("Keine Kolonie mit der ID %d vorhanden!", $colonyId);
90 1
            return;
91
        }
92
93 1
        $this->colonyTick->work($colony);
94
95 1
        $game->addInformationf("Der Kolonie-Tick für die Kolonie mit der ID %d wurde durchgeführt!", $colonyId);
96
    }
97
98
    public function performSessionCheck(): bool
99
    {
100
        return true;
101
    }
102
}
103