Skip to content

Commit a5dc587

Browse files
authored
Merge pull request #108 from dachcom-digital/bugfix/document-deletion
fix document deletion
2 parents e679ccc + 2db7cb2 commit a5dc587

File tree

5 files changed

+100
-12
lines changed

5 files changed

+100
-12
lines changed

UPGRADE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Upgrade Notes
22

3+
## 4.0.9
4+
- [BUGFIX] fixed document deletion [see more](https://github.com/dachcom-digital/pimcore-dynamic-search/pull/108)
35
## 4.0.8
46
- [IMPROVEMENT] `dynamic_search.element_listener_options.allowed_document_types` added, read more about it [here](./docs/01_DispatchWorkflow.md#listener)
57
## 4.0.7

src/Queue/DataCollector.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
use DynamicSearchBundle\Context\ContextDefinitionInterface;
1818
use DynamicSearchBundle\Logger\LoggerInterface;
1919
use DynamicSearchBundle\Queue\Message\QueueResourceMessage;
20+
use DynamicSearchBundle\Resource\ResourceInfo;
21+
use DynamicSearchBundle\Resource\ResourceInfoInterface;
2022
use DynamicSearchBundle\Service\LockServiceInterface;
2123
use DynamicSearchBundle\Validator\ResourceValidatorInterface;
24+
use Pimcore\Model\Document;
2225
use Pimcore\Model\Element;
2326
use Pimcore\Model\Element\ElementInterface;
2427
use Symfony\Component\Messenger\MessageBusInterface;
@@ -108,10 +111,19 @@ public function addToContextQueue(string $contextName, string $dispatchType, mix
108111

109112
protected function generateJob(string $contextName, string $dispatchType, mixed $resource, array $options): void
110113
{
111-
// @todo: introduce generic "resource info" dto with resource information
114+
// todo: create resource info factory
112115
if ($resource instanceof ElementInterface) {
113-
$resourceType = sprintf('%s-%s', Element\Service::getElementType($resource), $resource->getId());
114-
$resource = null;
116+
$resourceInfo = new ResourceInfo(
117+
$resource->getId(),
118+
Element\Service::getElementType($resource)
119+
);
120+
121+
if ($resource instanceof Document && null !== $locale = $resource->getProperty('language')) {
122+
$resourceInfo->setResourceLocale($locale);
123+
}
124+
125+
$resourceType = ResourceInfoInterface::TYPE_PIMCORE_ELEMENT;
126+
$resource = $resourceInfo;
115127
} elseif (is_object($resource)) {
116128
$resourceType = get_class($resource);
117129
} else {

src/Queue/MessageHandler/QueuedResourcesHandler.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use DynamicSearchBundle\Processor\Harmonizer\ResourceHarmonizerInterface;
2121
use DynamicSearchBundle\Queue\Message\ProcessResourceMessage;
2222
use DynamicSearchBundle\Queue\Message\QueueResourceMessage;
23+
use DynamicSearchBundle\Resource\ResourceInfoInterface;
2324
use Pimcore\Model\Asset;
2425
use Pimcore\Model\DataObject;
2526
use Pimcore\Model\Document;
@@ -55,23 +56,39 @@ private function process(array $jobs): void
5556
foreach ($jobs as [$message, $ack]) {
5657
try {
5758
$resource = $message->resource;
58-
// @todo: use introduced "resource info" dto to determinate resource / type
59-
if (str_contains($message->resourceType, '-')) {
60-
[$type, $id] = explode('-', $message->resourceType);
61-
if (is_numeric($id)) {
62-
$id = (int) $id;
59+
60+
if ($message->resourceType === ResourceInfoInterface::TYPE_PIMCORE_ELEMENT) {
61+
$resourceInfo = $resource;
62+
if (!$resourceInfo instanceof ResourceInfoInterface) {
63+
$this->logger->error(
64+
'Unable to get resource info for pimcore resource.',
65+
'queue',
66+
$message->contextName
67+
);
68+
69+
$ack->ack($message);
70+
71+
continue;
6372
}
64-
$resource = Element\Service::getElementById($type, $id);
73+
74+
$resource = Element\Service::getElementById($resourceInfo->getResourceType(), $resourceInfo->getResourceId());
6575
if ($resource === null && $message->dispatchType === ContextDefinitionInterface::CONTEXT_DISPATCH_TYPE_DELETE) {
6676
// at this time, the resource is already deleted by pimcore
6777
// since we do not serialize the resource into the message,
68-
// we need to create a dummy resource to retrieve a valid resource meta for deletion
69-
$resource = match ($type) {
78+
// we need to create a fake resource in order for the resource normalizer
79+
// to generate a valid resource meta for deletion
80+
$resource = match ($resourceInfo->getResourceType()) {
7081
'document' => new Document(),
7182
'asset' => new Asset(),
7283
'object' => new DataObject\Concrete(),
84+
default => null,
7385
};
74-
$resource->setId($id);
86+
87+
$resource?->setId($resourceInfo->getResourceId());
88+
89+
if ($resource instanceof Document && null !== $locale = $resourceInfo->getResourceLocale()) {
90+
$resource->setProperty('language', 'text', $locale, false, true);
91+
}
7592
}
7693
}
7794

src/Resource/ResourceInfo.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace DynamicSearchBundle\Resource;
4+
5+
class ResourceInfo implements ResourceInfoInterface
6+
{
7+
public function __construct(
8+
protected int|string $resourceId,
9+
protected string $resourceType,
10+
protected ?string $resourceLocale = null
11+
)
12+
{
13+
}
14+
15+
public function getResourceType(): string
16+
{
17+
return $this->resourceType;
18+
}
19+
20+
public function getResourceId(): int|string
21+
{
22+
return $this->resourceId;
23+
}
24+
25+
public function getResourceLocale(): string
26+
{
27+
return $this->resourceLocale;
28+
}
29+
30+
public function setResourceType(string $resourceType): void
31+
{
32+
$this->resourceType = $resourceType;
33+
}
34+
35+
public function setResourceId(int|string $resourceId): void
36+
{
37+
$this->resourceId = $resourceId;
38+
}
39+
40+
public function setResourceLocale(string $resourceLocale): void
41+
{
42+
$this->resourceLocale = $resourceLocale;
43+
}
44+
45+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace DynamicSearchBundle\Resource;
4+
5+
interface ResourceInfoInterface
6+
{
7+
public const TYPE_PIMCORE_ELEMENT = 'pimcore_element';
8+
9+
public function getResourceId(): int|string;
10+
public function getResourceType(): string;
11+
public function getResourceLocale(): ?string;
12+
}

0 commit comments

Comments
 (0)