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

86 lines
2.7 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Services\Eggs\Variables;
use Pterodactyl\Models\EggVariable;
use Illuminate\Contracts\Validation\Factory;
use Pterodactyl\Traits\Services\ValidatesValidationRules;
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
use Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException;
class VariableCreationService
{
use ValidatesValidationRules;
/**
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface
*/
private $repository;
/**
* @var \Illuminate\Contracts\Validation\Factory
*/
private $validator;
/**
* VariableCreationService constructor.
*
2017-10-08 04:29:08 +00:00
* @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $repository
2019-09-06 04:32:57 +00:00
* @param \Illuminate\Contracts\Validation\Factory $validator
*/
public function __construct(EggVariableRepositoryInterface $repository, Factory $validator)
2017-10-08 04:29:08 +00:00
{
$this->repository = $repository;
$this->validator = $validator;
}
/**
* Return the validation factory instance to be used by rule validation
* checking in the trait.
*
* @return \Illuminate\Contracts\Validation\Factory
*/
protected function getValidator(): Factory
{
return $this->validator;
}
/**
* Create a new variable for a given Egg.
*
2019-09-06 04:32:57 +00:00
* @param int $egg
* @param array $data
* @return \Pterodactyl\Models\EggVariable
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Service\Egg\Variable\BadValidationRuleException
* @throws \Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException
*/
public function handle(int $egg, array $data): EggVariable
{
if (in_array(strtoupper(array_get($data, 'env_variable')), explode(',', EggVariable::RESERVED_ENV_NAMES))) {
2017-08-13 19:55:09 +00:00
throw new ReservedVariableNameException(sprintf(
2017-08-22 03:10:48 +00:00
'Cannot use the protected name %s for this environment variable.',
array_get($data, 'env_variable')
2017-08-13 19:55:09 +00:00
));
}
if (! empty($data['rules'] ?? '')) {
$this->validateRules($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->create([
'egg_id' => $egg,
2018-02-17 19:37:53 +00:00
'name' => $data['name'] ?? '',
'description' => $data['description'] ?? '',
'env_variable' => $data['env_variable'] ?? '',
'default_value' => $data['default_value'] ?? '',
'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'] ?? '',
]);
}
}