More repository/service/refactor changes

This commit is contained in:
Dane Everitt 2017-08-12 15:29:01 -05:00
parent 2c77d5c44d
commit b8d7d99096
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
44 changed files with 977 additions and 669 deletions

View file

@ -0,0 +1,77 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Services\Services\Options;
use Pterodactyl\Models\ServiceOption;
use Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface;
use Pterodactyl\Exceptions\Services\ServiceOption\InvalidCopyFromException;
class InstallScriptUpdateService
{
/**
* @var \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface
*/
protected $serviceOptionRepository;
/**
* InstallScriptUpdateService constructor.
*
* @param \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface $serviceOptionRepository
*/
public function __construct(ServiceOptionRepositoryInterface $serviceOptionRepository)
{
$this->serviceOptionRepository = $serviceOptionRepository;
}
/**
* Modify the option install script for a given service option.
*
* @param int|\Pterodactyl\Models\ServiceOption $option
* @param array $data
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Services\ServiceOption\InvalidCopyFromException
*/
public function handle($option, array $data)
{
if (! $option instanceof ServiceOption) {
$option = $this->serviceOptionRepository->find($option);
}
if (! is_null(array_get($data, 'copy_script_from'))) {
if (! $this->serviceOptionRepository->isCopiableScript(array_get($data, 'copy_script_from'), $option->service_id)) {
throw new InvalidCopyFromException(trans('admin/exceptions.service.options.invalid_copy_id'));
}
}
$this->serviceOptionRepository->withoutFresh()->update($option->id, [
'script_install' => array_get($data, 'script_install'),
'script_is_privileged' => array_get($data, 'script_is_privileged'),
'script_entry' => array_get($data, 'script_entry'),
'script_container' => array_get($data, 'script_container'),
'copy_script_from' => array_get($data, 'copy_script_from'),
]);
}
}

View file

@ -24,10 +24,10 @@
namespace Pterodactyl\Services\Services\Options;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface;
use Pterodactyl\Exceptions\Services\ServiceOption\NoParentConfigurationFoundException;
class CreationService
class OptionCreationService
{
/**
* @var \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface
@ -50,8 +50,8 @@ class CreationService
* @param array $data
* @return \Pterodactyl\Models\ServiceOption
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Services\ServiceOption\NoParentConfigurationFoundException
*/
public function handle(array $data)
{
@ -62,7 +62,7 @@ class CreationService
]);
if ($results !== 1) {
throw new DisplayException(trans('admin/exceptions.service.options.must_be_child'));
throw new NoParentConfigurationFoundException(trans('admin/exceptions.service.options.must_be_child'));
}
} else {
$data['config_from'] = null;

View file

@ -0,0 +1,77 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Services\Services\Options;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface;
use Pterodactyl\Exceptions\Services\ServiceOption\HasActiveServersException;
class OptionDeletionService
{
/**
* @var \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface
*/
protected $repository;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/
protected $serverRepository;
/**
* OptionDeletionService constructor.
*
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $serverRepository
* @param \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface $repository
*/
public function __construct(
ServerRepositoryInterface $serverRepository,
ServiceOptionRepositoryInterface $repository
) {
$this->repository = $repository;
$this->serverRepository = $serverRepository;
}
/**
* Delete an option from the database if it has no active servers attached to it.
*
* @param int $option
* @return int
*
* @throws \Pterodactyl\Exceptions\Services\ServiceOption\HasActiveServersException
*/
public function handle($option)
{
$servers = $this->serverRepository->findCountWhere([
['option_id', '=', $option],
]);
if ($servers > 0) {
throw new HasActiveServersException(trans('admin/exceptions.service.options.delete_has_servers'));
}
return $this->repository->delete($option);
}
}

View file

@ -0,0 +1,77 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Services\Services\Options;
use Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface;
use Pterodactyl\Exceptions\Services\ServiceOption\NoParentConfigurationFoundException;
use Pterodactyl\Models\ServiceOption;
class OptionUpdateService
{
/**
* @var \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface
*/
protected $repository;
/**
* OptionUpdateService constructor.
*
* @param \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface $repository
*/
public function __construct(ServiceOptionRepositoryInterface $repository)
{
$this->repository = $repository;
}
/**
* Update a service option.
*
* @param int|\Pterodactyl\Models\ServiceOption $option
* @param array $data
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Services\ServiceOption\NoParentConfigurationFoundException
*/
public function handle($option, array $data)
{
if (! $option instanceof ServiceOption) {
$option = $this->repository->find($option);
}
if (! is_null(array_get($data, 'config_from'))) {
$results = $this->repository->findCountWhere([
['service_id', '=', $option->service_id],
['id', '=', array_get($data, 'config_from')],
]);
if ($results !== 1) {
throw new NoParentConfigurationFoundException(trans('admin/exceptions.service.options.must_be_child'));
}
}
$this->repository->withoutFresh()->update($option->id, $data);
}
}

View file

@ -25,6 +25,10 @@
namespace Pterodactyl\Services\Services\Variables;
use Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface;
use Pterodactyl\Exceptions\Services\ServiceVariable\ReservedVariableNameException;
use Pterodactyl\Models\ServiceOption;
use Pterodactyl\Models\ServiceVariable;
class VariableCreationService
{
@ -33,20 +37,45 @@ class VariableCreationService
*/
protected $serviceOptionRepository;
public function __construct(ServiceOptionRepositoryInterface $serviceOptionRepository)
{
/**
* @var \Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface
*/
protected $serviceVariableRepository;
public function __construct(
ServiceOptionRepositoryInterface $serviceOptionRepository,
ServiceVariableRepositoryInterface $serviceVariableRepository
) {
$this->serviceOptionRepository = $serviceOptionRepository;
$this->serviceVariableRepository = $serviceVariableRepository;
}
/**
* Create a new variable for a given service option.
*
* @param int $optionId
* @param int $option
* @param array $data
* @return \Pterodactyl\Models\ServiceVariable
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Services\ServiceVariable\ReservedVariableNameException
*/
public function handle($optionId, array $data)
public function handle($option, array $data)
{
$option = $this->serviceOptionRepository->find($optionId);
if ($option instanceof ServiceOption) {
$option = $option->id;
}
if (in_array(strtoupper(array_get($data, 'env_variable')), explode(',', ServiceVariable::RESERVED_ENV_NAMES))) {
throw new ReservedVariableNameException(sprintf('Cannot use the protected name %s for this environment variable.'));
}
$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));
}
}

