PteroTheme/app/Services/Services/Variables/VariableCreationService.php

70 lines
2.3 KiB
PHP
Raw Normal View History

<?php
2017-09-26 03:43:01 +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
*/
namespace Pterodactyl\Services\Services\Variables;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\EggVariable;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface;
use Pterodactyl\Exceptions\Service\ServiceVariable\ReservedVariableNameException;
class VariableCreationService
{
/**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
*/
protected $serviceOptionRepository;
/**
* @var \Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface
*/
protected $serviceVariableRepository;
public function __construct(
EggRepositoryInterface $serviceOptionRepository,
ServiceVariableRepositoryInterface $serviceVariableRepository
) {
$this->serviceOptionRepository = $serviceOptionRepository;
$this->serviceVariableRepository = $serviceVariableRepository;
}
/**
* Create a new variable for a given service option.
*
* @param int|\Pterodactyl\Models\Egg $option
* @param array $data
* @return \Pterodactyl\Models\EggVariable
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Service\ServiceVariable\ReservedVariableNameException
*/
public function handle($option, array $data)
{
if ($option instanceof Egg) {
$option = $option->id;
}
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
));
}
$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));
}
}