2017-08-12 20:29:01 +00:00
|
|
|
<?php
|
|
|
|
|
2017-10-07 21:16:51 +00:00
|
|
|
namespace Pterodactyl\Services\Eggs\Variables;
|
2017-08-12 20:29:01 +00:00
|
|
|
|
2017-10-07 04:57:53 +00:00
|
|
|
use Pterodactyl\Models\EggVariable;
|
2018-03-17 20:09:09 +00:00
|
|
|
use Illuminate\Contracts\Validation\Factory;
|
2017-08-12 20:29:01 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
2018-03-17 20:09:09 +00:00
|
|
|
use Pterodactyl\Traits\Services\ValidatesValidationRules;
|
2017-10-07 21:16:51 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
|
|
|
|
use Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException;
|
2017-08-12 20:29:01 +00:00
|
|
|
|
|
|
|
class VariableUpdateService
|
|
|
|
{
|
2018-03-17 20:09:09 +00:00
|
|
|
use ValidatesValidationRules;
|
|
|
|
|
2017-08-12 20:29:01 +00:00
|
|
|
/**
|
2017-10-07 21:16:51 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface
|
2017-08-12 20:29:01 +00:00
|
|
|
*/
|
2018-03-17 20:09:09 +00:00
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Validation\Factory
|
|
|
|
*/
|
|
|
|
private $validator;
|
2017-08-12 20:29:01 +00:00
|
|
|
|
2017-08-13 19:55:09 +00:00
|
|
|
/**
|
|
|
|
* VariableUpdateService constructor.
|
|
|
|
*
|
2017-10-07 21:16:51 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $repository
|
2018-03-17 20:09:09 +00:00
|
|
|
* @param \Illuminate\Contracts\Validation\Factory $validator
|
2017-08-13 19:55:09 +00:00
|
|
|
*/
|
2018-03-17 20:09:09 +00:00
|
|
|
public function __construct(EggVariableRepositoryInterface $repository, Factory $validator)
|
2017-08-12 20:29:01 +00:00
|
|
|
{
|
2017-08-13 19:55:09 +00:00
|
|
|
$this->repository = $repository;
|
2018-03-17 20:09:09 +00:00
|
|
|
$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;
|
2017-08-12 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-07 21:16:51 +00:00
|
|
|
* Update a specific egg variable.
|
2017-08-12 20:29:01 +00:00
|
|
|
*
|
2018-02-11 22:47:50 +00:00
|
|
|
* @param \Pterodactyl\Models\EggVariable $variable
|
|
|
|
* @param array $data
|
2017-08-13 19:55:09 +00:00
|
|
|
* @return mixed
|
2017-08-12 20:29:01 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
2017-08-13 19:55:09 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2017-10-07 21:16:51 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException
|
2017-08-12 20:29:01 +00:00
|
|
|
*/
|
2018-02-11 22:47:50 +00:00
|
|
|
public function handle(EggVariable $variable, array $data)
|
2017-08-12 20:29:01 +00:00
|
|
|
{
|
|
|
|
if (! is_null(array_get($data, 'env_variable'))) {
|
2017-10-07 04:57:53 +00:00
|
|
|
if (in_array(strtoupper(array_get($data, 'env_variable')), explode(',', EggVariable::RESERVED_ENV_NAMES))) {
|
2017-09-03 21:32:52 +00:00
|
|
|
throw new ReservedVariableNameException(trans('exceptions.service.variables.reserved_name', [
|
2017-08-12 20:29:01 +00:00
|
|
|
'name' => array_get($data, 'env_variable'),
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
$search = $this->repository->setColumns('id')->findCountWhere([
|
2018-02-17 19:37:53 +00:00
|
|
|
['env_variable', '=', $data['env_variable']],
|
2017-10-07 21:16:51 +00:00
|
|
|
['egg_id', '=', $variable->egg_id],
|
2017-08-12 20:29:01 +00:00
|
|
|
['id', '!=', $variable->id],
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($search > 0) {
|
2017-09-03 21:32:52 +00:00
|
|
|
throw new DisplayException(trans('exceptions.service.variables.env_not_unique', [
|
2017-08-12 20:29:01 +00:00
|
|
|
'name' => array_get($data, 'env_variable'),
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 20:09:09 +00:00
|
|
|
if (! empty($data['rules'] ?? '')) {
|
|
|
|
$this->validateRules($data['rules']);
|
|
|
|
}
|
|
|
|
|
2017-12-31 01:56:42 +00:00
|
|
|
$options = array_get($data, 'options') ?? [];
|
2017-08-12 20:29:01 +00:00
|
|
|
|
2018-02-17 19:37:53 +00:00
|
|
|
return $this->repository->withoutFreshModel()->update($variable->id, [
|
|
|
|
'name' => $data['name'] ?? '',
|
|
|
|
'description' => $data['description'] ?? '',
|
|
|
|
'env_variable' => $data['env_variable'] ?? '',
|
|
|
|
'default_value' => $data['default_value'] ?? '',
|
2017-08-13 19:55:09 +00:00
|
|
|
'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'] ?? '',
|
|
|
|
]);
|
2017-08-12 20:29:01 +00:00
|
|
|
}
|
|
|
|
}
|