misc_pterodactyl-panel/app/Services/Eggs/Sharing/EggImporterService.php

51 lines
1.5 KiB
PHP
Raw Normal View History

2017-10-04 04:31:04 +00:00
<?php
2017-10-07 21:19:07 +00:00
namespace Pterodactyl\Services\Eggs\Sharing;
2017-10-04 04:31:04 +00:00
use Ramsey\Uuid\Uuid;
use Illuminate\Support\Arr;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\Nest;
2017-10-04 04:31:04 +00:00
use Illuminate\Http\UploadedFile;
use Pterodactyl\Models\EggVariable;
2017-10-04 04:31:04 +00:00
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Services\Eggs\EggParserService;
2017-10-04 04:31:04 +00:00
class EggImporterService
2017-10-04 04:31:04 +00:00
{
public function __construct(protected ConnectionInterface $connection, protected EggParserService $parser)
{
2017-10-04 04:31:04 +00:00
}
/**
* Take an uploaded JSON file and parse it into a new egg.
2017-10-04 04:31:04 +00:00
*
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException|\Throwable
2017-10-04 04:31:04 +00:00
*/
public function handle(UploadedFile $file, int $nest): Egg
2017-10-04 04:31:04 +00:00
{
$parsed = $this->parser->handle($file);
2017-10-04 04:31:04 +00:00
/** @var \Pterodactyl\Models\Nest $nest */
$nest = Nest::query()->with('eggs', 'eggs.variables')->findOrFail($nest);
2017-10-04 04:31:04 +00:00
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,
]);
2017-10-04 04:31:04 +00:00
$egg = $this->parser->fillFromParsed($egg, $parsed);
$egg->save();
foreach ($parsed['variables'] ?? [] as $variable) {
EggVariable::query()->forceCreate(array_merge($variable, ['egg_id' => $egg->id]));
}
2017-10-04 04:31:04 +00:00
return $egg;
2017-10-04 04:31:04 +00:00
});
}
2017-10-04 04:31:04 +00:00
}