Passed
Push — develop ( 07cb21...42e7d4 )
by Nikita
10:20
created

GdaemonTaskRepository::cancel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Gameap\Repositories;
4
5
use Gameap\Exceptions\Repositories\GdaemonTaskRepositoryException;
6
use Gameap\Models\Server;
7
use Gameap\Models\GdaemonTask;
8
use Gameap\Models\DedicatedServer;
9
use Gameap\Exceptions\Repositories\RecordExistExceptions;
10
use Illuminate\Support\Facades\DB;
11
use PDO;
12
13
/**
14
 * Class GdaemonTaskRepository
15
*/
16
class GdaemonTaskRepository
17
{
18
    protected $model;
19
20
    public function __construct(GdaemonTask $gdaemonTask)
21
    {
22
        $this->model = $gdaemonTask;
23
    }
24
    
25
    public function getAll($perPage = 20)
26
    {
27
        $gdaemonTasks = GdaemonTask::orderBy('id', 'DESC')->paginate($perPage);
28
        return $gdaemonTasks;
29
    }
30
31
    public function getById(int $id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

31
    public function getById(/** @scrutinizer ignore-unused */ int $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
    {
33
34
    }
35
36
    /**
37
     * Create new starting of game server task
38
     *
39
     * @param Server $server
40
     * @param int $runAftId
41
     * @return int Gdaemon Task ID
42
     */
43
    public function addServerStart(Server $server, int $runAftId = 0)
44
    {
45
        $this->workingTaskNotExistOrFail($server, GdaemonTask::TASK_SERVER_START, 'Server start task is already exists');
46
        
47
        return GdaemonTask::create([
48
            'run_aft_id' => $runAftId,
49
            'dedicated_server_id' => $server->ds_id,
50
            'server_id' => $server->id,
51
            'task' => GdaemonTask::TASK_SERVER_START,
52
        ])->id;
53
    }
54
55
    /**
56
     * Create new stopping of game server task
57
     *
58
     * @param Server $server
59
     * @param int $runAftId
60
     * @return int Gdaemon Task ID
61
     */
62
    public function addServerStop(Server $server, int $runAftId = 0)
63
    {
64
        $this->workingTaskNotExistOrFail($server, GdaemonTask::TASK_SERVER_STOP, 'Server stop task is already exists');
65
66
        return GdaemonTask::create([
67
            'run_aft_id' => $runAftId,
68
            'dedicated_server_id' => $server->ds_id,
69
            'server_id' => $server->id,
70
            'task' => GdaemonTask::TASK_SERVER_STOP,
71
        ])->id;
72
    }
73
74
    /**
75
     * @param Server $server
76
     * @param int    $runAftId
77
     *
78
     * @return int Gdaemon Task ID
79
     */
80
    public function addServerRestart(Server $server, int $runAftId = 0)
81
    {
82
        $this->workingTaskNotExistOrFail($server, GdaemonTask::TASK_SERVER_RESTART, 'Server restart task is already exists');
83
        
84
        return GdaemonTask::create([
85
            'run_aft_id' => $runAftId,
86
            'dedicated_server_id' => $server->ds_id,
87
            'server_id' => $server->id,
88
            'task' => GdaemonTask::TASK_SERVER_RESTART,
89
        ])->id;
90
    }
91
92
    /**
93
     * @param Server $server
94
     * @param int    $runAftId
95
     *
96
     * @return int Gdaemon Task ID
97
     */
98
    public function addServerUpdate(Server $server, int $runAftId = 0)
99
    {
100
        $this->workingTaskNotExistOrFail($server, GdaemonTask::TASK_SERVER_UPDATE, 'Server update/install task is already exists');
101
        
102
        return GdaemonTask::create([
103
            'run_aft_id' => $runAftId,
104
            'dedicated_server_id' => $server->ds_id,
105
            'server_id' => $server->id,
106
            'task' => GdaemonTask::TASK_SERVER_UPDATE,
107
        ])->id;
108
    }
109
110
    /**
111
     * Remove server files
112
     *
113
     * @param Server $server
114
     * @param int    $runAftId
115
     * @return int Gdaemon Task ID
116
     * @throws RecordExistExceptions
117
     */
118
    public function addServerDelete(Server $server, int $runAftId = 0)
119
    {
120
        $this->workingTaskNotExistOrFail($server, GdaemonTask::TASK_SERVER_DELETE, 'Server delete task is already exists');
121
        
122
        return GdaemonTask::create([
123
            'run_aft_id' => $runAftId,
124
            'dedicated_server_id' => $server->ds_id,
125
            'server_id' => $server->id,
126
            'task' => GdaemonTask::TASK_SERVER_DELETE,
127
        ])->id;
128
    }
129
130
    /**
131
     * @param GdaemonTask $gdaemonTask
132
     * @param string $output
133
     */
134
    public function concatOutput(GdaemonTask $gdaemonTask, string $output)
135
    {
136
        if (empty($output)) {
137
            return;
138
        }
139
140
        $qoutedOutput = DB::connection()->getPdo()->quote($output);
141
142
        $dbDriver = DB::connection()->getPDO()->getAttribute(PDO::ATTR_DRIVER_NAME);
143
144
        if ($dbDriver == 'mysql') {
145
            $gdaemonTask->update(['output' => DB::raw("CONCAT(IFNULL(output,''), {$qoutedOutput})")]);
146
        } else if ($dbDriver == 'sqlite' || $dbDriver == 'pgsql') {
147
            $gdaemonTask->update(['output' => DB::raw("COALESCE(output, '') || {$qoutedOutput}")]);
148
        } else {
149
            $gdaemonTask->update(['output' => $gdaemonTask->output . $output]);
150
        }
151
    }
152
153
    /**
154
     * @param GdaemonTask $gdaemonTask
155
     * @throws GdaemonTaskRepositoryException
156
     */
157
    public function cancel(GdaemonTask $gdaemonTask)
158
    {
159
        if ($gdaemonTask->status != GdaemonTask::STATUS_WAITING) {
160
            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; 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

160
            throw new GdaemonTaskRepositoryException(/** @scrutinizer ignore-type */ __('gdaemon_tasks.cancel_fail_cannot_be_canceled'));
Loading history...
161
        }
162
163
        $gdaemonTask->status = GdaemonTask::STATUS_CANCELED;
164
        $gdaemonTask->save();
165
    }
166
167
    /**
168
     * @param Server $server
169
     * @param string|array $task task name
170
     * @param string $failMsg Failure message
171
     *
172
     * @throws RecordExistExceptions
173
     */
174
    private function workingTaskNotExistOrFail(Server $server, $task, $failMsg = 'Task is already exists')
175
    {
176
        if (is_array($task)) {
177
            $taskQuery = GdaemonTask::whereIn(['task', $task])->where([['server_id', '=', $server->id]]);
178
        } else {
179
            $taskQuery = GdaemonTask::where([
180
                ['task', '=', $task],
181
                ['server_id', '=', $server->id],
182
                ['dedicated_server_id', '=', $server->ds_id]
183
            ]);
184
        }
185
186
        $taskExist = $taskQuery->whereIn('status', [
187
            GdaemonTask::STATUS_WAITING, 
188
            GdaemonTask::STATUS_WORKING
189
        ])->exists();
190
191
        if ($taskExist) {
192
            throw new RecordExistExceptions($failMsg);
193
        }
194
    }
195
}
196