48 lines
1.3 KiB
PHP
48 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\State;
|
|
|
|
use ApiPlatform\Metadata\Operation;
|
|
use ApiPlatform\State\ProcessorInterface;
|
|
use App\ApiResource\GiteaSettings;
|
|
use App\Entity\GiteaConfiguration;
|
|
use App\Repository\GiteaConfigurationRepository;
|
|
use App\Service\TokenEncryptor;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
final readonly class GiteaSettingsProcessor implements ProcessorInterface
|
|
{
|
|
public function __construct(
|
|
private EntityManagerInterface $em,
|
|
private GiteaConfigurationRepository $configRepository,
|
|
private TokenEncryptor $tokenEncryptor,
|
|
) {}
|
|
|
|
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): GiteaSettings
|
|
{
|
|
assert($data instanceof GiteaSettings);
|
|
|
|
$config = $this->configRepository->findSingleton();
|
|
if (null === $config) {
|
|
$config = new GiteaConfiguration();
|
|
}
|
|
|
|
$config->setUrl($data->url);
|
|
|
|
if (null !== $data->token && '' !== $data->token) {
|
|
$config->setEncryptedToken($this->tokenEncryptor->encrypt($data->token));
|
|
}
|
|
|
|
$this->em->persist($config);
|
|
$this->em->flush();
|
|
|
|
$result = new GiteaSettings();
|
|
$result->url = $config->getUrl();
|
|
$result->hasToken = $config->hasToken();
|
|
|
|
return $result;
|
|
}
|
|
}
|