Skip to content

Feature/delete endpoint #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Api/Data/DeletePayloadInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Opengento\BetterBo\Api\Data;

interface DeletePayloadInterface
{
/**
* @return mixed
*/
public function getEntityId();

/**
* @param int $entityId
* @return mixed
*/
public function setEntityId(int $entityId);

/**
* @return mixed
*/
public function getStoreViewId();

/**
* @param int $storeViewId
* @return mixed
*/
public function setStoreViewId(int $storeViewId);

/**
* @return mixed
*/
public function getAttributeCode();

/**
* @param string $attributeCode
* @return mixed
*/
public function setAttributeCode(string $attributeCode);
}
28 changes: 28 additions & 0 deletions Api/Data/DeleteResponseInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Opengento\BetterBo\Api\Data;

interface DeleteResponseInterface
{
/**
* @return string
*/
public function getType(): string;

/**
* @param string $type
* @return void
*/
public function setType(string $type): void;

/**
* @return string
*/
public function getMessage(): string;

/**
* @param string $message
* @return void
*/
public function setMessage(string $message): void;
}
1 change: 0 additions & 1 deletion Api/Data/SavePayloadInterface.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


namespace Opengento\BetterBo\Api\Data;

interface SavePayloadInterface
Expand Down
1 change: 0 additions & 1 deletion Api/Data/SavePayloadValueInterface.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


namespace Opengento\BetterBo\Api\Data;

interface SavePayloadValueInterface
Expand Down
2 changes: 0 additions & 2 deletions Api/Data/SaveResponseInterface.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace Opengento\BetterBo\Api\Data;


interface SaveResponseInterface
{
/**
Expand Down
9 changes: 8 additions & 1 deletion Api/ProductManagementInterface.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


namespace Opengento\BetterBo\Api;

use Opengento\BetterBo\Api\Data\SavePayloadValueInterface;
Expand All @@ -22,4 +21,12 @@ public function getProductData(string $entityId, string $attributeCode): string;
* @return SaveResponseInterface
*/
public function saveProductData(string $entityId, string $attributeCode, array $values): SaveResponseInterface;

/**
* @param string $entityId
* @param string $attributeCode
* @param string $storeViewId
* @return \Opengento\BetterBo\Api\Data\DeleteResponseInterface
*/
public function deleteProductData(string $entityId, string $attributeCode, string $storeViewId): \Opengento\BetterBo\Api\Data\DeleteResponseInterface;
}
2 changes: 1 addition & 1 deletion Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Opengento\BetterBo\Model;

use Opengento\BetterBo\Api\ConfigInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Opengento\BetterBo\Api\ConfigInterface;

class Config implements ConfigInterface
{
Expand Down
51 changes: 51 additions & 0 deletions Model/Data/Delete/DeletePayload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* SaveResponse
*
* @copyright Copyright © 2024 Blackbird Agency. All rights reserved.
* @author [email protected]
*/

declare(strict_types=1);

namespace Opengento\BetterBo\Model\Data\Delete;

use Opengento\BetterBo\Api\Data\DeletePayloadInterface;

class DeletePayload implements DeletePayloadInterface
{
protected int $entityId;
protected int $storeViewId;
public string $attributeCode;

public function getEntityId(): int
{
return $this->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;
}
}
61 changes: 61 additions & 0 deletions Model/Data/Delete/DeleteResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* SaveResponse
*
* @copyright Copyright © 2024 Blackbird Agency. All rights reserved.
* @author [email protected]
*/

declare(strict_types=1);

namespace Opengento\BetterBo\Model\Data\Delete;

use Opengento\BetterBo\Api\Data\DeleteResponseInterface;

class DeleteResponse implements DeleteResponseInterface
{
/**
* @var string
*/
protected string $type;

/**
* @var string
*/
protected string $message;

/**
* @return string
*/
public function getType(): string
{
return $this->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;
}
}
2 changes: 1 addition & 1 deletion Model/Data/Get/GetPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function getAttributeCode(): string

public function setAttributeCode(string $attribute): void
{
$this->attributeCode = $attribute;
$this->attributeCode = $attribute;
}

public function getEntityId(): int
Expand Down
53 changes: 42 additions & 11 deletions Model/ProductManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,39 @@

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
{
public const TYPE_SUCCESS = 'success';
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
) {
}

/**
Expand Down Expand Up @@ -78,7 +85,7 @@ public function getProductData(string $entityId, string $attributeCode): string
$result = [
'type' => self::TYPE_ERROR,
'message' => '',
'data' => []
'data' => [],
];

try {
Expand Down Expand Up @@ -129,4 +136,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);
}
}
58 changes: 58 additions & 0 deletions Model/Service/DeleteProductAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* SaveProductAttributes
*
* @copyright Copyright © 2024 Blackbird Agency. All rights reserved.
* @author [email protected]
*/

declare(strict_types=1);

namespace Opengento\BetterBo\Model\Service;

use Exception;
use Magento\Catalog\Model\Product\Action;
use Opengento\BetterBo\Api\Data\DeletePayloadInterface;
use Opengento\BetterBo\Api\Data\DeleteResponseInterfaceFactory;

class DeleteProductAttribute
{
public function __construct(
protected Action $productAction,
protected DeleteResponseInterfaceFactory $deleteResponseValueFactory
) {
}

/**
* @param DeletePayloadInterface $payload
* @return \Opengento\BetterBo\Api\Data\DeleteResponseInterface
*/
public function execute(DeletePayloadInterface $payload): \Opengento\BetterBo\Api\Data\DeleteResponseInterface
{
$type = 'error';
$message = '';

try {
$this->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;
}
}
Loading
Loading