2017-08-09 05:24:55 +01:00
|
|
|
<?php
|
2017-09-26 03:43:01 +01:00
|
|
|
/**
|
2017-08-09 05:24:55 +01:00
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 03:43:01 +01:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-08-09 05:24:55 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Services\Variables;
|
|
|
|
|
2017-10-07 05:57:53 +01:00
|
|
|
use Pterodactyl\Models\Egg;
|
|
|
|
use Pterodactyl\Models\EggVariable;
|
|
|
|
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
2017-08-12 21:29:01 +01:00
|
|
|
use Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface;
|
2017-08-19 04:19:06 +01:00
|
|
|
use Pterodactyl\Exceptions\Service\ServiceVariable\ReservedVariableNameException;
|
2017-08-09 05:24:55 +01:00
|
|
|
|
|
|
|
class VariableCreationService
|
|
|
|
{
|
|
|
|
/**
|
2017-10-07 05:57:53 +01:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
|
2017-08-09 05:24:55 +01:00
|
|
|
*/
|
|
|
|
protected $serviceOptionRepository;
|
|
|
|
|
2017-08-12 21:29:01 +01:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $serviceVariableRepository;
|
|
|
|
|
|
|
|
public function __construct(
|
2017-10-07 05:57:53 +01:00
|
|
|
EggRepositoryInterface $serviceOptionRepository,
|
2017-08-12 21:29:01 +01:00
|
|
|
ServiceVariableRepositoryInterface $serviceVariableRepository
|
|
|
|
) {
|
2017-08-09 05:24:55 +01:00
|
|
|
$this->serviceOptionRepository = $serviceOptionRepository;
|
2017-08-12 21:29:01 +01:00
|
|
|
$this->serviceVariableRepository = $serviceVariableRepository;
|
2017-08-09 05:24:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new variable for a given service option.
|
|
|
|
*
|
2017-10-07 05:57:53 +01:00
|
|
|
* @param int|\Pterodactyl\Models\Egg $option
|
|
|
|
* @param array $data
|
|
|
|
* @return \Pterodactyl\Models\EggVariable
|
2017-08-12 21:29:01 +01:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
2017-08-19 04:19:06 +01:00
|
|
|
* @throws \Pterodactyl\Exceptions\Service\ServiceVariable\ReservedVariableNameException
|
2017-08-09 05:24:55 +01:00
|
|
|
*/
|
2017-08-12 21:29:01 +01:00
|
|
|
public function handle($option, array $data)
|
2017-08-09 05:24:55 +01:00
|
|
|
{
|
2017-10-07 05:57:53 +01:00
|
|
|
if ($option instanceof Egg) {
|
2017-08-12 21:29:01 +01:00
|
|
|
$option = $option->id;
|
|
|
|
}
|
|
|
|
|
2017-10-07 05:57:53 +01:00
|
|
|
if (in_array(strtoupper(array_get($data, 'env_variable')), explode(',', EggVariable::RESERVED_ENV_NAMES))) {
|
2017-08-13 20:55:09 +01:00
|
|
|
throw new ReservedVariableNameException(sprintf(
|
2017-08-22 04:10:48 +01:00
|
|
|
'Cannot use the protected name %s for this environment variable.',
|
|
|
|
array_get($data, 'env_variable')
|
2017-08-13 20:55:09 +01:00
|
|
|
));
|
2017-08-12 21:29:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$options = array_get($data, 'options', []);
|
|
|
|
|
|
|
|
return $this->serviceVariableRepository->create(array_merge([
|
|
|
|
'option_id' => $option,
|
|
|
|
'user_viewable' => in_array('user_viewable', $options),
|
|
|
|
'user_editable' => in_array('user_editable', $options),
|
|
|
|
], $data));
|
2017-08-09 05:24:55 +01:00
|
|
|
}
|
|
|
|
}
|