2017-10-09 04:50:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Eggs\Sharing;
|
|
|
|
|
2020-10-06 04:29:35 +00:00
|
|
|
use Pterodactyl\Models\Egg;
|
2017-10-09 04:50:52 +00:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2022-05-15 18:40:19 +00:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Pterodactyl\Models\EggVariable;
|
2017-10-09 04:50:52 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2022-05-15 18:40:19 +00:00
|
|
|
use Pterodactyl\Services\Eggs\EggParserService;
|
2017-10-09 04:50:52 +00:00
|
|
|
|
|
|
|
class EggUpdateImporterService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* EggUpdateImporterService constructor.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function __construct(protected ConnectionInterface $connection, protected EggParserService $parser)
|
2022-05-15 18:40:19 +00:00
|
|
|
{
|
2017-10-09 04:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an existing Egg using an uploaded JSON file.
|
|
|
|
*
|
2022-05-15 18:40:19 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException|\Throwable
|
2017-10-09 04:50:52 +00:00
|
|
|
*/
|
2022-05-15 18:40:19 +00:00
|
|
|
public function handle(Egg $egg, UploadedFile $file): Egg
|
2017-10-09 04:50:52 +00:00
|
|
|
{
|
2022-05-15 18:40:19 +00:00
|
|
|
$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());
|
|
|
|
});
|
|
|
|
}
|
2017-10-09 04:50:52 +00:00
|
|
|
|
2022-05-15 18:40:19 +00:00
|
|
|
$imported = array_map(fn ($value) => $value['env_variable'], $parsed['variables'] ?? []);
|
2017-10-09 04:50:52 +00:00
|
|
|
|
2022-05-15 18:40:19 +00:00
|
|
|
$egg->variables()->whereNotIn('env_variable', $imported)->delete();
|
2017-10-09 04:50:52 +00:00
|
|
|
|
2022-05-15 18:40:19 +00:00
|
|
|
return $egg->refresh();
|
2017-10-09 04:50:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|