mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-29 16:44:44 +02:00
45 lines
1.3 KiB
PHP
45 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Eggs;
|
|
|
|
use Ramsey\Uuid\Uuid;
|
|
use App\Models\Egg;
|
|
use App\Contracts\Repository\EggRepositoryInterface;
|
|
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
|
use App\Exceptions\Service\Egg\NoParentConfigurationFoundException;
|
|
|
|
class EggCreationService
|
|
{
|
|
/**
|
|
* EggCreationService constructor.
|
|
*/
|
|
public function __construct(private ConfigRepository $config, private EggRepositoryInterface $repository)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Create a new egg.
|
|
*
|
|
* @throws \App\Exceptions\Model\DataValidationException
|
|
* @throws \App\Exceptions\Service\Egg\NoParentConfigurationFoundException
|
|
*/
|
|
public function handle(array $data): Egg
|
|
{
|
|
$data['config_from'] = array_get($data, 'config_from');
|
|
if (!is_null($data['config_from'])) {
|
|
$results = $this->repository->findCountWhere([
|
|
['id', '=', array_get($data, 'config_from')],
|
|
]);
|
|
|
|
if ($results !== 1) {
|
|
throw new NoParentConfigurationFoundException(trans('exceptions.egg.invalid_copy_id'));
|
|
}
|
|
}
|
|
|
|
return $this->repository->create(array_merge($data, [
|
|
'uuid' => Uuid::uuid4()->toString(),
|
|
'author' => $this->config->get('panel.service.author'),
|
|
]), true, true);
|
|
}
|
|
}
|