Skip to content

Commit

Permalink
FRW-7201 Added DELETE method support for Data Exchange API. (#10925)
Browse files Browse the repository at this point in the history
FRW-7201 Added DELETE method support for Data Exchange API.
  • Loading branch information
dimitriyTsemma authored May 27, 2024
1 parent 24c73a2 commit 3db92f3
Show file tree
Hide file tree
Showing 26 changed files with 714 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<transfer name="DynamicEntityDefinition" strict="true">
<property name="identifier" type="string"/>
<property name="fieldDefinitions" type="DynamicEntityFieldDefinition[]" singular="fieldDefinition"/>
<property name="isDeletable" type="bool"/>
</transfer>

<transfer name="DynamicEntityFieldDefinition" strict="true">
Expand Down Expand Up @@ -103,6 +104,7 @@
<transfer name="DynamicEntityCollectionDeleteCriteria" strict="true">
<property name="isTransactional" type="bool"/>
<property name="dynamicEntityIds" type="int[]" singular="idDynamicEntity"/>
<property name="dynamicEntityConditions" type="DynamicEntityConditions"/>
</transfer>

<transfer name="Error">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ protected function executeDynamicEntityPostCreatePlugins(
DynamicEntityConfigurationTransfer $dynamicEntityConfigurationTransfer,
DynamicEntityCollectionResponseTransfer $dynamicEntityCollectionResponseTransfer
): DynamicEntityCollectionResponseTransfer {
$dynamicEntityPostEditResponseTransfer = new DynamicEntityPostEditResponseTransfer();
$dynamicEntityPostEditRequestTransfers = $this->dynamicEntityMapper
->mapDynamicEntityCollectionResponseTransferToPostEditRequestTransfersArray(
$dynamicEntityConfigurationTransfer,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace Spryker\Zed\DynamicEntity\Business\Deleter;

use Generated\Shared\Transfer\DynamicEntityCollectionDeleteCriteriaTransfer;
use Generated\Shared\Transfer\DynamicEntityCollectionRequestTransfer;
use Generated\Shared\Transfer\DynamicEntityCollectionResponseTransfer;
use Generated\Shared\Transfer\DynamicEntityCollectionTransfer;
use Generated\Shared\Transfer\DynamicEntityConfigurationTransfer;
use Generated\Shared\Transfer\DynamicEntityCriteriaTransfer;
use Generated\Shared\Transfer\ErrorTransfer;
use Spryker\Zed\DynamicEntity\Business\Mapper\DynamicEntityMapperInterface;
use Spryker\Zed\DynamicEntity\Business\Reader\DynamicEntityReaderInterface;
use Spryker\Zed\DynamicEntity\Persistence\DynamicEntityEntityManagerInterface;

class DynamicEntityDeleter implements DynamicEntityDeleterInterface
{
/**
* @var string
*/
protected const ERROR_MESSAGE_ENTITY_NOT_EXIST = 'dynamic_entity.validation.entity_does_not_exist';

/**
* @var string
*/
protected const ERROR_MESSAGE_METHOD_NOT_ALLOWED = 'dynamic_entity.validation.method_not_allowed';

/**
* @var string
*/
protected const PLACEHOLDER_ALIAS_NAME = '%aliasName%';

/**
* @var string
*/
protected const ERROR_PATH_PLACEHOLDER = '%s[%d]';

/**
* @var int
*/
protected const ERROR_PATH_INDEX = 0;

/**
* @var \Spryker\Zed\DynamicEntity\Persistence\DynamicEntityEntityManagerInterface
*/
protected DynamicEntityEntityManagerInterface $entityManager;

/**
* @var \Spryker\Zed\DynamicEntity\Business\Mapper\DynamicEntityMapperInterface
*/
protected DynamicEntityMapperInterface $dynamicEntityMapper;

/**
* @var \Spryker\Zed\DynamicEntity\Business\Reader\DynamicEntityReaderInterface
*/
protected DynamicEntityReaderInterface $dynamicEntityReader;

/**
* @param \Spryker\Zed\DynamicEntity\Persistence\DynamicEntityEntityManagerInterface $entityManager
* @param \Spryker\Zed\DynamicEntity\Business\Mapper\DynamicEntityMapperInterface $dynamicEntityMapper
* @param \Spryker\Zed\DynamicEntity\Business\Reader\DynamicEntityReaderInterface $dynamicEntityReader
*/
public function __construct(
DynamicEntityEntityManagerInterface $entityManager,
DynamicEntityMapperInterface $dynamicEntityMapper,
DynamicEntityReaderInterface $dynamicEntityReader
) {
$this->entityManager = $entityManager;
$this->dynamicEntityMapper = $dynamicEntityMapper;
$this->dynamicEntityReader = $dynamicEntityReader;
}

/**
* @param \Generated\Shared\Transfer\DynamicEntityCollectionDeleteCriteriaTransfer $dynamicEntityCollectionDeleteCriteriaTransfer
*
* @return \Generated\Shared\Transfer\DynamicEntityCollectionResponseTransfer
*/
public function deleteEntityCollection(
DynamicEntityCollectionDeleteCriteriaTransfer $dynamicEntityCollectionDeleteCriteriaTransfer
): DynamicEntityCollectionResponseTransfer {
$dynamicEntityCriteriaTransfer = $this->dynamicEntityMapper->mapDynamicEntityCollectionDeleteCriteriaTransferToDynamicEntityCriteriaTransfer(
$dynamicEntityCollectionDeleteCriteriaTransfer,
new DynamicEntityCriteriaTransfer(),
);

$dynamicEntityConfigurationTransfer = $this->getDynamicEntityConfigurationTransfer($dynamicEntityCriteriaTransfer);

if ($dynamicEntityConfigurationTransfer->getDynamicEntityDefinitionOrFail()->getIsDeletable() !== true) {
return $this->createErrorResponse($dynamicEntityCriteriaTransfer, static::ERROR_MESSAGE_METHOD_NOT_ALLOWED);
}

$dynamicEntityCollectionTransfer = $this->dynamicEntityReader->getEntityCollection($dynamicEntityCriteriaTransfer);

if ($dynamicEntityCollectionTransfer->getDynamicEntities()->count() === 0) {
return $this->createErrorResponse($dynamicEntityCriteriaTransfer, static::ERROR_MESSAGE_ENTITY_NOT_EXIST);
}

if ($dynamicEntityCollectionTransfer->getErrors()->count() !== 0) {
return $this->createDynamicEntityCollectionResponseTransfer($dynamicEntityCollectionTransfer);
}

return $this->entityManager->deleteDynamicEntity(
$dynamicEntityCollectionTransfer,
$dynamicEntityConfigurationTransfer,
);
}

/**
* @param \Generated\Shared\Transfer\DynamicEntityCriteriaTransfer $dynamicEntityCriteriaTransfer
* @param string $errorMessage
*
* @return \Generated\Shared\Transfer\DynamicEntityCollectionResponseTransfer
*/
protected function createErrorResponse(
DynamicEntityCriteriaTransfer $dynamicEntityCriteriaTransfer,
string $errorMessage
): DynamicEntityCollectionResponseTransfer {
$errorTransfer = (new ErrorTransfer())
->setMessage($errorMessage)
->setParameters([
static::PLACEHOLDER_ALIAS_NAME => $dynamicEntityCriteriaTransfer->getDynamicEntityConditionsOrFail()->getTableAliasOrFail(),
]);

return (new DynamicEntityCollectionResponseTransfer())
->addError($errorTransfer);
}

/**
* @param \Generated\Shared\Transfer\DynamicEntityCollectionTransfer $dynamicEntityCollectionTransfer
*
* @return \Generated\Shared\Transfer\DynamicEntityCollectionResponseTransfer
*/
protected function createDynamicEntityCollectionResponseTransfer(
DynamicEntityCollectionTransfer $dynamicEntityCollectionTransfer
): DynamicEntityCollectionResponseTransfer {
$dynamicEntityCollectionResponseTransfer = new DynamicEntityCollectionResponseTransfer();
foreach ($dynamicEntityCollectionTransfer->getErrors() as $errorTransfer) {
$dynamicEntityCollectionResponseTransfer->addError($errorTransfer);
}

return $dynamicEntityCollectionResponseTransfer;
}

/**
* @param \Generated\Shared\Transfer\DynamicEntityCriteriaTransfer $dynamicEntityCriteriaTransfer
*
* @return \Generated\Shared\Transfer\DynamicEntityConfigurationTransfer
*/
protected function getDynamicEntityConfigurationTransfer(DynamicEntityCriteriaTransfer $dynamicEntityCriteriaTransfer): DynamicEntityConfigurationTransfer
{
$tableAlias = $dynamicEntityCriteriaTransfer->getDynamicEntityConditionsOrFail()->getTableAliasOrFail();
$dynamicEntityConfigurationResponse = $this->dynamicEntityReader->getDynamicEntityConfigurationTransferTree((new DynamicEntityCollectionRequestTransfer())->setTableAlias($tableAlias));

return $dynamicEntityConfigurationResponse->getDynamicEntityConfigurationTransfer();
}

/**
* @param string $tableAlias
*
* @return string
*/
protected function buildErrorPath(string $tableAlias): string
{
return sprintf(static::ERROR_PATH_PLACEHOLDER, $tableAlias, static::ERROR_PATH_INDEX);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace Spryker\Zed\DynamicEntity\Business\Deleter;

use Generated\Shared\Transfer\DynamicEntityCollectionDeleteCriteriaTransfer;
use Generated\Shared\Transfer\DynamicEntityCollectionResponseTransfer;

interface DynamicEntityDeleterInterface
{
/**
* @param \Generated\Shared\Transfer\DynamicEntityCollectionDeleteCriteriaTransfer $dynamicEntityCollectionDeleteCriteriaTransfer
*
* @return \Generated\Shared\Transfer\DynamicEntityCollectionResponseTransfer
*/
public function deleteEntityCollection(
DynamicEntityCollectionDeleteCriteriaTransfer $dynamicEntityCollectionDeleteCriteriaTransfer
): DynamicEntityCollectionResponseTransfer;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Spryker\Zed\DynamicEntity\Business\Creator\DynamicEntityConfigurationCreatorInterface;
use Spryker\Zed\DynamicEntity\Business\Creator\DynamicEntityCreator;
use Spryker\Zed\DynamicEntity\Business\Creator\DynamicEntityCreatorInterface;
use Spryker\Zed\DynamicEntity\Business\Deleter\DynamicEntityDeleter;
use Spryker\Zed\DynamicEntity\Business\Deleter\DynamicEntityDeleterInterface;
use Spryker\Zed\DynamicEntity\Business\Expander\DynamicEntityPostEditRequestExpander;
use Spryker\Zed\DynamicEntity\Business\Expander\DynamicEntityPostEditRequestExpanderInterface;
use Spryker\Zed\DynamicEntity\Business\Filter\DynamicEntityFieldCreationFilter;
Expand Down Expand Up @@ -580,6 +582,18 @@ public function createDynamicEntityErrorPathResolver(): DynamicEntityErrorPathRe
return new DynamicEntityErrorPathResolver();
}

/**
* @return \Spryker\Zed\DynamicEntity\Business\Deleter\DynamicEntityDeleterInterface
*/
public function createDynamicEntityDeleter(): DynamicEntityDeleterInterface
{
return new DynamicEntityDeleter(
$this->getEntityManager(),
$this->createDynamicEntityMapper(),
$this->createDynamicEntityReader(),
);
}

/**
* @return \Spryker\Zed\DynamicEntity\Business\Validator\DynamicEntityValidatorInterface
*/
Expand Down
16 changes: 16 additions & 0 deletions src/Spryker/Zed/DynamicEntity/Business/DynamicEntityFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Spryker\Zed\DynamicEntity\Business;

use Generated\Shared\Transfer\DynamicEntityCollectionDeleteCriteriaTransfer;
use Generated\Shared\Transfer\DynamicEntityCollectionRequestTransfer;
use Generated\Shared\Transfer\DynamicEntityCollectionResponseTransfer;
use Generated\Shared\Transfer\DynamicEntityCollectionTransfer;
Expand Down Expand Up @@ -138,4 +139,19 @@ public function updateDynamicEntityConfigurationCollection(
): DynamicEntityConfigurationCollectionResponseTransfer {
return $this->getFactory()->createDynamicEntityConfigurationUpdater()->updateDynamicEntityConfigurationCollection($dynamicEntityConfigurationCollectionTransfer);
}

/**
* {@inheritDoc}
*
* @api
*
* @param \Generated\Shared\Transfer\DynamicEntityCollectionDeleteCriteriaTransfer $dynamicEntityCollectionDeleteCriteriaTransfer
*
* @return \Generated\Shared\Transfer\DynamicEntityCollectionResponseTransfer
*/
public function deleteDynamicEntityCollection(
DynamicEntityCollectionDeleteCriteriaTransfer $dynamicEntityCollectionDeleteCriteriaTransfer
): DynamicEntityCollectionResponseTransfer {
return $this->getFactory()->createDynamicEntityDeleter()->deleteEntityCollection($dynamicEntityCollectionDeleteCriteriaTransfer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Spryker\Zed\DynamicEntity\Business;

use Generated\Shared\Transfer\DynamicEntityCollectionDeleteCriteriaTransfer;
use Generated\Shared\Transfer\DynamicEntityCollectionRequestTransfer;
use Generated\Shared\Transfer\DynamicEntityCollectionResponseTransfer;
use Generated\Shared\Transfer\DynamicEntityCollectionTransfer;
Expand Down Expand Up @@ -129,4 +130,18 @@ public function createDynamicEntityConfigurationCollection(
public function updateDynamicEntityConfigurationCollection(
DynamicEntityConfigurationCollectionRequestTransfer $dynamicEntityConfigurationCollectionTransfer
): DynamicEntityConfigurationCollectionResponseTransfer;

/**
* Specification:
* - Deletes a collection of entities based on the provided criteria.
*
* @api
*
* @param \Generated\Shared\Transfer\DynamicEntityCollectionDeleteCriteriaTransfer $dynamicEntityCollectionDeleteCriteriaTransfer
*
* @return \Generated\Shared\Transfer\DynamicEntityCollectionResponseTransfer
*/
public function deleteDynamicEntityCollection(
DynamicEntityCollectionDeleteCriteriaTransfer $dynamicEntityCollectionDeleteCriteriaTransfer
): DynamicEntityCollectionResponseTransfer;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Spryker\Zed\DynamicEntity\Business\Mapper;

use Generated\Shared\Transfer\DynamicEntityCollectionDeleteCriteriaTransfer;
use Generated\Shared\Transfer\DynamicEntityCollectionRequestTransfer;
use Generated\Shared\Transfer\DynamicEntityCollectionResponseTransfer;
use Generated\Shared\Transfer\DynamicEntityCollectionTransfer;
Expand Down Expand Up @@ -36,6 +37,11 @@ class DynamicEntityMapper implements DynamicEntityMapperInterface
*/
protected const IDENTIFIER = 'identifier';

/**
* @var string
*/
protected const IS_DELETABLE = 'isDeletable';

/**
* @var string
*/
Expand Down Expand Up @@ -101,6 +107,9 @@ protected function mapDynamicEntityDefinitionToDynamicEntityDefinitionTransfer(
}

$dynamicEntityDefinitionTransfer->setIdentifier($definition[static::IDENTIFIER]);
if (array_key_exists(static::IS_DELETABLE, $definition)) {
$dynamicEntityDefinitionTransfer->setIsDeletable($definition[static::IS_DELETABLE]);
}

foreach ($definition[static::FIELDS] as $field) {
$dynamicEntityFieldDefinitionTransfer = (new DynamicEntityFieldDefinitionTransfer())->fromArray($field, true);
Expand Down Expand Up @@ -367,6 +376,19 @@ public function mapDynamicEntityTransferToDynamicEntityConditionsTransfer(
return $dynamicEntityConditionsTransfer;
}

/**
* @param \Generated\Shared\Transfer\DynamicEntityCollectionDeleteCriteriaTransfer $dynamicEntityCollectionDeleteCriteriaTransfer
* @param \Generated\Shared\Transfer\DynamicEntityCriteriaTransfer $dynamicEntityCriteriaTransfer
*
* @return \Generated\Shared\Transfer\DynamicEntityCriteriaTransfer
*/
public function mapDynamicEntityCollectionDeleteCriteriaTransferToDynamicEntityCriteriaTransfer(
DynamicEntityCollectionDeleteCriteriaTransfer $dynamicEntityCollectionDeleteCriteriaTransfer,
DynamicEntityCriteriaTransfer $dynamicEntityCriteriaTransfer
): DynamicEntityCriteriaTransfer {
return $dynamicEntityCriteriaTransfer->fromArray($dynamicEntityCollectionDeleteCriteriaTransfer->toArray(true, true), true);
}

/**
* @param \Generated\Shared\Transfer\DynamicEntityConfigurationTransfer $dynamicEntityConfigurationTransfer
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Spryker\Zed\DynamicEntity\Business\Mapper;

use Generated\Shared\Transfer\DynamicEntityCollectionDeleteCriteriaTransfer;
use Generated\Shared\Transfer\DynamicEntityCollectionRequestTransfer;
use Generated\Shared\Transfer\DynamicEntityCollectionResponseTransfer;
use Generated\Shared\Transfer\DynamicEntityCollectionTransfer;
Expand Down Expand Up @@ -109,4 +110,15 @@ public function mapDynamicEntityTransferToDynamicEntityConditionsTransfer(
DynamicEntityTransfer $dynamicEntityTransfer,
DynamicEntityConfigurationTransfer $dynamicEntityConfigurationTransfer
): ?DynamicEntityConditionsTransfer;

/**
* @param \Generated\Shared\Transfer\DynamicEntityCollectionDeleteCriteriaTransfer $dynamicEntityCollectionDeleteCriteriaTransfer
* @param \Generated\Shared\Transfer\DynamicEntityCriteriaTransfer $dynamicEntityCriteriaTransfer
*
* @return \Generated\Shared\Transfer\DynamicEntityCriteriaTransfer
*/
public function mapDynamicEntityCollectionDeleteCriteriaTransferToDynamicEntityCriteriaTransfer(
DynamicEntityCollectionDeleteCriteriaTransfer $dynamicEntityCollectionDeleteCriteriaTransfer,
DynamicEntityCriteriaTransfer $dynamicEntityCriteriaTransfer
): DynamicEntityCriteriaTransfer;
}
Loading

0 comments on commit 3db92f3

Please sign in to comment.