2020-12-04 08:41:55 +00:00
|
|
|
<?php
|
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-12-04 08:41:55 +00:00
|
|
|
namespace App\Entity\Repository;
|
|
|
|
|
2020-12-22 07:10:03 +00:00
|
|
|
use App\Doctrine\ReloadableEntityManagerInterface;
|
2020-12-04 08:41:55 +00:00
|
|
|
use App\Doctrine\Repository;
|
|
|
|
use App\Entity;
|
|
|
|
use App\Exception\ValidationException;
|
2022-05-31 07:50:49 +00:00
|
|
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
2020-12-04 08:41:55 +00:00
|
|
|
use Symfony\Component\Serializer\Serializer;
|
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
|
|
|
2021-09-06 09:06:31 +00:00
|
|
|
/**
|
|
|
|
* @extends Repository<Entity\Settings>
|
|
|
|
*/
|
2022-05-31 07:50:49 +00:00
|
|
|
final class SettingsRepository extends Repository
|
2020-12-04 08:41:55 +00:00
|
|
|
{
|
2021-04-21 21:15:52 +00:00
|
|
|
protected string $entityClass = Entity\Settings::class;
|
2020-12-10 22:46:03 +00:00
|
|
|
|
2020-12-04 08:41:55 +00:00
|
|
|
public function __construct(
|
2020-12-22 07:10:03 +00:00
|
|
|
ReloadableEntityManagerInterface $em,
|
2022-05-31 07:50:49 +00:00
|
|
|
private readonly Serializer $serializer,
|
|
|
|
private readonly ValidatorInterface $validator
|
2020-12-04 08:41:55 +00:00
|
|
|
) {
|
2022-05-31 07:50:49 +00:00
|
|
|
parent::__construct($em);
|
2020-12-04 08:41:55 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 21:15:52 +00:00
|
|
|
public function readSettings(): Entity\Settings
|
2020-12-04 12:33:37 +00:00
|
|
|
{
|
2021-05-11 14:31:56 +00:00
|
|
|
static $settingsId = null;
|
|
|
|
|
|
|
|
if (null !== $settingsId) {
|
|
|
|
$settings = $this->repository->find($settingsId);
|
|
|
|
if ($settings instanceof Entity\Settings) {
|
|
|
|
return $settings;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 21:15:52 +00:00
|
|
|
$settings = $this->repository->findOneBy([]);
|
2021-04-18 08:26:06 +00:00
|
|
|
|
2021-04-21 21:15:52 +00:00
|
|
|
if (!($settings instanceof Entity\Settings)) {
|
|
|
|
$settings = new Entity\Settings();
|
|
|
|
$this->em->persist($settings);
|
|
|
|
$this->em->flush();
|
2021-04-18 08:26:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 14:31:56 +00:00
|
|
|
$settingsId = $settings->getAppUniqueIdentifier();
|
|
|
|
|
2021-04-21 21:15:52 +00:00
|
|
|
return $settings;
|
2020-12-04 08:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Entity\Settings|array $settingsObj
|
|
|
|
*/
|
2021-04-23 22:12:47 +00:00
|
|
|
public function writeSettings(Entity\Settings|array $settingsObj): void
|
2020-12-04 08:41:55 +00:00
|
|
|
{
|
|
|
|
if (is_array($settingsObj)) {
|
2021-04-21 21:15:52 +00:00
|
|
|
$settings = $this->readSettings();
|
2021-04-25 10:51:12 +00:00
|
|
|
$settings = $this->fromArray($settings, $settingsObj);
|
2021-04-21 21:15:52 +00:00
|
|
|
} else {
|
|
|
|
$settings = $settingsObj;
|
2020-12-04 08:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$errors = $this->validator->validate($settingsObj);
|
|
|
|
if (count($errors) > 0) {
|
2021-11-03 04:12:02 +00:00
|
|
|
throw ValidationException::fromValidationErrors($errors);
|
2020-12-04 08:41:55 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 21:15:52 +00:00
|
|
|
$this->em->persist($settings);
|
2020-12-04 08:41:55 +00:00
|
|
|
$this->em->flush();
|
|
|
|
}
|
2022-05-31 07:50:49 +00:00
|
|
|
|
|
|
|
public function fromArray(Entity\Settings $entity, array $source): Entity\Settings
|
|
|
|
{
|
|
|
|
return $this->serializer->denormalize(
|
|
|
|
$source,
|
|
|
|
Entity\Settings::class,
|
|
|
|
null,
|
|
|
|
[
|
|
|
|
AbstractNormalizer::OBJECT_TO_POPULATE => $entity,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function toArray(Entity\Settings $entity): array
|
|
|
|
{
|
|
|
|
return (array)$this->serializer->normalize(
|
|
|
|
$entity
|
|
|
|
);
|
|
|
|
}
|
2020-12-04 08:41:55 +00:00
|
|
|
}
|