2017-08-12 20:29:01 +00:00
|
|
|
<?php
|
|
|
|
|
2017-10-07 21:16:51 +00:00
|
|
|
namespace Pterodactyl\Services\Eggs\Variables;
|
2017-08-12 20:29:01 +00:00
|
|
|
|
2020-05-08 03:40:19 +00:00
|
|
|
use Illuminate\Support\Str;
|
2022-12-15 00:05:46 +00:00
|
|
|
use Pterodactyl\Models\Egg;
|
2017-10-07 04:57:53 +00:00
|
|
|
use Pterodactyl\Models\EggVariable;
|
2017-08-12 20:29:01 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
2018-03-17 20:09:09 +00:00
|
|
|
use Pterodactyl\Traits\Services\ValidatesValidationRules;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
|
2017-10-07 21:16:51 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
|
|
|
|
use Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException;
|
2017-08-12 20:29:01 +00:00
|
|
|
|
|
|
|
class VariableUpdateService
|
|
|
|
{
|
2018-03-17 20:09:09 +00:00
|
|
|
use ValidatesValidationRules;
|
|
|
|
|
2017-08-13 19:55:09 +00:00
|
|
|
/**
|
|
|
|
* VariableUpdateService constructor.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function __construct(private EggVariableRepositoryInterface $repository, private ValidationFactory $validator)
|
2017-08-12 20:29:01 +00:00
|
|
|
{
|
2018-03-17 20:09:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the validation factory instance to be used by rule validation
|
|
|
|
* checking in the trait.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
protected function getValidator(): ValidationFactory
|
2018-03-17 20:09:09 +00:00
|
|
|
{
|
|
|
|
return $this->validator;
|
2017-08-12 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-07 21:16:51 +00:00
|
|
|
* Update a specific egg variable.
|
2017-08-12 20:29:01 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
2017-10-07 21:16:51 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException
|
2017-08-12 20:29:01 +00:00
|
|
|
*/
|
2022-12-15 00:05:46 +00:00
|
|
|
public function handle(Egg $egg, array $data): void
|
2017-08-12 20:29:01 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!is_null(array_get($data, 'env_variable'))) {
|
2017-10-07 04:57:53 +00:00
|
|
|
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')]));
|
2017-08-12 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
$count = $egg->variables()
|
|
|
|
->where('egg_variables.env_variable', $data['env_variable'])
|
|
|
|
->where('egg_variables.id', '!=', $data['id'])
|
|
|
|
->count();
|
2017-08-12 20:29:01 +00:00
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
if ($count > 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')]));
|
2017-08-12 20:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!empty($data['rules'] ?? '')) {
|
2020-05-08 03:40:19 +00:00
|
|
|
$this->validateRules(
|
|
|
|
(is_string($data['rules']) && Str::contains($data['rules'], ';;'))
|
|
|
|
? explode(';;', $data['rules'])
|
|
|
|
: $data['rules']
|
|
|
|
);
|
2018-03-17 20:09:09 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 01:56:42 +00:00
|
|
|
$options = array_get($data, 'options') ?? [];
|
2017-08-12 20:29:01 +00:00
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
$egg->variables()->where('egg_variables.id', $data['id'])->update([
|
2018-02-17 19:37:53 +00:00
|
|
|
'name' => $data['name'] ?? '',
|
|
|
|
'description' => $data['description'] ?? '',
|
|
|
|
'env_variable' => $data['env_variable'] ?? '',
|
|
|
|
'default_value' => $data['default_value'] ?? '',
|
2022-12-15 00:05:46 +00:00
|
|
|
'user_viewable' => $data['user_viewable'],
|
|
|
|
'user_editable' => $data['user_editable'],
|
2018-02-17 19:37:53 +00:00
|
|
|
'rules' => $data['rules'] ?? '',
|
|
|
|
]);
|
2017-08-12 20:29:01 +00:00
|
|
|
}
|
|
|
|
}
|