Path: blob/1.0-develop/app/Services/Eggs/Variables/VariableUpdateService.php
10287 views
<?php12namespace Pterodactyl\Services\Eggs\Variables;34use Illuminate\Support\Str;5use Pterodactyl\Models\EggVariable;6use Pterodactyl\Exceptions\DisplayException;7use Pterodactyl\Traits\Services\ValidatesValidationRules;8use Illuminate\Contracts\Validation\Factory as ValidationFactory;9use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;10use Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException;1112class VariableUpdateService13{14use ValidatesValidationRules;1516/**17* VariableUpdateService constructor.18*/19public function __construct(private EggVariableRepositoryInterface $repository, private ValidationFactory $validator)20{21}2223/**24* Return the validation factory instance to be used by rule validation25* checking in the trait.26*/27protected function getValidator(): ValidationFactory28{29return $this->validator;30}3132/**33* Update a specific egg variable.34*35* @throws DisplayException36* @throws \Pterodactyl\Exceptions\Model\DataValidationException37* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException38* @throws ReservedVariableNameException39*/40public function handle(EggVariable $variable, array $data): mixed41{42if (!is_null(array_get($data, 'env_variable'))) {43if (in_array(strtoupper(array_get($data, 'env_variable')), explode(',', EggVariable::RESERVED_ENV_NAMES))) {44throw new ReservedVariableNameException(trans('exceptions.service.variables.reserved_name', ['name' => array_get($data, 'env_variable')]));45}4647$search = $this->repository->setColumns('id')->findCountWhere([48['env_variable', '=', $data['env_variable']],49['egg_id', '=', $variable->egg_id],50['id', '!=', $variable->id],51]);5253if ($search > 0) {54throw new DisplayException(trans('exceptions.service.variables.env_not_unique', ['name' => array_get($data, 'env_variable')]));55}56}5758if (!empty($data['rules'] ?? '')) {59$this->validateRules(60(is_string($data['rules']) && Str::contains($data['rules'], ';;'))61? explode(';;', $data['rules'])62: $data['rules']63);64}6566$options = array_get($data, 'options') ?? [];6768return $this->repository->withoutFreshModel()->update($variable->id, [69'name' => $data['name'] ?? '',70'description' => $data['description'] ?? '',71'env_variable' => $data['env_variable'] ?? '',72'default_value' => $data['default_value'] ?? '',73'user_viewable' => in_array('user_viewable', $options),74'user_editable' => in_array('user_editable', $options),75'rules' => $data['rules'] ?? '',76]);77}78}798081