From 4d062d2b666269dfdf408e2155ae0956fa40e021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Arroyo?= Date: Thu, 13 Jul 2023 16:09:48 +0200 Subject: [PATCH 1/7] feat: taoItems_models_classes_ItemsService::delete --- models/classes/class.ItemsService.php | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/models/classes/class.ItemsService.php b/models/classes/class.ItemsService.php index 8ee98724..150d57c4 100755 --- a/models/classes/class.ItemsService.php +++ b/models/classes/class.ItemsService.php @@ -35,6 +35,9 @@ use oat\taoItems\model\event\ItemRemovedEvent; use oat\taoItems\model\ItemModelStatus; use oat\taoQtiItem\helpers\QtiFile; +use oat\taoQtiItem\model\qti\Service as QtiItemService; +use oat\taoQtiItem\model\qti\parser\ElementReferencesExtractor; +use Psr\Container\ContainerInterface; /** * Service methods to manage the Items business models using the RDF API. @@ -132,6 +135,39 @@ public function isItemClass(core_kernel_classes_Class $clazz) return $clazz->equals($this->getRootClass()) || $clazz->isSubClassOf($this->getRootClass()); } + // @todo Recheck name + // @todo Create command class + public function delete(array $command): void + { + $resource = $command['resource']; // @todo + + if (LockManager::getImplementation()->isLocked($resource)) { + $userId = common_session_SessionManager::getSession()->getUser()->getIdentifier(); + LockManager::getImplementation()->releaseLock($resource, $userId); + } + + $result = $this->deleteItemContent($resource) && parent::deleteResource($resource); + + if (!$result) { + throw new Exception( + sprintf( + 'Error deleting item content for resource "%s" [%s]', + $resource->getLabel(), + $resource->getId() + ) + ); + } + + $this->getEventManager()->trigger( + new ItemRemovedEvent( + $resource->getUri(), + [ + ItemRemovedEvent::PAYLOAD_KEY_DELETE_ASSETS => (bool) $command['deleteAssets'], + ] + ) + ); + } + /** * please call deleteResource() instead * @deprecated From df4e0d174a7506682e3730b824a89a332825eec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Arroyo?= Date: Thu, 13 Jul 2023 16:10:13 +0200 Subject: [PATCH 2/7] feat: Extend ItemRemovedEvent --- models/classes/event/ItemRemovedEvent.php | 37 ++++++++--------------- 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/models/classes/event/ItemRemovedEvent.php b/models/classes/event/ItemRemovedEvent.php index c6d6eda4..37e2bc0a 100644 --- a/models/classes/event/ItemRemovedEvent.php +++ b/models/classes/event/ItemRemovedEvent.php @@ -15,49 +15,38 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * - * Copyright (c) 2016 (original work) Open Assessment Technologies SA - * + * Copyright (c) 2016-2023 (original work) Open Assessment Technologies SA. */ namespace oat\taoItems\model\event; -use JsonSerializable; use oat\oatbox\event\Event; +use JsonSerializable; class ItemRemovedEvent implements Event, JsonSerializable { - /** @var string */ - protected $itemUri; + public const PAYLOAD_KEY_DELETE_ASSETS = 'deleteAssets'; - /** - * @param String $itemUri - */ - public function __construct($itemUri) + protected string $itemUri; + + private array $payload; + + public function __construct(string $itemUri, array $payload = []) { $this->itemUri = $itemUri; + $this->payload = $payload; } - - /** - * Return a unique name for this event - * @see \oat\oatbox\event\Event::getName() - */ public function getName() { return get_class($this); } - /** - * Specify data which should be serialized to JSON - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php - * @return mixed data which can be serialized by json_encode, - * which is a value of any type other than a resource. - * @since 5.4.0 - */ public function jsonSerialize() { - return [ - 'itemUri' => $this->itemUri - ]; + return array_merge( + ['itemUri' => $this->itemUri], + $this->payload + ); } } From c88b365a25fcd90a80e64352502c1af6b49d509b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Arroyo?= Date: Thu, 13 Jul 2023 16:22:56 +0200 Subject: [PATCH 3/7] chore: PSR-12 stuff --- models/classes/class.ItemsService.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/models/classes/class.ItemsService.php b/models/classes/class.ItemsService.php index 150d57c4..bf33a13a 100755 --- a/models/classes/class.ItemsService.php +++ b/models/classes/class.ItemsService.php @@ -140,7 +140,7 @@ public function isItemClass(core_kernel_classes_Class $clazz) public function delete(array $command): void { $resource = $command['resource']; // @todo - + if (LockManager::getImplementation()->isLocked($resource)) { $userId = common_session_SessionManager::getSession()->getUser()->getIdentifier(); LockManager::getImplementation()->releaseLock($resource, $userId); @@ -157,7 +157,7 @@ public function delete(array $command): void ) ); } - + $this->getEventManager()->trigger( new ItemRemovedEvent( $resource->getUri(), @@ -167,7 +167,7 @@ public function delete(array $command): void ) ); } - + /** * please call deleteResource() instead * @deprecated From b332891000292a4fe3d0346d336a617f0cbf828a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Arroyo?= Date: Fri, 14 Jul 2023 13:20:25 +0200 Subject: [PATCH 4/7] chore: Remove unused imports --- models/classes/class.ItemsService.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/models/classes/class.ItemsService.php b/models/classes/class.ItemsService.php index bf33a13a..b21e1bbc 100755 --- a/models/classes/class.ItemsService.php +++ b/models/classes/class.ItemsService.php @@ -19,7 +19,7 @@ * (under the project TAO-TRANSFER); * 2009-2012 (update and modification) Public Research Centre Henri Tudor * (under the project TAO-SUSTAIN & TAO-DEV); - * 2012-2016 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT) + * 2012-2023 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT) */ use oat\taoItems\model\TaoItemOntology; @@ -35,9 +35,6 @@ use oat\taoItems\model\event\ItemRemovedEvent; use oat\taoItems\model\ItemModelStatus; use oat\taoQtiItem\helpers\QtiFile; -use oat\taoQtiItem\model\qti\Service as QtiItemService; -use oat\taoQtiItem\model\qti\parser\ElementReferencesExtractor; -use Psr\Container\ContainerInterface; /** * Service methods to manage the Items business models using the RDF API. From 82ff017cb219956f51cafbfbd75c4ef8e72c0675 Mon Sep 17 00:00:00 2001 From: Gabriel Felipe Soares Date: Fri, 14 Jul 2023 13:36:26 +0200 Subject: [PATCH 5/7] chore: create command for better encapsulation + deprecate method --- models/classes/Command/DeleteItemCommand.php | 47 ++++++++++++++++++++ models/classes/class.ItemsService.php | 27 +++++------ 2 files changed, 58 insertions(+), 16 deletions(-) create mode 100644 models/classes/Command/DeleteItemCommand.php diff --git a/models/classes/Command/DeleteItemCommand.php b/models/classes/Command/DeleteItemCommand.php new file mode 100644 index 00000000..0b2820ba --- /dev/null +++ b/models/classes/Command/DeleteItemCommand.php @@ -0,0 +1,47 @@ +resource = $resource; + $this->deleteRelatedAssets = $deleteRelatedAssets; + } + + public function getResource(): core_kernel_classes_Resource + { + return $this->resource; + } + + public function mustDeleteRelatedAssets(): bool + { + return $this->deleteRelatedAssets; + } +} diff --git a/models/classes/class.ItemsService.php b/models/classes/class.ItemsService.php index b21e1bbc..71b1572a 100755 --- a/models/classes/class.ItemsService.php +++ b/models/classes/class.ItemsService.php @@ -22,6 +22,7 @@ * 2012-2023 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT) */ +use oat\taoItems\model\Command\DeleteItemCommand; use oat\taoItems\model\TaoItemOntology; use oat\generis\model\fileReference\FileReferenceSerializer; use oat\oatbox\filesystem\Directory; @@ -132,11 +133,9 @@ public function isItemClass(core_kernel_classes_Class $clazz) return $clazz->equals($this->getRootClass()) || $clazz->isSubClassOf($this->getRootClass()); } - // @todo Recheck name - // @todo Create command class - public function delete(array $command): void + public function delete(DeleteItemCommand $command): void { - $resource = $command['resource']; // @todo + $resource = $command->getResource(); if (LockManager::getImplementation()->isLocked($resource)) { $userId = common_session_SessionManager::getSession()->getUser()->getIdentifier(); @@ -150,7 +149,7 @@ public function delete(array $command): void sprintf( 'Error deleting item content for resource "%s" [%s]', $resource->getLabel(), - $resource->getId() + $resource->getUri() ) ); } @@ -159,7 +158,7 @@ public function delete(array $command): void new ItemRemovedEvent( $resource->getUri(), [ - ItemRemovedEvent::PAYLOAD_KEY_DELETE_ASSETS => (bool) $command['deleteAssets'], + ItemRemovedEvent::PAYLOAD_KEY_DELETE_ASSETS => $command->mustDeleteRelatedAssets(), ] ) ); @@ -179,21 +178,17 @@ public function deleteItem(core_kernel_classes_Resource $item) * @param core_kernel_classes_Resource $resource * @throws common_exception_Unauthorized * @return boolean + * @deprecated use self::delete() */ public function deleteResource(core_kernel_classes_Resource $resource) { - if (LockManager::getImplementation()->isLocked($resource)) { - $userId = common_session_SessionManager::getSession()->getUser()->getIdentifier(); - LockManager::getImplementation()->releaseLock($resource, $userId); - } - - $result = $this->deleteItemContent($resource) && parent::deleteResource($resource); + try { + $this->delete(new DeleteItemCommand($resource)); - if ($result) { - $this->getEventManager()->trigger(new ItemRemovedEvent($resource->getUri())); + return true; + } catch (Throwable $exception) { + return false; } - - return $result; } /** From 4bca6aedb46803ac7dd45f2810f1eb20944e0c87 Mon Sep 17 00:00:00 2001 From: Gabriel Felipe Soares Date: Fri, 14 Jul 2023 13:52:14 +0200 Subject: [PATCH 6/7] chore: add constants to hold all data from event and use proper delete command methods --- models/classes/class.ItemsService.php | 2 +- models/classes/event/ItemRemovedEvent.php | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/models/classes/class.ItemsService.php b/models/classes/class.ItemsService.php index 71b1572a..21d65def 100755 --- a/models/classes/class.ItemsService.php +++ b/models/classes/class.ItemsService.php @@ -158,7 +158,7 @@ public function delete(DeleteItemCommand $command): void new ItemRemovedEvent( $resource->getUri(), [ - ItemRemovedEvent::PAYLOAD_KEY_DELETE_ASSETS => $command->mustDeleteRelatedAssets(), + ItemRemovedEvent::PAYLOAD_KEY_DELETE_RELATED_ASSETS => $command->mustDeleteRelatedAssets(), ] ) ); diff --git a/models/classes/event/ItemRemovedEvent.php b/models/classes/event/ItemRemovedEvent.php index 37e2bc0a..8adc5630 100644 --- a/models/classes/event/ItemRemovedEvent.php +++ b/models/classes/event/ItemRemovedEvent.php @@ -25,7 +25,8 @@ class ItemRemovedEvent implements Event, JsonSerializable { - public const PAYLOAD_KEY_DELETE_ASSETS = 'deleteAssets'; + public const PAYLOAD_KEY_DELETE_RELATED_ASSETS = 'deleteRelatedAssets'; + public const PAYLOAD_KEY_ITEM_URI = 'itemUri'; protected string $itemUri; @@ -45,7 +46,7 @@ public function getName() public function jsonSerialize() { return array_merge( - ['itemUri' => $this->itemUri], + [self::PAYLOAD_KEY_ITEM_URI => $this->itemUri], $this->payload ); } From 784ef53c88233e14029d39514e223a63027d3904 Mon Sep 17 00:00:00 2001 From: Gabriel Felipe Soares Date: Fri, 21 Jul 2023 16:20:50 +0200 Subject: [PATCH 7/7] chore: add unit tests --- .../unit/model/event/ItemRemovedEventTest.php | 48 +++++++++++++++++++ .../classes/Command/DeleteItemCommandTest.php | 39 +++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 test/unit/model/event/ItemRemovedEventTest.php create mode 100644 test/unit/models/classes/Command/DeleteItemCommandTest.php diff --git a/test/unit/model/event/ItemRemovedEventTest.php b/test/unit/model/event/ItemRemovedEventTest.php new file mode 100644 index 00000000..5bbe5c80 --- /dev/null +++ b/test/unit/model/event/ItemRemovedEventTest.php @@ -0,0 +1,48 @@ + true, + ] + ); + + $this->assertSame(ItemRemovedEvent::class, $event->getName()); + $this->assertSame( + [ + ItemRemovedEvent::PAYLOAD_KEY_ITEM_URI => 'itemUri', + ItemRemovedEvent::PAYLOAD_KEY_DELETE_RELATED_ASSETS => true, + ], + $event->jsonSerialize() + ); + } +} diff --git a/test/unit/models/classes/Command/DeleteItemCommandTest.php b/test/unit/models/classes/Command/DeleteItemCommandTest.php new file mode 100644 index 00000000..74a011ee --- /dev/null +++ b/test/unit/models/classes/Command/DeleteItemCommandTest.php @@ -0,0 +1,39 @@ +createMock(core_kernel_classes_Resource::class); + $command = new DeleteItemCommand($resource, true); + + $this->assertSame($resource, $command->getResource()); + $this->assertTrue($command->mustDeleteRelatedAssets()); + } +}