From 5f67a7aadc20c5c1fcb7698508b11337de5450e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 19 Sep 2024 17:47:55 +0200 Subject: [PATCH] Create a service to duplicate a storedObject into another one --- .../Service/StoredObjectDuplicate.php | 47 ++++++++++++++++++ .../Service/StoredObjectRestore.php | 3 ++ .../Service/StoredObjectDuplicateTest.php | 48 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 src/Bundle/ChillDocStoreBundle/Service/StoredObjectDuplicate.php create mode 100644 src/Bundle/ChillDocStoreBundle/Tests/Service/StoredObjectDuplicateTest.php diff --git a/src/Bundle/ChillDocStoreBundle/Service/StoredObjectDuplicate.php b/src/Bundle/ChillDocStoreBundle/Service/StoredObjectDuplicate.php new file mode 100644 index 000000000..b1c7e7a87 --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Service/StoredObjectDuplicate.php @@ -0,0 +1,47 @@ +getCurrentVersion(); + + $oldContent = $this->storedObjectManager->read($fromVersion); + + $storedObject = new StoredObject(); + + $newVersion = $this->storedObjectManager->write($storedObject, $oldContent, $fromVersion->getType()); + + $newVersion->setCreatedFrom($fromVersion); + + $this->logger->info('[StoredObjectDuplicate] Duplicated stored object from a version of a previous stored object', [ + 'from_stored_object_uuid' => $fromVersion->getStoredObject()->getUuid(), + 'to_stored_object_uuid' => $storedObject->getUuid(), + 'old_version_id' => $fromVersion->getId(), + 'old_version_version' => $fromVersion->getVersion(), + 'new_version_id' => $newVersion->getVersion(), + ]); + + return $storedObject; + } +} diff --git a/src/Bundle/ChillDocStoreBundle/Service/StoredObjectRestore.php b/src/Bundle/ChillDocStoreBundle/Service/StoredObjectRestore.php index 3c6469185..bad032346 100644 --- a/src/Bundle/ChillDocStoreBundle/Service/StoredObjectRestore.php +++ b/src/Bundle/ChillDocStoreBundle/Service/StoredObjectRestore.php @@ -14,6 +14,9 @@ namespace Chill\DocStoreBundle\Service; use Chill\DocStoreBundle\Entity\StoredObjectVersion; use Psr\Log\LoggerInterface; +/** + * Class responsible for restoring stored object versions into the same stored object. + */ final readonly class StoredObjectRestore implements StoredObjectRestoreInterface { public function __construct(private readonly StoredObjectManagerInterface $storedObjectManager, private readonly LoggerInterface $logger) {} diff --git a/src/Bundle/ChillDocStoreBundle/Tests/Service/StoredObjectDuplicateTest.php b/src/Bundle/ChillDocStoreBundle/Tests/Service/StoredObjectDuplicateTest.php new file mode 100644 index 000000000..2e9da0bab --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Tests/Service/StoredObjectDuplicateTest.php @@ -0,0 +1,48 @@ +registerVersion(type: $type = 'application/test'); + + $manager = $this->createMock(StoredObjectManagerInterface::class); + $manager->method('read')->with($version)->willReturn('1234'); + $manager + ->expects($this->once()) + ->method('write') + ->with($this->isInstanceOf(StoredObject::class), '1234', 'application/test') + ->willReturnCallback(fn (StoredObject $so, $content, $type) => $so->registerVersion(type: $type)); + + $storedObjectDuplicate = new StoredObjectDuplicate($manager, new NullLogger()); + + $actual = $storedObjectDuplicate->duplicate($storedObject); + + self::assertNotNull($actual->getCurrentVersion()); + self::assertNotNull($actual->getCurrentVersion()->getCreatedFrom()); + self::assertSame($version, $actual->getCurrentVersion()->getCreatedFrom()); + } +}