|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gameap\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\CarbonInterval; |
|
6
|
|
|
use Gameap\Exceptions\Repositories\RepositoryValidationException; |
|
7
|
|
|
use Gameap\Models\ServerTask; |
|
8
|
|
|
|
|
9
|
|
|
class ServersTasksRepository |
|
10
|
|
|
{ |
|
11
|
|
|
const MIN_PERIOD_IN_SECONDS = 600; |
|
12
|
|
|
|
|
13
|
|
|
/** @var ServerTask */ |
|
14
|
|
|
protected $model; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(ServerTask $serverTask) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->model = $serverTask; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param int $serverId |
|
23
|
|
|
* @return array |
|
24
|
|
|
*/ |
|
25
|
|
|
public function getTasks(int $serverId): array |
|
26
|
|
|
{ |
|
27
|
|
|
$tasks = []; |
|
28
|
|
|
|
|
29
|
|
|
/** @var ServerTask $task */ |
|
30
|
|
|
foreach (ServerTask::where('server_id', $serverId)->get() as $task) { |
|
31
|
|
|
$tasks[] = $this->convertModelToArray($task); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return $tasks; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param int $taskId |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
|
|
public function get(int $taskId): array |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->convertModelToArray(ServerTask::find($taskId)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param array $task |
|
48
|
|
|
* @return int |
|
49
|
|
|
* @throws RepositoryValidationException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function store(array $task): int |
|
52
|
|
|
{ |
|
53
|
|
|
$this->validate($task); |
|
54
|
|
|
$convertedTask = $this->convert($task); |
|
55
|
|
|
|
|
56
|
|
|
$serverTask = ServerTask::create($convertedTask); |
|
57
|
|
|
|
|
58
|
|
|
return $serverTask->id; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param int $taskId |
|
63
|
|
|
* @param array $task |
|
64
|
|
|
* @throws RepositoryValidationException |
|
65
|
|
|
*/ |
|
66
|
|
|
public function update(int $taskId, array $task) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->validate($task); |
|
69
|
|
|
$convertedTask = $this->convert($task); |
|
70
|
|
|
|
|
71
|
|
|
ServerTask::where('id', $taskId)->update($convertedTask); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param array $task |
|
76
|
|
|
* @return array |
|
77
|
|
|
* @throws RepositoryValidationException |
|
78
|
|
|
*/ |
|
79
|
|
|
private function convert(array $task) { |
|
80
|
|
|
|
|
81
|
|
|
if ($task['repeat'] != 1) { |
|
82
|
|
|
if (empty($task['repeat_period'])) { |
|
83
|
|
|
throw new RepositoryValidationException(__('servers_tasks.errors.empty_period')); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$task['repeat_period'] = CarbonInterval::fromString($task['repeat_period']) |
|
87
|
|
|
->locale('en') |
|
88
|
|
|
->cascade() |
|
89
|
|
|
->totalSeconds; |
|
90
|
|
|
|
|
91
|
|
|
if ($task['repeat_period'] < self::MIN_PERIOD_IN_SECONDS) { |
|
92
|
|
|
throw new RepositoryValidationException(__('servers_tasks.errors.minimum_period')); |
|
93
|
|
|
} |
|
94
|
|
|
} else { |
|
95
|
|
|
$task['repeat_period'] = 0; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $task; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param array $task |
|
103
|
|
|
* @throws RepositoryValidationException |
|
104
|
|
|
*/ |
|
105
|
|
|
private function validate(array $task) { |
|
106
|
|
|
if (empty($task) || empty($task['command'])) { |
|
107
|
|
|
throw new RepositoryValidationException(__('servers_tasks.errors.empty_command')); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param ServerTask $task |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
private function convertModelToArray(ServerTask $task) |
|
116
|
|
|
{ |
|
117
|
|
|
return [ |
|
118
|
|
|
'id' => $task->id, |
|
119
|
|
|
'command' => $task->command, |
|
120
|
|
|
'server_id' => $task->server_id, |
|
121
|
|
|
'repeat' => $task->repeat, |
|
122
|
|
|
'repeat_period' => CarbonInterval::seconds($task->repeat_period) |
|
|
|
|
|
|
123
|
|
|
->locale('en') |
|
124
|
|
|
->cascade() |
|
125
|
|
|
->forHumans(), |
|
126
|
|
|
'execute_date' => $task->execute_date, |
|
127
|
|
|
'payload' => $task->payload, |
|
128
|
|
|
'created_at' => $task->created_at, |
|
129
|
|
|
'updated_at' => $task->updated_at |
|
130
|
|
|
]; |
|
131
|
|
|
} |
|
132
|
|
|
} |