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

ShowShipCreator::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 4
eloc 23
c 2
b 1
f 0
nc 4
nop 1
dl 0
loc 30
ccs 0
cts 22
cp 0
crap 20
rs 9.552
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\NPC\View\ShowShipCreator;
6
7
use Override;
8
use request;
9
use Stu\Module\Control\GameControllerInterface;
10
use Stu\Module\Control\ViewControllerInterface;
11
use Stu\Orm\Repository\ShipBuildplanRepositoryInterface;
12
use Stu\Orm\Repository\UserRepositoryInterface;
13
use Stu\Orm\Repository\LayerRepositoryInterface;
14
use Stu\Orm\Repository\TorpedoTypeRepositoryInterface;
15
16
final class ShowShipCreator implements ViewControllerInterface
17
{
18
    public const string VIEW_IDENTIFIER = 'SHOW_SHIP_CREATOR';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 18 at column 24
Loading history...
19
20
    public function __construct(
21
        private ShipBuildplanRepositoryInterface $shipBuildplanRepository,
22
        private UserRepositoryInterface $userRepository,
23
        private LayerRepositoryInterface $layerRepository,
24
        private TorpedoTypeRepositoryInterface $torpedoTypeRepository
25
    ) {}
26
27
    #[Override]
28
    public function handle(GameControllerInterface $game): void
29
    {
30
        $userId = request::getInt('userId');
31
        $buildplanId = request::getInt('buildplanId');
32
33
        $game->setTemplateFile('html/npc/shipCreator.twig');
34
        $game->appendNavigationPart('/npc/index.php?SHOW_SHIP_CREATOR=1', 'Schiff erstellen');
35
        $game->setPageTitle('Schiff erstellen');
36
37
        if ($userId > 0) {
38
            $selectedUser = $this->userRepository->find($userId);
39
            $game->setTemplateVar('USER_ID', $userId);
40
            $game->setTemplateVar('SELECTED_USER', $selectedUser);
41
42
            if ($buildplanId > 0) {
43
                $buildplan = $this->shipBuildplanRepository->find($buildplanId);
44
                if ($buildplan !== null) {
45
                    $possibleTorpedoTypes = $this->torpedoTypeRepository->getByLevel($buildplan->getRump()->getTorpedoLevel());
46
                    $game->setTemplateVar('TORPEDO_TYPES', $possibleTorpedoTypes);
47
                    $game->setTemplateVar('SELECTED_BUILDPLAN', $buildplan);
48
                    $game->setTemplateVar('LAYERS', $this->layerRepository->findAll());
49
                }
50
            } else {
51
                $game->setTemplateVar('BUILDPLANS', $this->shipBuildplanRepository->getByUser($userId));
52
            }
53
        } else {
54
            $npcList = iterator_to_array($this->userRepository->getNpcList());
55
            $nonNpcList = iterator_to_array($this->userRepository->getNonNpcList());
56
            $allUsers = array_merge($npcList, $nonNpcList);
57
            $game->setTemplateVar('ALL_USERS', $allUsers);
58
        }
59
    }
60
}
61