Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Services/Eggs/Variables/VariableUpdateService.php
10287 views
1
<?php
2
3
namespace Pterodactyl\Services\Eggs\Variables;
4
5
use Illuminate\Support\Str;
6
use Pterodactyl\Models\EggVariable;
7
use Pterodactyl\Exceptions\DisplayException;
8
use Pterodactyl\Traits\Services\ValidatesValidationRules;
9
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
10
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
11
use Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException;
12
13
class VariableUpdateService
14
{
15
use ValidatesValidationRules;
16
17
/**
18
* VariableUpdateService constructor.
19
*/
20
public function __construct(private EggVariableRepositoryInterface $repository, private ValidationFactory $validator)
21
{
22
}
23
24
/**
25
* Return the validation factory instance to be used by rule validation
26
* checking in the trait.
27
*/
28
protected function getValidator(): ValidationFactory
29
{
30
return $this->validator;
31
}
32
33
/**
34
* Update a specific egg variable.
35
*
36
* @throws DisplayException
37
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
38
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
39
* @throws ReservedVariableNameException
40
*/
41
public function handle(EggVariable $variable, array $data): mixed
42
{
43
if (!is_null(array_get($data, 'env_variable'))) {
44
if (in_array(strtoupper(array_get($data, 'env_variable')), explode(',', EggVariable::RESERVED_ENV_NAMES))) {
45
throw new ReservedVariableNameException(trans('exceptions.service.variables.reserved_name', ['name' => array_get($data, 'env_variable')]));
46
}
47
48
$search = $this->repository->setColumns('id')->findCountWhere([
49
['env_variable', '=', $data['env_variable']],
50
['egg_id', '=', $variable->egg_id],
51
['id', '!=', $variable->id],
52
]);
53
54
if ($search > 0) {
55
throw new DisplayException(trans('exceptions.service.variables.env_not_unique', ['name' => array_get($data, 'env_variable')]));
56
}
57
}
58
59
if (!empty($data['rules'] ?? '')) {
60
$this->validateRules(
61
(is_string($data['rules']) && Str::contains($data['rules'], ';;'))
62
? explode(';;', $data['rules'])
63
: $data['rules']
64
);
65
}
66
67
$options = array_get($data, 'options') ?? [];
68
69
return $this->repository->withoutFreshModel()->update($variable->id, [
70
'name' => $data['name'] ?? '',
71
'description' => $data['description'] ?? '',
72
'env_variable' => $data['env_variable'] ?? '',
73
'default_value' => $data['default_value'] ?? '',
74
'user_viewable' => in_array('user_viewable', $options),
75
'user_editable' => in_array('user_editable', $options),
76
'rules' => $data['rules'] ?? '',
77
]);
78
}
79
}
80
81