feat : add Gitea settings API resource with provider/processor
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
42
src/ApiResource/GiteaSettings.php
Normal file
42
src/ApiResource/GiteaSettings.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\ApiResource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use ApiPlatform\Metadata\Put;
|
||||
use App\State\GiteaSettingsProcessor;
|
||||
use App\State\GiteaSettingsProvider;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new Get(
|
||||
uriTemplate: '/settings/gitea',
|
||||
normalizationContext: ['groups' => ['gitea_settings:read']],
|
||||
provider: GiteaSettingsProvider::class,
|
||||
security: "is_granted('ROLE_ADMIN')",
|
||||
),
|
||||
new Put(
|
||||
uriTemplate: '/settings/gitea',
|
||||
denormalizationContext: ['groups' => ['gitea_settings:write']],
|
||||
normalizationContext: ['groups' => ['gitea_settings:read']],
|
||||
provider: GiteaSettingsProvider::class,
|
||||
processor: GiteaSettingsProcessor::class,
|
||||
security: "is_granted('ROLE_ADMIN')",
|
||||
),
|
||||
],
|
||||
)]
|
||||
final class GiteaSettings
|
||||
{
|
||||
#[Groups(['gitea_settings:read', 'gitea_settings:write'])]
|
||||
public ?string $url = null;
|
||||
|
||||
#[Groups(['gitea_settings:write'])]
|
||||
public ?string $token = null;
|
||||
|
||||
#[Groups(['gitea_settings:read'])]
|
||||
public bool $hasToken = false;
|
||||
}
|
||||
47
src/State/GiteaSettingsProcessor.php
Normal file
47
src/State/GiteaSettingsProcessor.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
30
src/State/GiteaSettingsProvider.php
Normal file
30
src/State/GiteaSettingsProvider.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\GiteaSettings;
|
||||
use App\Repository\GiteaConfigurationRepository;
|
||||
|
||||
final readonly class GiteaSettingsProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private GiteaConfigurationRepository $configRepository,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): GiteaSettings
|
||||
{
|
||||
$config = $this->configRepository->findSingleton();
|
||||
$dto = new GiteaSettings();
|
||||
|
||||
if (null !== $config) {
|
||||
$dto->url = $config->getUrl();
|
||||
$dto->hasToken = $config->hasToken();
|
||||
}
|
||||
|
||||
return $dto;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user