forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEggImporterService.php
More file actions
50 lines (41 loc) · 1.49 KB
/
EggImporterService.php
File metadata and controls
50 lines (41 loc) · 1.49 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 Ramsey\Uuid\Uuid;
use Illuminate\Support\Arr;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\Nest;
use Illuminate\Http\UploadedFile;
use Pterodactyl\Models\EggVariable;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Services\Eggs\EggParserService;
class EggImporterService
{
public function __construct(protected ConnectionInterface $connection, protected EggParserService $parser)
{
}
/**
* Take an uploaded JSON file and parse it into a new egg.
*
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException|\Throwable
*/
public function handle(UploadedFile $file, int $nest): Egg
{
$parsed = $this->parser->handle($file);
/** @var Nest $nest */
$nest = Nest::query()->with('eggs', 'eggs.variables')->findOrFail($nest);
return $this->connection->transaction(function () use ($nest, $parsed) {
$egg = (new Egg())->forceFill([
'uuid' => Uuid::uuid4()->toString(),
'nest_id' => $nest->id,
'author' => Arr::get($parsed, 'author'),
'copy_script_from' => null,
]);
$egg = $this->parser->fillFromParsed($egg, $parsed);
$egg->save();
foreach ($parsed['variables'] ?? [] as $variable) {
EggVariable::query()->forceCreate(array_merge($variable, ['egg_id' => $egg->id]));
}
return $egg;
});
}
}