Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Services/Eggs/Variables/VariableCreationService.php
10294 views
1
<?php
2
3
namespace Pterodactyl\Services\Eggs\Variables;
4
5
use Pterodactyl\Models\EggVariable;
6
use Pterodactyl\Traits\Services\ValidatesValidationRules;
7
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
8
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
9
use Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException;
10
11
class VariableCreationService
12
{
13
use ValidatesValidationRules;
14
15
/**
16
* VariableCreationService constructor.
17
*/
18
public function __construct(private EggVariableRepositoryInterface $repository, private ValidationFactory $validator)
19
{
20
}
21
22
/**
23
* Return the validation factory instance to be used by rule validation
24
* checking in the trait.
25
*/
26
protected function getValidator(): ValidationFactory
27
{
28
return $this->validator;
29
}
30
31
/**
32
* Create a new variable for a given Egg.
33
*
34
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
35
* @throws \Pterodactyl\Exceptions\Service\Egg\Variable\BadValidationRuleException
36
* @throws ReservedVariableNameException
37
*/
38
public function handle(int $egg, array $data): EggVariable
39
{
40
if (in_array(strtoupper(array_get($data, 'env_variable')), explode(',', EggVariable::RESERVED_ENV_NAMES))) {
41
throw new ReservedVariableNameException(sprintf('Cannot use the protected name %s for this environment variable.', array_get($data, 'env_variable')));
42
}
43
44
if (!empty($data['rules'] ?? '')) {
45
$this->validateRules($data['rules']);
46
}
47
48
$options = array_get($data, 'options') ?? [];
49
50
return $this->repository->create([
51
'egg_id' => $egg,
52
'name' => $data['name'] ?? '',
53
'description' => $data['description'] ?? '',
54
'env_variable' => $data['env_variable'] ?? '',
55
'default_value' => $data['default_value'] ?? '',
56
'user_viewable' => in_array('user_viewable', $options),
57
'user_editable' => in_array('user_editable', $options),
58
'rules' => $data['rules'] ?? '',
59
]);
60
}
61
}
62
63