2017-08-09 04:24:55 +00:00
|
|
|
<?php
|
2017-09-26 02:43:01 +00:00
|
|
|
/**
|
2017-08-09 04:24:55 +00:00
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-08-09 04:24:55 +00:00
|
|
|
*/
|
|
|
|
|
2017-10-07 21:16:51 +00:00
|
|
|
namespace Pterodactyl\Services\Eggs\Variables;
|
2017-08-09 04:24:55 +00:00
|
|
|
|
2017-10-07 04:57:53 +00:00
|
|
|
use Pterodactyl\Models\EggVariable;
|
|
|
|
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
2017-10-07 21:16:51 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
|
|
|
|
use Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException;
|
2017-08-09 04:24:55 +00:00
|
|
|
|
|
|
|
class VariableCreationService
|
|
|
|
{
|
|
|
|
/**
|
2017-10-07 04:57:53 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
|
2017-08-09 04:24:55 +00:00
|
|
|
*/
|
2017-10-07 21:16:51 +00:00
|
|
|
protected $eggRepository;
|
2017-08-09 04:24:55 +00:00
|
|
|
|
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
|
|
|
*/
|
2017-10-07 21:16:51 +00:00
|
|
|
protected $variableRepository;
|
2017-08-12 20:29:01 +00:00
|
|
|
|
2017-10-07 21:16:51 +00:00
|
|
|
/**
|
|
|
|
* VariableCreationService constructor.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $eggRepository
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $variableRepository
|
|
|
|
*/
|
2017-08-12 20:29:01 +00:00
|
|
|
public function __construct(
|
2017-10-07 21:16:51 +00:00
|
|
|
EggRepositoryInterface $eggRepository,
|
|
|
|
EggVariableRepositoryInterface $variableRepository
|
2017-08-12 20:29:01 +00:00
|
|
|
) {
|
2017-10-07 21:16:51 +00:00
|
|
|
$this->eggRepository = $eggRepository;
|
|
|
|
$this->variableRepository = $variableRepository;
|
2017-08-09 04:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-07 21:16:51 +00:00
|
|
|
* Create a new variable for a given Egg.
|
2017-08-09 04:24:55 +00:00
|
|
|
*
|
2017-10-07 21:16:51 +00:00
|
|
|
* @param int $egg
|
|
|
|
* @param array $data
|
2017-10-07 04:57:53 +00:00
|
|
|
* @return \Pterodactyl\Models\EggVariable
|
2017-08-12 20:29:01 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
2017-10-07 21:16:51 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException
|
2017-08-09 04:24:55 +00:00
|
|
|
*/
|
2017-10-07 21:16:51 +00:00
|
|
|
public function handle(int $egg, array $data): EggVariable
|
2017-08-09 04:24:55 +00:00
|
|
|
{
|
2017-10-07 04:57:53 +00:00
|
|
|
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
|
|
|
));
|
2017-08-12 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$options = array_get($data, 'options', []);
|
|
|
|
|
2017-10-07 21:16:51 +00:00
|
|
|
return $this->variableRepository->create(array_merge([
|
|
|
|
'egg_id' => $egg,
|
2017-08-12 20:29:01 +00:00
|
|
|
'user_viewable' => in_array('user_viewable', $options),
|
|
|
|
'user_editable' => in_array('user_editable', $options),
|
|
|
|
], $data));
|
2017-08-09 04:24:55 +00:00
|
|
|
}
|
|
|
|
}
|