Path: blob/1.0-develop/app/Services/Eggs/Variables/VariableCreationService.php
10294 views
<?php12namespace Pterodactyl\Services\Eggs\Variables;34use Pterodactyl\Models\EggVariable;5use Pterodactyl\Traits\Services\ValidatesValidationRules;6use Illuminate\Contracts\Validation\Factory as ValidationFactory;7use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;8use Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException;910class VariableCreationService11{12use ValidatesValidationRules;1314/**15* VariableCreationService constructor.16*/17public function __construct(private EggVariableRepositoryInterface $repository, private ValidationFactory $validator)18{19}2021/**22* Return the validation factory instance to be used by rule validation23* checking in the trait.24*/25protected function getValidator(): ValidationFactory26{27return $this->validator;28}2930/**31* Create a new variable for a given Egg.32*33* @throws \Pterodactyl\Exceptions\Model\DataValidationException34* @throws \Pterodactyl\Exceptions\Service\Egg\Variable\BadValidationRuleException35* @throws ReservedVariableNameException36*/37public function handle(int $egg, array $data): EggVariable38{39if (in_array(strtoupper(array_get($data, 'env_variable')), explode(',', EggVariable::RESERVED_ENV_NAMES))) {40throw new ReservedVariableNameException(sprintf('Cannot use the protected name %s for this environment variable.', array_get($data, 'env_variable')));41}4243if (!empty($data['rules'] ?? '')) {44$this->validateRules($data['rules']);45}4647$options = array_get($data, 'options') ?? [];4849return $this->repository->create([50'egg_id' => $egg,51'name' => $data['name'] ?? '',52'description' => $data['description'] ?? '',53'env_variable' => $data['env_variable'] ?? '',54'default_value' => $data['default_value'] ?? '',55'user_viewable' => in_array('user_viewable', $options),56'user_editable' => in_array('user_editable', $options),57'rules' => $data['rules'] ?? '',58]);59}60}616263