Skip to content

Commit 9db5c63

Browse files
committed
Fix observers
1 parent c065bbd commit 9db5c63

File tree

11 files changed

+235
-240
lines changed

11 files changed

+235
-240
lines changed

app/code/Magento/MediaContent/etc/di.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
<preference for="Magento\MediaContentApi\Api\Data\ContentIdentityInterface" type="Magento\MediaContent\Model\ContentIdentity"/>
1717
<preference for="Magento\MediaContentApi\Api\Data\ContentAssetLinkInterface" type="Magento\MediaContent\Model\ContentAssetLink"/>
1818
<preference for="Magento\MediaContentApi\Model\SearchPatternConfigInterface" type="Magento\MediaContent\Model\Content\SearchPatternConfig"/>
19-
<preference for="Magento\MediaContent\Model\Content\ConfigInterface" type="Magento\MediaContent\Model\Content\Config"/>
2019
<type name="Magento\MediaGalleryApi\Api\DeleteAssetsByPathsInterface">
2120
<plugin name="remove_media_content_after_asset_is_removed_by_path" type="Magento\MediaContent\Plugin\MediaGalleryAssetDeleteByPath" />
2221
</type>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaContentApi\Model\Composite;
9+
10+
use Magento\MediaContentApi\Api\Data\ContentIdentityInterface;
11+
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
12+
13+
/**
14+
* Get concatenated content for all store views
15+
*/
16+
class GetEntityContents implements GetEntityContentsInterface
17+
{
18+
/**
19+
* @var array|GetEntityContentsInterface[]
20+
*/
21+
private $items;
22+
23+
/**
24+
* GetContent constructor.
25+
* @param GetEntityContentsInterface[] $items
26+
*/
27+
public function __construct(
28+
$items = []
29+
) {
30+
foreach ($items as $item) {
31+
if (!$item instanceof GetEntityContentsInterface) {
32+
throw new \InvalidArgumentException(
33+
__('GetContent items must implement %1.', GetEntityContentsInterface::class)
34+
);
35+
}
36+
}
37+
38+
$this->items = $items;
39+
}
40+
41+
/**
42+
* @param ContentIdentityInterface $contentIdentity
43+
* @return string[]
44+
*/
45+
public function execute(ContentIdentityInterface $contentIdentity): array
46+
{
47+
if (isset($this->items[$contentIdentity->getEntityType()])) {
48+
return $this->items[$contentIdentity->getEntityType()]->execute($contentIdentity);
49+
}
50+
return [];
51+
}
52+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\MediaContentApi\Model;
8+
9+
use Magento\MediaContentApi\Api\Data\ContentIdentityInterface;
10+
11+
/**
12+
* Get Entity Contents.
13+
*/
14+
interface GetEntityContentsInterface
15+
{
16+
/**
17+
* @param ContentIdentityInterface $contentIdentity
18+
* @return string[]
19+
*/
20+
public function execute(ContentIdentityInterface $contentIdentity): array;
21+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<preference for="Magento\MediaContentApi\Model\GetEntityContentsInterface" type="Magento\MediaContentApi\Model\Composite\GetEntityContents"/>
10+
</config>

app/code/Magento/MediaContentCatalog/Model/ResourceModel/GetCategoryContent.php

Lines changed: 0 additions & 84 deletions
This file was deleted.

app/code/Magento/MediaContentCatalog/Model/ResourceModel/GetProductContent.php renamed to app/code/Magento/MediaContentCatalog/Model/ResourceModel/GetEntityContent.php

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@
77

88
namespace Magento\MediaContentCatalog\Model\ResourceModel;
99

10-
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
1110
use Magento\Catalog\Model\ResourceModel\Product;
1211
use Magento\Framework\App\ResourceConnection;
12+
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
13+
use Magento\MediaContentApi\Api\Data\ContentIdentityInterface;
14+
use Magento\Eav\Model\Config;
1315

1416
/**
1517
* Get concatenated content for all store views
1618
*/
17-
class GetProductContent
19+
class GetEntityContent implements GetEntityContentsInterface
1820
{
21+
/**
22+
* @var Config
23+
*/
24+
private $config;
25+
1926
/**
2027
* @var Product
2128
*/
@@ -27,58 +34,43 @@ class GetProductContent
2734
private $resourceConnection;
2835

2936
/**
37+
* @param Config $config
3038
* @param ResourceConnection $resourceConnection
3139
* @param Product $productResource
3240
*/
3341
public function __construct(
42+
Config $config,
3443
ResourceConnection $resourceConnection,
3544
Product $productResource
3645
) {
46+
$this->config = $config;
3747
$this->productResource = $productResource;
3848
$this->resourceConnection = $resourceConnection;
3949
}
4050

4151
/**
42-
* Get concatenated product content for all store views
52+
* Get product content for all store views
4353
*
44-
* @param int $entityId
45-
* @param AbstractAttribute $attribute
46-
* @return string
47-
*/
48-
public function execute(int $entityId, AbstractAttribute $attribute): string
49-
{
50-
return implode(
51-
PHP_EOL,
52-
$this->getDistinctContent(
53-
$entityId,
54-
$attribute
55-
)
56-
);
57-
}
58-
59-
/**
60-
* Load values of an product attribute for all store views
61-
*
62-
* @param int $entityId
63-
* @param AbstractAttribute $attribute
54+
* @param ContentIdentityInterface $contentIdentity
6455
* @return array
56+
* @throws \Magento\Framework\Exception\LocalizedException
6557
*/
66-
private function getDistinctContent(int $entityId, AbstractAttribute $attribute): array
58+
public function execute(ContentIdentityInterface $contentIdentity): array
6759
{
60+
$attribute = $this->config->getAttribute($contentIdentity->getEntityType(), $contentIdentity->getField());
6861
$connection = $this->resourceConnection->getConnection();
62+
6963
$select = $connection->select()->from(
7064
['abt' => $attribute->getBackendTable()],
7165
'abt.value'
72-
)->joinInner(
73-
['rt' => $this->productResource->getEntityTable()],
74-
'rt.' . $attribute->getEntityIdField() . ' = abt.' . $attribute->getEntityIdField()
75-
)->where(
76-
'rt.entity_id = ?',
77-
$entityId
7866
)->where(
7967
$connection->quoteIdentifier('abt.attribute_id') . ' = ?',
8068
(int) $attribute->getAttributeId()
81-
);
69+
)->where(
70+
$connection->quoteIdentifier('abt.' . $attribute->getEntityIdField()) . ' = ?',
71+
$contentIdentity->getEntityId()
72+
)->distinct(true);
73+
8274
return $connection->fetchCol($select);
8375
}
8476
}

app/code/Magento/MediaContentCatalog/Observer/Category.php

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
use Magento\Framework\Event\ObserverInterface;
1313
use Magento\MediaContentApi\Api\UpdateContentAssetLinksInterface;
1414
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
15-
use Magento\MediaContentCatalog\Model\ResourceModel\GetCategoryContent;
16-
use Magento\Eav\Model\Config;
15+
use Magento\MediaContentApi\Model\GetEntityContentsInterface;
1716

1817
/**
1918
* Observe the catalog_category_save_after event and run processing relation between category content and media asset.
@@ -41,37 +40,28 @@ class Category implements ObserverInterface
4140
private $contentIdentityFactory;
4241

4342
/**
44-
* @var GetCategoryContent
43+
* @var GetEntityContentsInterface
4544
*/
4645
private $getContent;
4746

48-
/**
49-
*
50-
* @var Config
51-
*/
52-
private $config;
53-
5447
/**
5548
* Create links for category content
5649
*
5750
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
58-
* @param GetCategoryContent $getContent
51+
* @param GetEntityContentsInterface $getContent
5952
* @param UpdateContentAssetLinksInterface $updateContentAssetLinks
60-
* @param Config $config
6153
* @param array $fields
6254
*/
6355
public function __construct(
6456
ContentIdentityInterfaceFactory $contentIdentityFactory,
65-
GetCategoryContent $getContent,
57+
GetEntityContentsInterface $getContent,
6658
UpdateContentAssetLinksInterface $updateContentAssetLinks,
67-
Config $config,
6859
array $fields
6960
) {
7061
$this->contentIdentityFactory = $contentIdentityFactory;
7162
$this->getContent = $getContent;
7263
$this->updateContentAssetLinks = $updateContentAssetLinks;
7364
$this->fields = $fields;
74-
$this->config = $config;
7565
}
7666

7767
/**
@@ -89,17 +79,15 @@ public function execute(Observer $observer): void
8979
if (!$model->dataHasChangedFor($field)) {
9080
continue;
9181
}
92-
$attribute = $this->config->getAttribute(self::CONTENT_TYPE, $field);
93-
$this->updateContentAssetLinks->execute(
94-
$this->contentIdentityFactory->create(
95-
[
96-
self::TYPE => self::CONTENT_TYPE,
97-
self::FIELD => $field,
98-
self::ENTITY_ID => (string) $model->getEntityId(),
99-
]
100-
),
101-
$this->getContent->execute((int) $model->getEntityId(), $attribute)
82+
$contentIdentity = $this->contentIdentityFactory->create(
83+
[
84+
self::TYPE => self::CONTENT_TYPE,
85+
self::FIELD => $field,
86+
self::ENTITY_ID => (string) $model->getEntityId(),
87+
]
10288
);
89+
$concatenatedContent = implode(PHP_EOL, $this->getContent->execute($contentIdentity));
90+
$this->updateContentAssetLinks->execute($contentIdentity, $concatenatedContent);
10391
}
10492
}
10593
}

0 commit comments

Comments
 (0)