misc_pterodactyl-panel/app/Services/Eggs/Variables/VariableUpdateService.php

80 lines
2.9 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Services\Eggs\Variables;
use Illuminate\Support\Str;
use Pterodactyl\Models\EggVariable;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Traits\Services\ValidatesValidationRules;
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
use Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException;
class VariableUpdateService
{
use ValidatesValidationRules;
2017-08-13 19:55:09 +00:00
/**
* VariableUpdateService constructor.
*/
public function __construct(private EggVariableRepositoryInterface $repository, private ValidationFactory $validator)
{
}
/**
* Return the validation factory instance to be used by rule validation
* checking in the trait.
*/
protected function getValidator(): ValidationFactory
{
return $this->validator;
}
/**
* Update a specific egg variable.
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
2017-08-13 19:55:09 +00:00
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException
*/
public function handle(EggVariable $variable, array $data): mixed
{
2021-01-23 20:33:34 +00:00
if (!is_null(array_get($data, 'env_variable'))) {
if (in_array(strtoupper(array_get($data, 'env_variable')), explode(',', EggVariable::RESERVED_ENV_NAMES))) {
2021-01-23 20:33:34 +00:00
throw new ReservedVariableNameException(trans('exceptions.service.variables.reserved_name', ['name' => array_get($data, 'env_variable')]));
}
$search = $this->repository->setColumns('id')->findCountWhere([
2018-02-17 19:37:53 +00:00
['env_variable', '=', $data['env_variable']],
['egg_id', '=', $variable->egg_id],
['id', '!=', $variable->id],
]);
if ($search > 0) {
2021-01-23 20:33:34 +00:00
throw new DisplayException(trans('exceptions.service.variables.env_not_unique', ['name' => array_get($data, 'env_variable')]));
}
}
2021-01-23 20:33:34 +00:00
if (!empty($data['rules'] ?? '')) {
$this->validateRules(
(is_string($data['rules']) && Str::contains($data['rules'], ';;'))
? explode(';;', $data['rules'])
: $data['rules']
);
}
2017-12-31 01:56:42 +00:00
$options = array_get($data, 'options') ?? [];
2018-02-17 19:37:53 +00:00
return $this->repository->withoutFreshModel()->update($variable->id, [
'name' => $data['name'] ?? '',
'description' => $data['description'] ?? '',
'env_variable' => $data['env_variable'] ?? '',
'default_value' => $data['default_value'] ?? '',
2017-08-13 19:55:09 +00:00
'user_viewable' => in_array('user_viewable', $options),
'user_editable' => in_array('user_editable', $options),
2018-02-17 19:37:53 +00:00
'rules' => $data['rules'] ?? '',
]);
}
}