From a59fb8c72ec7ac3cf02d0b9404337c5ed757cd3c Mon Sep 17 00:00:00 2001 From: Thomas Moutlen Date: Sat, 9 Nov 2024 18:12:02 +0100 Subject: [PATCH 1/2] feat: add delete endpoint --- Api/Data/DeletePayloadInterface.php | 41 +++++++++++++ Api/Data/DeleteResponseInterface.php | 30 +++++++++ Api/ProductManagementInterface.php | 9 +++ Model/Data/Delete/DeletePayload.php | 51 ++++++++++++++++ Model/Data/Delete/DeleteResponse.php | 61 +++++++++++++++++++ Model/ProductManagement.php | 48 ++++++++++++--- Model/Service/DeleteProductAttribute.php | 59 ++++++++++++++++++ Model/{ => Service}/GetProductAttributes.php | 6 +- Model/{ => Service}/SaveProductAttributes.php | 2 +- etc/di.xml | 4 +- etc/webapi.xml | 8 +++ 11 files changed, 306 insertions(+), 13 deletions(-) create mode 100644 Api/Data/DeletePayloadInterface.php create mode 100644 Api/Data/DeleteResponseInterface.php create mode 100644 Model/Data/Delete/DeletePayload.php create mode 100644 Model/Data/Delete/DeleteResponse.php create mode 100644 Model/Service/DeleteProductAttribute.php rename Model/{ => Service}/GetProductAttributes.php (94%) rename Model/{ => Service}/SaveProductAttributes.php (97%) diff --git a/Api/Data/DeletePayloadInterface.php b/Api/Data/DeletePayloadInterface.php new file mode 100644 index 0000000..8c4f0a8 --- /dev/null +++ b/Api/Data/DeletePayloadInterface.php @@ -0,0 +1,41 @@ +entityId; + } + + public function setEntityId(int $entityId): void + { + $this->entityId = $entityId; + } + + public function getStoreViewId(): int + { + return $this->storeViewId; + } + + public function setStoreViewId(int $storeViewId): void + { + $this->storeViewId = $storeViewId; + } + + public function getAttributeCode(): string + { + return $this->attributeCode; + } + + public function setAttributeCode(string $attributeCode): void + { + $this->attributeCode = $attributeCode; + } +} diff --git a/Model/Data/Delete/DeleteResponse.php b/Model/Data/Delete/DeleteResponse.php new file mode 100644 index 0000000..ee2442d --- /dev/null +++ b/Model/Data/Delete/DeleteResponse.php @@ -0,0 +1,61 @@ +type; + } + + /** + * @param string $type + * @return void + */ + public function setType(string $type): void + { + $this->type = $type; + } + + /** + * @return string + */ + public function getMessage(): string + { + return $this->message; + } + + /** + * @param string $message + * @return void + */ + public function setMessage(string $message): void + { + $this->message = $message; + } +} diff --git a/Model/ProductManagement.php b/Model/ProductManagement.php index a01938c..47e55c0 100644 --- a/Model/ProductManagement.php +++ b/Model/ProductManagement.php @@ -13,17 +13,22 @@ use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Serialize\SerializerInterface; +use Opengento\BetterBo\Api\Data\DeletePayloadInterface; +use Opengento\BetterBo\Api\Data\DeletePayloadInterfaceFactory; +use Opengento\BetterBo\Api\Data\DeleteResponseInterface; +use Opengento\BetterBo\Api\Data\DeleteResponseInterfaceFactory; use Opengento\BetterBo\Api\Data\GetPayloadInterface; use Opengento\BetterBo\Api\Data\GetPayloadInterfaceFactory; -use Opengento\BetterBo\Api\Data\SavePayloadInterfaceFactory; use Opengento\BetterBo\Api\Data\SavePayloadInterface; +use Opengento\BetterBo\Api\Data\SavePayloadInterfaceFactory; use Opengento\BetterBo\Api\Data\SavePayloadValueInterface; use Opengento\BetterBo\Api\Data\SaveResponseInterface; use Opengento\BetterBo\Api\Data\SaveResponseInterfaceFactory; use Opengento\BetterBo\Api\GetProductAttributesInterface; use Opengento\BetterBo\Api\ProductManagementInterface; use Opengento\BetterBo\Model\Exception\PayloadValidationException; -use function array_column; +use Opengento\BetterBo\Model\Service\DeleteProductAttribute; +use Opengento\BetterBo\Model\Service\SaveProductAttributes; class ProductManagement implements ProductManagementInterface { @@ -31,12 +36,15 @@ class ProductManagement implements ProductManagementInterface public const TYPE_ERROR = 'error'; public function __construct( - protected GetPayloadInterfaceFactory $getPayloadFactory, - protected SavePayloadInterfaceFactory $savePayloadInterfaceFactory, - protected SerializerInterface $serializer, - protected GetProductAttributesInterface $getProductAttributes, - protected SaveProductAttributes $saveProductAttributes, - protected SaveResponseInterfaceFactory $saveResponseInterfaceFactory + protected GetPayloadInterfaceFactory $getPayloadFactory, + protected SavePayloadInterfaceFactory $savePayloadInterfaceFactory, + protected SerializerInterface $serializer, + protected GetProductAttributesInterface $getProductAttributes, + protected SaveProductAttributes $saveProductAttributes, + protected SaveResponseInterfaceFactory $saveResponseInterfaceFactory, + protected DeleteResponseInterfaceFactory $deleteResponseInterfaceFactory, + protected DeletePayloadInterfaceFactory $deletePayloadInterfaceFactory, + protected DeleteProductAttribute $deleteProductAttribute ) { } @@ -129,4 +137,28 @@ protected function initSavePayload(string $entityId, string $attributeCode, arra return $payload; } + + /** + * @throws PayloadValidationException + */ + protected function initDeletePayload(string $entityId, string $attributeCode, string $storeViewId): DeletePayloadInterface + { + if (empty($entityId) || empty($attributeCode) || empty($storeViewId)) { + throw new PayloadValidationException(__('Invalid DeletePayload')); + } + + /** @var \Opengento\BetterBo\Api\Data\DeletePayloadInterface $payload */ + $payload = $this->deletePayloadInterfaceFactory->create(); + $payload->setEntityId((int)$entityId); + $payload->setStoreViewId((int)$storeViewId); + $payload->setAttributeCode($attributeCode); + + return $payload; + } + + public function deleteProductData(string $entityId, string $attributeCode, string $storeViewId): DeleteResponseInterface + { + $payload = $this->initDeletePayload($entityId, $attributeCode, $storeViewId); + return $this->deleteProductAttribute->execute($payload); + } } diff --git a/Model/Service/DeleteProductAttribute.php b/Model/Service/DeleteProductAttribute.php new file mode 100644 index 0000000..9214f0f --- /dev/null +++ b/Model/Service/DeleteProductAttribute.php @@ -0,0 +1,59 @@ +productAction->updateAttributes( + [$payload->getEntityId()], + [$payload->getAttributeCode() => null], + $payload->getStoreViewId() + ); + $type = 'success'; + } catch (Exception $e) { + $message = $e->getMessage(); + } + + return $this->getResponse($type, $message); + } + + protected function getResponse(string $type, string $message): \Opengento\BetterBo\Api\Data\DeleteResponseInterface + { + /** @var \Opengento\BetterBo\Api\Data\DeleteResponseInterface $response */ + $response = $this->deleteResponseValueFactory->create(); + $response->setMessage($message); + $response->setType($type); + return $response; + } +} diff --git a/Model/GetProductAttributes.php b/Model/Service/GetProductAttributes.php similarity index 94% rename from Model/GetProductAttributes.php rename to Model/Service/GetProductAttributes.php index 7368965..40271bd 100644 --- a/Model/GetProductAttributes.php +++ b/Model/Service/GetProductAttributes.php @@ -9,11 +9,11 @@ declare(strict_types=1); -namespace Opengento\BetterBo\Model; +namespace Opengento\BetterBo\Model\Service; use Magento\Catalog\Api\Data\ProductAttributeInterface; use Magento\Catalog\Api\ProductRepositoryInterface; -use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory; +use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; use Magento\Eav\Api\AttributeRepositoryInterface; use Magento\Eav\Api\Data\AttributeOptionInterface; use Magento\Framework\Exception\NoSuchEntityException; @@ -38,7 +38,7 @@ class GetProductAttributes implements GetProductAttributesInterface public function __construct( protected GroupRepositoryInterface $groupRepository, protected AttributeRepositoryInterface $attributeRepository, - protected \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, + protected CollectionFactory $productCollectionFactory, protected StoreManagerInterface $storeManager, protected ProductRepositoryInterface $productRepository, ) diff --git a/Model/SaveProductAttributes.php b/Model/Service/SaveProductAttributes.php similarity index 97% rename from Model/SaveProductAttributes.php rename to Model/Service/SaveProductAttributes.php index ca586b0..f4f14aa 100644 --- a/Model/SaveProductAttributes.php +++ b/Model/Service/SaveProductAttributes.php @@ -9,7 +9,7 @@ declare(strict_types=1); -namespace Opengento\BetterBo\Model; +namespace Opengento\BetterBo\Model\Service; use Magento\Catalog\Model\Product\Action; use Opengento\BetterBo\Api\Data\SavePayloadInterface; diff --git a/etc/di.xml b/etc/di.xml index 3696c90..cdc3e6d 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -12,5 +12,7 @@ - + + + diff --git a/etc/webapi.xml b/etc/webapi.xml index d289292..5448a4a 100644 --- a/etc/webapi.xml +++ b/etc/webapi.xml @@ -23,4 +23,12 @@ + + + + + + + + From 1c0dae69a86212ae7f8e03eb0fb02aa3ab209c3d Mon Sep 17 00:00:00 2001 From: Thomas Moutlen Date: Sat, 9 Nov 2024 18:25:05 +0100 Subject: [PATCH 2/2] chore: quality code phpcsfixer --- Api/Data/DeletePayloadInterface.php | 2 -- Api/Data/DeleteResponseInterface.php | 2 -- Api/Data/SavePayloadInterface.php | 1 - Api/Data/SavePayloadValueInterface.php | 1 - Api/Data/SaveResponseInterface.php | 2 -- Api/ProductManagementInterface.php | 2 -- Model/Config.php | 2 +- Model/Data/Get/GetPayload.php | 2 +- Model/ProductManagement.php | 5 ++--- Model/Service/DeleteProductAttribute.php | 3 +-- Model/Service/GetProductAttributes.php | 16 ++++++---------- Model/Service/SaveProductAttributes.php | 5 ++--- Plugin/CategoryEavDataProviderPlugin.php | 2 +- Plugin/ConfigFieldPlugin.php | 15 ++++++++------- Plugin/ProductEavDataProviderPlugin.php | 9 +++++---- registration.php | 9 +++++---- .../catalog/product/attribute_by_stores.phtml | 4 ++-- 17 files changed, 34 insertions(+), 48 deletions(-) diff --git a/Api/Data/DeletePayloadInterface.php b/Api/Data/DeletePayloadInterface.php index 8c4f0a8..d4294fc 100644 --- a/Api/Data/DeletePayloadInterface.php +++ b/Api/Data/DeletePayloadInterface.php @@ -1,6 +1,5 @@ attributeCode = $attribute; + $this->attributeCode = $attribute; } public function getEntityId(): int diff --git a/Model/ProductManagement.php b/Model/ProductManagement.php index 47e55c0..93c8e1e 100644 --- a/Model/ProductManagement.php +++ b/Model/ProductManagement.php @@ -45,8 +45,7 @@ public function __construct( protected DeleteResponseInterfaceFactory $deleteResponseInterfaceFactory, protected DeletePayloadInterfaceFactory $deletePayloadInterfaceFactory, protected DeleteProductAttribute $deleteProductAttribute - ) - { + ) { } /** @@ -86,7 +85,7 @@ public function getProductData(string $entityId, string $attributeCode): string $result = [ 'type' => self::TYPE_ERROR, 'message' => '', - 'data' => [] + 'data' => [], ]; try { diff --git a/Model/Service/DeleteProductAttribute.php b/Model/Service/DeleteProductAttribute.php index 9214f0f..b7e229c 100644 --- a/Model/Service/DeleteProductAttribute.php +++ b/Model/Service/DeleteProductAttribute.php @@ -21,8 +21,7 @@ class DeleteProductAttribute public function __construct( protected Action $productAction, protected DeleteResponseInterfaceFactory $deleteResponseValueFactory - ) - { + ) { } /** diff --git a/Model/Service/GetProductAttributes.php b/Model/Service/GetProductAttributes.php index 40271bd..e173d2c 100644 --- a/Model/Service/GetProductAttributes.php +++ b/Model/Service/GetProductAttributes.php @@ -41,8 +41,7 @@ public function __construct( protected CollectionFactory $productCollectionFactory, protected StoreManagerInterface $storeManager, protected ProductRepositoryInterface $productRepository, - ) - { + ) { } /** @@ -53,7 +52,7 @@ public function execute(GetPayloadInterface $payload): array { return [ 'config' => $this->getAttributeConfig($payload->getAttributeCode()), - 'values' => $this->getAttributeValues($payload) + 'values' => $this->getAttributeValues($payload), ]; } @@ -75,16 +74,15 @@ protected function getAttributeConfig(string $attributeCode): array 'type' => $attribute->getFrontendInput(), 'frontendLabel' => $attribute->getDefaultFrontendLabel(), 'options' => array_map( - static fn(AttributeOptionInterface $option) => [ + static fn (AttributeOptionInterface $option) => [ 'label' => $option->getLabel(), - 'value' => (string)$option->getValue() + 'value' => (string)$option->getValue(), ], $attribute->getOptions() ), ]; } - /** * @param GetPayloadInterface $payload * @return array @@ -100,7 +98,7 @@ protected function getAttributeValues(GetPayloadInterface $payload): array $results[] = [ 'storeViewId' => $storeView->getId(), 'storeViewLabel' => $this->buildStoreViewLabel($storeView), - 'value' => $product->getData($payload->getAttributeCode()) + 'value' => $product->getData($payload->getAttributeCode()), ]; } @@ -132,8 +130,6 @@ protected function getStoreGroups(): array protected function sortResults(array &$results): void { - usort($results, function ($a, $b) { - return strcmp($a['storeViewLabel'], $b['storeViewLabel']); - }); + usort($results, fn ($a, $b) => strcmp($a['storeViewLabel'], $b['storeViewLabel'])); } } diff --git a/Model/Service/SaveProductAttributes.php b/Model/Service/SaveProductAttributes.php index f4f14aa..efcccb6 100644 --- a/Model/Service/SaveProductAttributes.php +++ b/Model/Service/SaveProductAttributes.php @@ -21,8 +21,7 @@ class SaveProductAttributes public function __construct( protected Action $productAction, protected SaveResponseValueInterfaceFactory $saveResponseValueFactory - ) - { + ) { } /** @@ -37,7 +36,7 @@ public function execute(SavePayloadInterface $payload): \Opengento\BetterBo\Api\ $payload->getValues(), static function ($r, SavePayloadValueInterface $value) use ($payload) { $r[$value->getStoreViewId()] = [ - $payload->getAttributeCode() => $value->getValue() + $payload->getAttributeCode() => $value->getValue(), ]; return $r; }, diff --git a/Plugin/CategoryEavDataProviderPlugin.php b/Plugin/CategoryEavDataProviderPlugin.php index 0b2ec11..d2719fa 100644 --- a/Plugin/CategoryEavDataProviderPlugin.php +++ b/Plugin/CategoryEavDataProviderPlugin.php @@ -40,7 +40,7 @@ public function afterPrepareFieldsMeta( foreach ($fieldsMap as $fieldSet => $fields) { foreach ($fields as $field) { // if (isset($result[$fieldSet]['children'][$field]['arguments']['data']['config']) && (int) $currentStoreViewId === (int) $adminStoreViewId) { - $result[$fieldSet]['children'][$field]['arguments']['data']['config']['storebtn'] = ""; + $result[$fieldSet]['children'][$field]['arguments']['data']['config']['storebtn'] = ""; // } } } diff --git a/Plugin/ConfigFieldPlugin.php b/Plugin/ConfigFieldPlugin.php index 71f463a..0a4cc1a 100644 --- a/Plugin/ConfigFieldPlugin.php +++ b/Plugin/ConfigFieldPlugin.php @@ -1,4 +1,5 @@ scopeConfig->getValue($path); // } // $scopeValue = $this->scopeConfig->getValue($path, $scopeType, $scope->getId()); - + // if (is_array($currentValue) || is_array($scopeValue)) { // return $scopeLine; // } - + // $currentValue = (string) $currentValue; // $scopeValue = (string) $scopeValue; - + // // dd($currentValue, $scopeValue); // if ($scopeValue != $currentValue) { // $scopeValue = $this->escaper->escapeHtml($scopeValue); @@ -200,4 +201,4 @@ private function getPath(Subject $subject) // { // return $this->request->getParam('store'); // } -} \ No newline at end of file +} diff --git a/Plugin/ProductEavDataProviderPlugin.php b/Plugin/ProductEavDataProviderPlugin.php index 6d61431..0950afb 100644 --- a/Plugin/ProductEavDataProviderPlugin.php +++ b/Plugin/ProductEavDataProviderPlugin.php @@ -13,7 +13,6 @@ use Magento\Store\Model\StoreManagerInterface; use Opengento\BetterBo\Model\Config; use Psr\Log\LoggerInterface; -use Magento\Store\Model\Store; /** * @property StoreInterface[] $stores @@ -48,8 +47,11 @@ public function afterSetupAttributeMeta(Eav $subject, array $result): array $storeSvg = ""; if (!isset($result['arguments']['data']['config']['code']) || $result['arguments']['data']['config']['globalScope'] - || !\in_array($result['arguments']['data']['config']['dataType'], - $this->betterBoConfig->getAttributesFieldsType(), true) + || !\in_array( + $result['arguments']['data']['config']['dataType'], + $this->betterBoConfig->getAttributesFieldsType(), + true + ) ) { return $result; } @@ -61,7 +63,6 @@ public function afterSetupAttributeMeta(Eav $subject, array $result): array return $result; } - $adminStoreViewId = \Magento\Store\Model\Store::DEFAULT_STORE_ID; $currentStoreViewId = $this->storeManager->getStore()->getId(); diff --git a/registration.php b/registration.php index 2b5345d..3a15405 100644 --- a/registration.php +++ b/registration.php @@ -1,6 +1,7 @@ getRequest()->getParam('id'); +$productId = $block->getRequest()->getParam('id'); ?>