forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEggUpdateImporterService.php
More file actions
50 lines (41 loc) · 1.61 KB
/
EggUpdateImporterService.php
File metadata and controls
50 lines (41 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace Pterodactyl\Services\Eggs\Sharing;
use Pterodactyl\Models\Egg;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;
use Pterodactyl\Models\EggVariable;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Services\Eggs\EggParserService;
class EggUpdateImporterService
{
/**
* EggUpdateImporterService constructor.
*/
public function __construct(protected ConnectionInterface $connection, protected EggParserService $parser)
{
}
/**
* Update an existing Egg using an uploaded JSON file.
*
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException|\Throwable
*/
public function handle(Egg $egg, UploadedFile $file): Egg
{
$parsed = $this->parser->handle($file);
return $this->connection->transaction(function () use ($egg, $parsed) {
$egg = $this->parser->fillFromParsed($egg, $parsed);
$egg->save();
// Update existing variables or create new ones.
foreach ($parsed['variables'] ?? [] as $variable) {
EggVariable::unguarded(function () use ($egg, $variable) {
$egg->variables()->updateOrCreate([
'env_variable' => $variable['env_variable'],
], Collection::make($variable)->except('egg_id', 'env_variable')->toArray());
});
}
$imported = array_map(fn ($value) => $value['env_variable'], $parsed['variables'] ?? []);
$egg->variables()->whereNotIn('env_variable', $imported)->delete();
return $egg->refresh();
});
}
}