View file

@ -0,0 +1,88 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Services\Services\Variables;
use Pterodactyl\Models\ServiceVariable;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface;
use Pterodactyl\Exceptions\Services\ServiceVariable\ReservedVariableNameException;
class VariableUpdateService
{
/**
* @var \Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface
*/
protected $serviceVariableRepository;
public function __construct(ServiceVariableRepositoryInterface $serviceVariableRepository)
{
$this->serviceVariableRepository = $serviceVariableRepository;
}
/**
* Update a specific service variable.
*
* @param int|\Pterodactyl\Models\ServiceVariable $variable
* @param array $data
* @return \Pterodactyl\Models\ServiceVariable
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Services\ServiceVariable\ReservedVariableNameException
*/
public function handle($variable, array $data)
{
if (! $variable instanceof ServiceVariable) {
$variable = $this->serviceVariableRepository->find($variable);
}
if (! is_null(array_get($data, 'env_variable'))) {
if (in_array(strtoupper(array_get($data, 'env_variable')), explode(',', ServiceVariable::RESERVED_ENV_NAMES))) {
throw new ReservedVariableNameException(trans('admin/exceptions.service.variables.reserved_name', [
'name' => array_get($data, 'env_variable'),
]));
}
$search = $this->serviceVariableRepository->withColumns('id')->findCountWhere([
['env_variable', '=', array_get($data, 'env_variable')],
['option_id', '=', $variable->option_id],
['id', '!=', $variable->id],
]);
if ($search > 0) {
throw new DisplayException(trans('admin/exceptions.service.variables.env_not_unique', [
'name' => array_get($data, 'env_variable'),
]));
}
}
$options = array_get($data, 'options', []);
return $this->serviceVariableRepository->update($variable->id, array_merge([
'user_viewable' => in_array('user_viewable', $options, $variable->user_viewable),
'user_editable' => in_array('user_editable', $options, $variable->user_editable),
], $data));
}
}