Update DropFile to handle object versioning

This commit is contained in:
2024-09-02 16:24:23 +02:00
parent b6edbb3eed
commit 3d49c959e0
42 changed files with 857 additions and 539 deletions

View File

@@ -12,37 +12,75 @@ declare(strict_types=1);
namespace Chill\DocStoreBundle\Serializer\Normalizer;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Repository\StoredObjectRepository;
use Chill\DocStoreBundle\Repository\StoredObjectRepositoryInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectToPopulateTrait;
/**
* Implements the DenormalizerInterface and is responsible for denormalizing data into StoredObject objects.
*
* If a new StoredObjectVersion has been added to the StoredObject, the version is created here and registered
* to the StoredObject.
*/
class StoredObjectDenormalizer implements DenormalizerInterface
{
use ObjectToPopulateTrait;
public function __construct(private readonly StoredObjectRepository $storedObjectRepository) {}
public function __construct(private readonly StoredObjectRepositoryInterface $storedObjectRepository) {}
public function denormalize($data, $type, $format = null, array $context = [])
public function denormalize($data, $type, $format = null, array $context = []): ?StoredObject
{
$object = $this->extractObjectToPopulate(StoredObject::class, $context);
$storedObject = $this->extractObjectToPopulate(StoredObject::class, $context);
if (null !== $object) {
return $object;
if (null === $storedObject) {
if (array_key_exists('uuid', $data)) {
$storedObject = $this->storedObjectRepository->findOneByUUID($data['uuid']);
} else {
$storedObject = $this->storedObjectRepository->find($data['id']);
}
if (null === $storedObject) {
throw new LogicException('Object not found');
}
}
return $this->storedObjectRepository->find($data['id']);
$storedObject->setTitle($data['title'] ?? $storedObject->getTitle());
if (true === ($data['currentVersion']['persisted'] ?? true)) {
// nothing has change, stop here
return $storedObject;
}
if ([] !== $diff = array_diff(['filename', 'iv', 'keyInfos', 'type'], array_keys($data['currentVersion']))) {
throw new TransformationFailedException(sprintf('missing some keys in currentVersion: %s', implode(', ', $diff)));
}
$storedObject->registerVersion(
$data['currentVersion']['iv'],
$data['currentVersion']['keyInfos'],
$data['currentVersion']['type'],
$data['currentVersion']['filename']
);
return $storedObject;
}
public function supportsDenormalization($data, $type, $format = null)
public function supportsDenormalization($data, $type, $format = null): bool
{
if (false === \is_array($data)) {
if (StoredObject::class !== $type) {
return false;
}
if (false === \array_key_exists('id', $data)) {
if (false === is_array($data)) {
return false;
}
return StoredObject::class === $type;
if (array_key_exists('id', $data) || array_key_exists('uuid', $data)) {
return true;
}
return false;
}
}

View File

@@ -89,7 +89,7 @@ final class StoredObjectNormalizer implements NormalizerInterface, NormalizerAwa
],
UrlGeneratorInterface::ABSOLUTE_URL,
),
'expiration' => $this->JWTDavTokenProvider->getTokenExpiration($accessToken)->format('U'),
'expiration' => $this->JWTDavTokenProvider->getTokenExpiration($accessToken)->getTimestamp(),
],
];
}

View File

@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocStoreBundle\Serializer\Normalizer;
use Chill\DocStoreBundle\Entity\StoredObjectVersion;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class StoredObjectVersionNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
public function normalize($object, ?string $format = null, array $context = [])
{
if (!$object instanceof StoredObjectVersion) {
throw new \InvalidArgumentException('The object must be an instance of '.StoredObjectVersion::class);
}
return [
'id' => $object->getId(),
'filename' => $object->getFilename(),
'version' => $object->getVersion(),
'iv' => array_values($object->getIv()),
'keyInfos' => $object->getKeyInfos(),
'type' => $object->getType(),
'createdAt' => $this->normalizer->normalize($object->getCreatedAt(), $format, $context),
'createdBy' => $this->normalizer->normalize($object->getCreatedBy(), $format, $context),
];
}
public function supportsNormalization($data, ?string $format = null, array $context = [])
{
return $data instanceof StoredObjectVersion;
}
}