GdaemonTaskRepository::concatOutput()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 16
ccs 0
cts 6
cp 0
rs 9.6111
c 0
b 0
f 0
cc 5
nc 4
nop 2
crap 30
1
<?php
2
3
namespace Gameap\Repositories;
4
5
use Gameap\Exceptions\Repositories\GdaemonTaskRepository\EmptyServerStartCommandException;
6
use Gameap\Exceptions\Repositories\GdaemonTaskRepository\GdaemonTaskRepositoryException;
7
use Gameap\Exceptions\Repositories\GdaemonTaskRepository\InvalidServerStartCommandException;
8
use Gameap\Exceptions\Repositories\RecordExistExceptions;
9
use Gameap\Models\GdaemonTask;
10
use Gameap\Models\Server;
11
use Illuminate\Support\Facades\DB;
12
use PDO;
13
14
class GdaemonTaskRepository extends Repository
15
{
16
    public function __construct(GdaemonTask $gdaemonTask)
17
    {
18
        $this->model = $gdaemonTask;
19
    }
20 9
21
    public function getAll($perPage = 20)
22 9
    {
23 9
        return GdaemonTask::orderBy('id', 'DESC')->paginate($perPage);
24
    }
25
26
    /**
27
     * Create new starting of game server task
28
     *
29
     * @param Server $server
30
     * @param int $runAftId
31
     * @return int Gdaemon Task ID
32
     *
33
     * @throws RecordExistExceptions
34
     * @throws InvalidServerStartCommandException
35
     * @throws EmptyServerStartCommandException
36
     */
37
    public function addServerStart(Server $server, int $runAftId = 0)
38
    {
39
        $this->workingTaskNotExistOrFail($server, GdaemonTask::TASK_SERVER_START, 'Server start task is already exists');
40
        $this->serverCommandCorrectOrFail($server);
41
42
        return GdaemonTask::create([
43
            'run_aft_id'          => $runAftId,
44
            'dedicated_server_id' => $server->ds_id,
45
            'server_id'           => $server->id,
46
            'task'                => GdaemonTask::TASK_SERVER_START,
47
        ])->id;
48
    }
49
50
    /**
51
     * Create new stopping of game server task
52
     *
53
     * @param Server $server
54
     * @param int $runAftId
55
     * @return int Gdaemon Task ID
56
     *
57
     * @throws RecordExistExceptions
58
     */
59
    public function addServerStop(Server $server, int $runAftId = 0)
60
    {
61
        $this->workingTaskNotExistOrFail($server, GdaemonTask::TASK_SERVER_STOP, 'Server stop task is already exists');
62
63
        return GdaemonTask::create([
64
            'run_aft_id'          => $runAftId,
65
            'dedicated_server_id' => $server->ds_id,
66
            'server_id'           => $server->id,
67
            'task'                => GdaemonTask::TASK_SERVER_STOP,
68
        ])->id;
69
    }
70
71
    /**
72
     * @param Server $server
73
     * @param int    $runAftId
74
     *
75
     * @return int Gdaemon Task ID
76
     *
77
     * @throws RecordExistExceptions
78
     * @throws InvalidServerStartCommandException
79
     * @throws EmptyServerStartCommandException
80
     */
81
    public function addServerRestart(Server $server, int $runAftId = 0)
82
    {
83
        $this->workingTaskNotExistOrFail($server, GdaemonTask::TASK_SERVER_RESTART, 'Server restart task is already exists');
84
        $this->serverCommandCorrectOrFail($server);
85
86
        return GdaemonTask::create([
87
            'run_aft_id'          => $runAftId,
88
            'dedicated_server_id' => $server->ds_id,
89
            'server_id'           => $server->id,
90
            'task'                => GdaemonTask::TASK_SERVER_RESTART,
91
        ])->id;
92
    }
93
94
    /**
95
     * @param Server $server
96
     * @param int    $runAftId
97
     *
98
     * @return int Gdaemon Task ID
99
     *
100
     * @throws RecordExistExceptions
101
     */
102
    public function addServerUpdate(Server $server, int $runAftId = 0)
103
    {
104
        $this->workingTaskNotExistOrFail(
105
            $server,
106
            [GdaemonTask::TASK_SERVER_UPDATE, GdaemonTask::TASK_SERVER_INSTALL],
107
            'Server update/install task is already exists'
108
        );
109
        
110
        return GdaemonTask::create([
111
            'run_aft_id'          => $runAftId,
112
            'dedicated_server_id' => $server->ds_id,
113
            'server_id'           => $server->id,
114
            'task'                => GdaemonTask::TASK_SERVER_UPDATE,
115
        ])->id;
116
    }
117
118
    public function addServerInstall(Server $server, int $runAftId = 0)
119
    {
120
        $this->workingTaskNotExistOrFail(
121
            $server,
122
            [GdaemonTask::TASK_SERVER_UPDATE, GdaemonTask::TASK_SERVER_INSTALL],
123
            'Server update/install task is already exists'
124
        );
125
126
        return GdaemonTask::create([
127
            'run_aft_id'          => $runAftId,
128
            'dedicated_server_id' => $server->ds_id,
129
            'server_id'           => $server->id,
130
            'task'                => GdaemonTask::TASK_SERVER_INSTALL,
131
        ])->id;
132
    }
133
134
    /**
135
     * Remove server files
136
     *
137
     * @param Server $server
138
     * @param int    $runAftId
139
     * @return int Gdaemon Task ID
140
     *
141
     * @throws RecordExistExceptions
142
     */
143
    public function addServerDelete(Server $server, int $runAftId = 0)
144
    {
145
        $this->workingTaskNotExistOrFail($server, GdaemonTask::TASK_SERVER_DELETE, 'Server delete task is already exists');
146
        
147
        return GdaemonTask::create([
148
            'run_aft_id'          => $runAftId,
149
            'dedicated_server_id' => $server->ds_id,
150
            'server_id'           => $server->id,
151
            'task'                => GdaemonTask::TASK_SERVER_DELETE,
152
        ])->id;
153
    }
154
155
    /**
156
     * @param $cmd
157
     * @param $dedicatedServerId
158
     * @param int $runAftId
159
     * @return mixed
160
     */
161
    public function addCmd($cmd, $dedicatedServerId, int $runAftId = 0)
162
    {
163
        return GdaemonTask::create([
164
            'run_aft_id'          => $runAftId,
165
            'dedicated_server_id' => $dedicatedServerId,
166
            'task'                => GdaemonTask::TASK_CMD_EXEC,
167
            'cmd'                 => $cmd,
168
        ])->id;
169
    }
170
171
    /**
172
     * @param int $serverId
173
     * @param string|array $task
174
     * @param string|array $status
175
     *
176
     * @return mixed
177
     */
178
    public function getTasks(int $serverId, $task, $status)
179
    {
180
        if (is_array($task)) {
181
            $taskQuery = GdaemonTask::whereIn('task', $task)->where([['server_id', '=', $serverId]]);
182
        } else {
183
            $taskQuery = GdaemonTask::where([
184
                ['task', '=', $task],
185
                ['server_id', '=', $serverId],
186
            ]);
187
        }
188
189
        $taskQuery = is_array($status)
190
            ? $taskQuery->whereIn('status', $status)
191
            : $taskQuery->where('status', '=', $status);
192
193
        return $taskQuery->get();
194
    }
195
196
    /**
197
     * Get working or waiting task id. Return 0 if task is not one
198
     *
199
     * @param int $serverId
200
     * @param $task
201
     * @return int
202
     */
203
    public function getOneWorkingTaskId(int $serverId, $task)
204
    {
205
        $tasks = $this->getTasks($serverId, $task, [GdaemonTask::STATUS_WAITING, GdaemonTask::STATUS_WORKING]);
206
207
        return (count($tasks) === 1)
208
            ? $tasks->first()->id
209
            : 0;
210
    }
211
212
    /**
213
     * @param GdaemonTask $gdaemonTask
214
     * @param string $output
215
     */
216
    public function concatOutput(GdaemonTask $gdaemonTask, string $output): void
217
    {
218
        if (empty($output)) {
219
            return;
220
        }
221
222
        $qoutedOutput = DB::connection()->getPdo()->quote($output);
223
224
        $dbDriver = DB::connection()->getPDO()->getAttribute(PDO::ATTR_DRIVER_NAME);
225
226
        if ($dbDriver === 'mysql') {
227
            $gdaemonTask->update(['output' => DB::raw("CONCAT(IFNULL(output,''), {$qoutedOutput})")]);
228
        } elseif ($dbDriver === 'sqlite' || $dbDriver === 'pgsql') {
229
            $gdaemonTask->update(['output' => DB::raw("COALESCE(output, '') || {$qoutedOutput}")]);
230
        } else {
231
            $gdaemonTask->update(['output' => $gdaemonTask->output . $output]);
232
        }
233
    }
234
235
    /**
236
     * @param GdaemonTask $gdaemonTask
237
     *
238
     * @throws GdaemonTaskRepositoryException
239
     */
240
    public function cancel(GdaemonTask $gdaemonTask): void
241
    {
242
        if ($gdaemonTask->status != GdaemonTask::STATUS_WAITING) {
243
            throw new GdaemonTaskRepositoryException(__('gdaemon_tasks.cancel_fail_cannot_be_canceled'));
0 ignored issues
show
Bug introduced by
It seems like __('gdaemon_tasks.cancel...il_cannot_be_canceled') can also be of type array and array; however, parameter $message of Gameap\Exceptions\Reposi...xception::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

243
            throw new GdaemonTaskRepositoryException(/** @scrutinizer ignore-type */ __('gdaemon_tasks.cancel_fail_cannot_be_canceled'));
Loading history...
244
        }
245
246
        $gdaemonTask->status = GdaemonTask::STATUS_CANCELED;
247
        $gdaemonTask->save();
248
    }
249
250
    /**
251
     * @param Server $server
252
     * @param string|array $task task name
253
     * @param string $failMsg Failure message
254
     *
255
     * @throws RecordExistExceptions
256
     */
257
    private function workingTaskNotExistOrFail(Server $server, $task, $failMsg = 'Task is already exists'): void
258
    {
259
        if (is_array($task)) {
260
            $taskQuery = GdaemonTask::whereIn('task', $task)->where([['server_id', '=', $server->id]]);
261
        } else {
262
            $taskQuery = GdaemonTask::where([
263
                ['task', '=', $task],
264
                ['server_id', '=', $server->id],
265
                ['dedicated_server_id', '=', $server->ds_id],
266
            ]);
267
        }
268
269
        $taskExist = $taskQuery->whereIn('status', [
270
            GdaemonTask::STATUS_WAITING,
271
            GdaemonTask::STATUS_WORKING,
272
        ])->exists();
273
274
        if ($taskExist) {
275
            throw new RecordExistExceptions($failMsg);
276
        }
277
    }
278
279
    /**
280
     * @param Server $server
281
     *
282
     * @throws InvalidServerStartCommandException
283
     * @throws EmptyServerStartCommandException
284
     */
285
    private function serverCommandCorrectOrFail(Server $server): void
286
    {
287
        if (empty($server->start_command)) {
288
            throw new EmptyServerStartCommandException(__('gdaemon_tasks.empty_server_start_command'));
0 ignored issues
show
Bug introduced by
It seems like __('gdaemon_tasks.empty_server_start_command') can also be of type array and array; however, parameter $message of Gameap\Exceptions\Reposi...xception::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

288
            throw new EmptyServerStartCommandException(/** @scrutinizer ignore-type */ __('gdaemon_tasks.empty_server_start_command'));
Loading history...
289
        }
290
    }
291
}
292