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

73 lines
2.5 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Services\Eggs\Variables;
use Pterodactyl\Models\EggVariable;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
use Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException;
class VariableUpdateService
{
/**
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface
*/
2017-08-13 19:55:09 +00:00
protected $repository;
2017-08-13 19:55:09 +00:00
/**
* VariableUpdateService constructor.
*
* @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $repository
2017-08-13 19:55:09 +00:00
*/
public function __construct(EggVariableRepositoryInterface $repository)
{
2017-08-13 19:55:09 +00:00
$this->repository = $repository;
}
/**
* Update a specific egg variable.
*
* @param int|\Pterodactyl\Models\EggVariable $variable
* @param array $data
2017-08-13 19:55:09 +00:00
* @return mixed
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
2017-08-13 19:55:09 +00:00
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException
*/
public function handle($variable, array $data)
{
if (! $variable instanceof EggVariable) {
2017-08-13 19:55:09 +00:00
$variable = $this->repository->find($variable);
}
if (! is_null(array_get($data, 'env_variable'))) {
if (in_array(strtoupper(array_get($data, 'env_variable')), explode(',', EggVariable::RESERVED_ENV_NAMES))) {
throw new ReservedVariableNameException(trans('exceptions.service.variables.reserved_name', [
'name' => array_get($data, 'env_variable'),
]));
}
$search = $this->repository->setColumns('id')->findCountWhere([
['env_variable', '=', array_get($data, 'env_variable')],
['egg_id', '=', $variable->egg_id],
['id', '!=', $variable->id],
]);
if ($search > 0) {
throw new DisplayException(trans('exceptions.service.variables.env_not_unique', [
'name' => array_get($data, 'env_variable'),
]));
}
}
2017-12-31 01:56:42 +00:00
$options = array_get($data, 'options') ?? [];
2018-01-05 22:33:50 +00:00
return $this->repository->withoutFreshModel()->update($variable->id, array_merge($data, [
2017-08-13 19:55:09 +00:00
'user_viewable' => in_array('user_viewable', $options),
'user_editable' => in_array('user_editable', $options),
]));
}
}