CreateGameServer::__invoke()   B
last analyzed

Complexity

Conditions 7
Paths 48

Size

Total Lines 59
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 59
rs 8.3466
cc 7
nc 48
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Gameap\UseCases;
4
5
use Gameap\Models\Server;
6
use Gameap\Repositories\GameModRepository;
7
use Gameap\Repositories\NodeRepository;
8
use Gameap\Repositories\GdaemonTaskRepository;
9
use Gameap\Repositories\ServerRepository;
10
use Gameap\UseCases\Commands\CreateGameServerCommand;
11
use Illuminate\Support\Str;
12
13
class CreateGameServer
14
{
15
    public const DEFAULT_RCON_PASSWORD_LENGTH = 10;
16
17
    /** @var GdaemonTaskRepository */
18
    protected $gdaemonTaskRepository;
19
20
    /** @var ServerRepository */
21
    protected $serverRepository;
22
23
    /** @var NodeRepository */
24
    protected $nodeRepository;
25
26
    /** @var GameModRepository */
27
    protected $gameModRepository;
28
29
    public function __construct(GdaemonTaskRepository $gdaemonTaskRepository, ServerRepository $serverRepository, NodeRepository $nodeRepository, GameModRepository $gameModRepository)
30
    {
31
        $this->gdaemonTaskRepository = $gdaemonTaskRepository;
32
        $this->serverRepository = $serverRepository;
33
        $this->nodeRepository = $nodeRepository;
34
        $this->gameModRepository = $gameModRepository;
35
    }
36
37
    public function __invoke(CreateGameServerCommand $command): array
38
    {
39
        $server = new Server();
40
41
        $server->uuid               = Str::orderedUuid()->toString();
42
        $server->uuid_short         = Str::substr($server->uuid, 0, 8);
43
        $server->ds_id              = $command->dsId;
44
        $server->name               = $command->name;
45
        $server->game_id            = $command->gameId;
46
        $server->game_mod_id        = $command->gameModId;
47
        $server->server_ip          = $command->serverIp;
48
        $server->server_port        = $command->serverPort;
49
        $server->query_port         = $command->queryPort;
50
        $server->rcon_port          = $command->rconPort;
51
        $server->su_user            = $command->suUser;
52
53
        $server->enabled = true;
54
        $server->blocked = false;
55
56
        if (empty($command->rcon)) {
57
            $server->rcon = Str::random(self::DEFAULT_RCON_PASSWORD_LENGTH);
58
        } else {
59
            $server->rcon = $command->rcon;
60
        }
61
62
        $node = $this->nodeRepository->findById($command->dsId);
63
64
        if (empty($command->startCommand)) {
65
            $gameMod = $this->gameModRepository->getById($command->gameModId);
66
67
            $server->start_command =
68
                $node->isLinux()
69
                    ? $gameMod->start_cmd_linux
70
                    : $gameMod->start_cmd_windows;
71
        } else {
72
            $server->start_command = $command->startCommand;
73
        }
74
75
        if (empty($command->dir)) {
76
            $server->dir = 'servers/' . $server->uuid;
77
        } else {
78
            $server->dir = $command->dir;
79
        }
80
81
        if ($command->install) {
82
            $server->installed = false;
83
        }
84
85
        $this->serverRepository->save($server);
86
87
        $taskID = 0;
88
89
        if ($command->install) {
90
            $taskID = $this->gdaemonTaskRepository->addServerInstall($server);
91
        }
92
93
        return [
94
            'taskId' => $taskID,
95
            'serverId' => $server->id,
96
        ];
97
    }
98
}
99