Skip to content

Commit

Permalink
[Data Objects]: Extend Patcher to use field adapters for editable data (
Browse files Browse the repository at this point in the history
#647)

* extend patcher to use adapters for editable data

* update controller default responses
  • Loading branch information
lukmzig authored Dec 19, 2024
1 parent 8d249ee commit 05814a2
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/Asset/Controller/PatchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ public function __construct(
content: new IdJson('ID of created jobRun', 'jobRunId')
)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
HttpResponseCodes::BAD_REQUEST,
HttpResponseCodes::INTERNAL_SERVER_ERROR,
HttpResponseCodes::NOT_FOUND,
HttpResponseCodes::UNAUTHORIZED,
])]
public function assetPatchById(#[MapRequestPayload] PatchAssetParameter $patchAssetParameter): Response
{
Expand Down
4 changes: 3 additions & 1 deletion src/Asset/Controller/PatchFolderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ public function __construct(
content: new IdJson('ID of created jobRun', 'jobRunId')
)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
HttpResponseCodes::BAD_REQUEST,
HttpResponseCodes::INTERNAL_SERVER_ERROR,
HttpResponseCodes::NOT_FOUND,
HttpResponseCodes::UNAUTHORIZED,
])]
public function assetPatchFolderById(#[MapRequestPayload] PatchFolderParameter $patchFolderParameter): Response
{
Expand Down
8 changes: 7 additions & 1 deletion src/Asset/Controller/UpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use Pimcore\Bundle\StudioBackendBundle\Asset\MappedParameter\UpdateAssetParameter;
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\AssetServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementSavingFailedException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\DefaultResponses;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\SuccessResponse;
Expand Down Expand Up @@ -49,6 +51,9 @@ public function __construct(
parent::__construct($serializer);
}

/**
* @throws ElementSavingFailedException|NotFoundException
*/
#[Route('/assets/{id}', name: 'pimcore_studio_api_update_asset', methods: ['PUT'])]
#[IsGranted(UserPermissions::ASSETS->value)]
#[Put(
Expand All @@ -65,8 +70,9 @@ public function __construct(
content: new OneOfAssetJson()
)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
HttpResponseCodes::INTERNAL_SERVER_ERROR,
HttpResponseCodes::NOT_FOUND,
HttpResponseCodes::UNAUTHORIZED,
])]
public function assetUpdateById(int $id, #[MapRequestPayload] UpdateAssetParameter $updateAsset): JsonResponse
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use OpenApi\Attributes\RequestBody;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Property\UpdateBooleanProperty;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Property\UpdateIntegerProperty;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Property\UpdateObjectProperty;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Property\UpdateStringProperty;

/**
Expand Down Expand Up @@ -57,6 +58,7 @@ public function __construct()
new UpdateStringProperty('childrenSortBy'),
new UpdateStringProperty('childrenSortOrder'),
new UpdateBooleanProperty('published'),
new UpdateObjectProperty('editableData'),
],
type: 'object',
),
Expand Down
4 changes: 3 additions & 1 deletion src/DataObject/Controller/PatchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ public function __construct(
content: new IdJson('ID of created jobRun', 'jobRunId')
)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
HttpResponseCodes::BAD_REQUEST,
HttpResponseCodes::INTERNAL_SERVER_ERROR,
HttpResponseCodes::NOT_FOUND,
HttpResponseCodes::UNAUTHORIZED,
])]
public function dataObjectPatchById(#[MapRequestPayload] DataParameter $parameter): Response
{
Expand Down
8 changes: 7 additions & 1 deletion src/DataObject/Controller/UpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use Pimcore\Bundle\StudioBackendBundle\DataObject\Attribute\Response\Content\OneOfDataObjectsJson;
use Pimcore\Bundle\StudioBackendBundle\DataObject\MappedParameter\DataParameter;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataObjectServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementSavingFailedException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\DefaultResponses;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\SuccessResponse;
Expand Down Expand Up @@ -49,6 +51,9 @@ public function __construct(
parent::__construct($serializer);
}

/**
* @throws ElementSavingFailedException|NotFoundException
*/
#[Route('/data-objects/{id}', name: 'pimcore_studio_api_update_data_object', methods: ['PUT'])]
#[IsGranted(UserPermissions::DATA_OBJECTS->value)]
#[Put(
Expand All @@ -65,8 +70,9 @@ public function __construct(
content: new OneOfDataObjectsJson()
)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
HttpResponseCodes::INTERNAL_SERVER_ERROR,
HttpResponseCodes::NOT_FOUND,
HttpResponseCodes::UNAUTHORIZED,
])]
public function dataObjectUpdateById(int $id, #[MapRequestPayload] DataParameter $parameter): JsonResponse
{
Expand Down
18 changes: 15 additions & 3 deletions src/Patcher/Service/PatchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Config;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Jobs;
use Pimcore\Bundle\StudioBackendBundle\Updater\Service\UpdateServiceInterface;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\Element\ElementDescriptor;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\UserInterface;
Expand All @@ -43,10 +45,11 @@
final readonly class PatchService implements PatchServiceInterface
{
public function __construct(
private SynchronousProcessingServiceInterface $synchronousProcessingService,
private JobExecutionAgentInterface $jobExecutionAgent,
private AdapterLoaderInterface $adapterLoader,
private ElementServiceInterface $elementService,
private AdapterLoaderInterface $adapterLoader
private JobExecutionAgentInterface $jobExecutionAgent,
private SynchronousProcessingServiceInterface $synchronousProcessingService,
private UpdateServiceInterface $updateService,
) {
}

Expand Down Expand Up @@ -112,6 +115,15 @@ public function patchElement(
UserInterface $user,
): void {
try {
if (isset($elementPatchData[UpdateServiceInterface::EDITABLE_DATA_KEY]) && $element instanceof Concrete) {
$this->updateService->updateEditableData(
$element,
$elementPatchData[UpdateServiceInterface::EDITABLE_DATA_KEY]
);

unset($elementPatchData[UpdateServiceInterface::EDITABLE_DATA_KEY]);
}

$adapters = $this->adapterLoader->loadAdapters($elementType);
foreach ($adapters as $adapter) {
$adapter->patch($element, $elementPatchData);
Expand Down
4 changes: 1 addition & 3 deletions src/Updater/Service/UpdateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
use ElementProviderTrait;
use ValidateFieldTypeTrait;

private const EDITABLE_DATA_KEY = 'editableData';

public function __construct(
private AdapterLoaderInterface $adapterLoader,
private DataAdapterServiceInterface $dataAdapterService,
Expand Down Expand Up @@ -74,7 +72,7 @@ public function update(string $elementType, int $id, array $data): void
/**
* @throws ElementSavingFailedException
*/
private function updateEditableData(Concrete $element, array $editableData): void
public function updateEditableData(Concrete $element, array $editableData): void
{
try {
$class = $element->getClass();
Expand Down
8 changes: 8 additions & 0 deletions src/Updater/Service/UpdateServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@

use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementSavingFailedException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Model\DataObject\Concrete;

/**
* @internal
*/
interface UpdateServiceInterface
{
public const EDITABLE_DATA_KEY = 'editableData';

/**
* @throws ElementSavingFailedException|NotFoundException
*/
public function update(string $elementType, int $id, array $data): void;

/**
* @throws ElementSavingFailedException
*/
public function updateEditableData(Concrete $element, array $editableData): void;
}

0 comments on commit 05814a2

Please sign in to comment.