2023-04-02 00:55:20 +02:00
|
|
|
<?php
|
2023-06-11 18:59:07 +02:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2023-04-02 00:55:20 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
namespace App\Serializer;
|
|
|
|
|
|
|
|
use App\Entity\Base\AbstractStructuralDBElement;
|
|
|
|
use App\Repository\StructuralDBElementRepository;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2023-06-11 18:12:22 +02:00
|
|
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
2023-04-02 00:55:20 +02:00
|
|
|
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
|
2023-06-11 18:12:22 +02:00
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
|
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
|
2023-04-02 00:55:20 +02:00
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
|
|
|
|
2023-06-11 15:02:59 +02:00
|
|
|
/**
|
|
|
|
* @see \App\Tests\Serializer\StructuralElementDenormalizerTest
|
|
|
|
*/
|
2023-06-11 18:12:22 +02:00
|
|
|
class StructuralElementDenormalizer implements DenormalizerInterface
|
2023-04-02 00:55:20 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
private array $object_cache = [];
|
|
|
|
|
2023-06-11 18:12:22 +02:00
|
|
|
public function __construct(
|
|
|
|
private readonly EntityManagerInterface $entityManager,
|
|
|
|
#[Autowire(service: ObjectNormalizer::class)]
|
|
|
|
private readonly DenormalizerInterface $denormalizer)
|
2023-04-02 00:55:20 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-04-15 19:33:39 +02:00
|
|
|
public function supportsDenormalization($data, string $type, string $format = null, array $context = []): bool
|
2023-04-02 00:55:20 +02:00
|
|
|
{
|
|
|
|
return is_array($data)
|
|
|
|
&& is_subclass_of($type, AbstractStructuralDBElement::class)
|
2023-04-15 23:14:53 +02:00
|
|
|
//Only denormalize if we are doing a file import operation
|
2023-04-15 22:05:29 +02:00
|
|
|
&& in_array('import', $context['groups'] ?? [], true);
|
2023-04-02 00:55:20 +02:00
|
|
|
}
|
|
|
|
|
2023-04-15 21:18:11 +02:00
|
|
|
public function denormalize($data, string $type, string $format = null, array $context = []): ?AbstractStructuralDBElement
|
2023-04-02 00:55:20 +02:00
|
|
|
{
|
|
|
|
/** @var AbstractStructuralDBElement $deserialized_entity */
|
2023-06-11 18:12:22 +02:00
|
|
|
$deserialized_entity = $this->denormalizer->denormalize($data, $type, $format, $context);
|
2023-04-02 00:55:20 +02:00
|
|
|
|
|
|
|
//Check if we already have the entity in the database (via path)
|
|
|
|
/** @var StructuralDBElementRepository $repo */
|
|
|
|
$repo = $this->entityManager->getRepository($type);
|
|
|
|
|
|
|
|
$path = $deserialized_entity->getFullPath(AbstractStructuralDBElement::PATH_DELIMITER_ARROW);
|
|
|
|
$db_elements = $repo->getEntityByPath($path, AbstractStructuralDBElement::PATH_DELIMITER_ARROW);
|
2023-06-11 14:15:46 +02:00
|
|
|
if ($db_elements !== []) {
|
2023-04-02 00:55:20 +02:00
|
|
|
//We already have the entity in the database, so we can return it
|
|
|
|
return end($db_elements);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Check if we have created the entity in this request before (so we don't create multiple entities for the same path)
|
|
|
|
//Entities get saved in the cache by type and path
|
2023-04-15 23:14:53 +02:00
|
|
|
//We use a different cache for this then the objects created by a string value (saved in repo). However, that should not be a problem
|
2023-04-02 00:55:20 +02:00
|
|
|
//unless the user data has mixed structure between json data and a string path
|
|
|
|
if (isset($this->object_cache[$type][$path])) {
|
|
|
|
return $this->object_cache[$type][$path];
|
|
|
|
}
|
|
|
|
|
|
|
|
//Save the entity in the cache
|
|
|
|
$this->object_cache[$type][$path] = $deserialized_entity;
|
|
|
|
|
|
|
|
//We don't have the entity in the database, so we have to persist it
|
|
|
|
$this->entityManager->persist($deserialized_entity);
|
|
|
|
|
|
|
|
return $deserialized_entity;
|
|
|
|
}
|
|
|
|
|
2023-06-11 18:12:22 +02:00
|
|
|
public function getSupportedTypes(): array
|
2023-04-02 00:55:20 +02:00
|
|
|
{
|
2023-06-11 18:12:22 +02:00
|
|
|
//Must be false, because we use in_array in supportsDenormalization
|
|
|
|
return [
|
|
|
|
AbstractStructuralDBElement::class => false,
|
|
|
|
];
|
2023-04-02 00:55:20 +02:00
|
|
|
}
|
2023-06-11 18:59:07 +02:00
|
|
|
}
|