Skip to content

Commit 3a1a3c7

Browse files
author
Gabriel Galvao da Gama
committed
Improved attribute fetching
1 parent dfc733a commit 3a1a3c7

File tree

3 files changed

+16
-55
lines changed

3 files changed

+16
-55
lines changed

app/code/Magento/MediaContent/Model/GetAssetIdByContentStatus.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function execute(string $value): array
9090
[]
9191
)->where(
9292
'content_table.' . $this->statusColumn . ' = ?',
93-
$this->getValueFromMap($value)
93+
$value
9494
);
9595

9696
$result = $this->connection->getConnection()->fetchAll($sql);
@@ -99,16 +99,4 @@ public function execute(string $value): array
9999
return $item['asset_id'];
100100
}, $result);
101101
}
102-
103-
/**
104-
* @param string $value
105-
* @return string
106-
*/
107-
private function getValueFromMap(string $value): string
108-
{
109-
if (count($this->valueMap) > 0 && array_key_exists($value, $this->valueMap)) {
110-
return $this->valueMap[$value];
111-
}
112-
return $value;
113-
}
114102
}

app/code/Magento/MediaContent/Model/GetAssetIdByEavContentStatus.php

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magento\MediaContent\Model;
99

10+
use Magento\Eav\Api\Data\AttributeInterface;
11+
use Magento\Eav\Model\Config;
1012
use Magento\Framework\App\ResourceConnection;
1113
use Magento\MediaContentApi\Model\GetAssetIdByContentStatusInterface;
1214

@@ -16,17 +18,16 @@
1618
class GetAssetIdByEavContentStatus implements GetAssetIdByContentStatusInterface
1719
{
1820
private const TABLE_CONTENT_ASSET = 'media_content_asset';
19-
private const TABLE_EAV_ATTRIBUTE = 'eav_attribute';
2021

2122
/**
2223
* @var ResourceConnection
2324
*/
2425
private $connection;
2526

2627
/**
27-
* @var string
28+
* @var Config
2829
*/
29-
private $entityEavTypeTable;
30+
private $config;
3031

3132
/**
3233
* @var string
@@ -38,58 +39,53 @@ class GetAssetIdByEavContentStatus implements GetAssetIdByContentStatusInterface
3839
*/
3940
private $entityType;
4041

41-
/**
42-
* @var int
43-
*/
44-
private $entityTypeId;
45-
4642
/**
4743
* @var array
4844
*/
4945
private $valueMap;
5046

5147
/**
52-
* GetEavContentIdByStatus constructor.
48+
* GetAssetIdByEavContentStatus constructor.
5349
* @param ResourceConnection $resource
54-
* @param string $entityEavTypeTable
50+
* @param Config $config
5551
* @param string $attributeCode
5652
* @param string $entityType
57-
* @param int $entityTypeId
5853
* @param array $valueMap
5954
*/
6055
public function __construct(
6156
ResourceConnection $resource,
62-
string $entityEavTypeTable,
57+
Config $config,
6358
string $attributeCode,
6459
string $entityType,
65-
int $entityTypeId,
6660
array $valueMap = []
6761
) {
6862
$this->connection = $resource;
69-
$this->entityEavTypeTable = $entityEavTypeTable;
63+
$this->config = $config;
7064
$this->attributeCode = $attributeCode;
7165
$this->entityType = $entityType;
72-
$this->entityTypeId = $entityTypeId;
7366
$this->valueMap = $valueMap;
7467
}
7568

7669
/**
7770
* @param string $value
7871
* @return array
72+
* @throws \Magento\Framework\Exception\LocalizedException
7973
*/
8074
public function execute(string $value): array
8175
{
82-
$statusAttributeId = $this->getAttributeId();
76+
$statusAttribute = $this->config->getAttribute($this->entityType, $this->attributeCode);
77+
8378
$sql = $this->connection->getConnection()->select()->from(
8479
['asset_content_table' => $this->connection->getTableName(self::TABLE_CONTENT_ASSET)],
8580
['asset_id']
8681
)->where(
8782
'entity_type = ?',
8883
$this->entityType
8984
)->joinInner(
90-
['entity_eav_type' => $this->connection->getTableName($this->entityEavTypeTable)],
91-
'asset_content_table.entity_id = entity_eav_type.entity_id AND entity_eav_type.attribute_id = ' .
92-
$statusAttributeId,
85+
['entity_eav_type' => $statusAttribute->getBackendTable()],
86+
'asset_content_table.entity_id = entity_eav_type.' . $statusAttribute->getEntityIdField() .
87+
' AND entity_eav_type.attribute_id = ' .
88+
$statusAttribute->getAttributeId(),
9389
[]
9490
)->where(
9591
'entity_eav_type.value = ?',
@@ -103,25 +99,6 @@ public function execute(string $value): array
10399
}, $result);
104100
}
105101

106-
/**
107-
* @return string
108-
*/
109-
private function getAttributeId(): string
110-
{
111-
$sql = $this->connection->getConnection()->select()->from(
112-
['eav' => $this->connection->getTableName(self::TABLE_EAV_ATTRIBUTE)],
113-
['attribute_id']
114-
)->where(
115-
'entity_type_id = ?',
116-
$this->entityTypeId
117-
)->where(
118-
'attribute_code = ?',
119-
$this->attributeCode
120-
);
121-
122-
return $this->connection->getConnection()->fetchOne($sql);
123-
}
124-
125102
/**
126103
* @param string $value
127104
* @return string

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
</type>
1919
<virtualType name="Magento\MediaContent\Model\GetAssetIdByProductStatus" type="Magento\MediaContent\Model\GetAssetIdByEavContentStatus">
2020
<arguments>
21-
<argument name="entityEavTypeTable" xsi:type="string">catalog_product_entity_int</argument>
2221
<argument name="attributeCode" xsi:type="string">status</argument>
2322
<argument name="entityType" xsi:type="string">catalog_product</argument>
24-
<argument name="entityTypeId" xsi:type="string">4</argument>
2523
<argument name="valueMap" xsi:type="array">
2624
<item name="1" xsi:type="string">1</item>
2725
<item name="0" xsi:type="string">2</item>
@@ -30,10 +28,8 @@
3028
</virtualType>
3129
<virtualType name="Magento\MediaContent\Model\GetAssetIdByCategoryStatus" type="Magento\MediaContent\Model\GetAssetIdByEavContentStatus">
3230
<arguments>
33-
<argument name="entityEavTypeTable" xsi:type="string">catalog_category_entity_int</argument>
3431
<argument name="attributeCode" xsi:type="string">is_active</argument>
3532
<argument name="entityType" xsi:type="string">catalog_category</argument>
36-
<argument name="entityTypeId" xsi:type="string">3</argument>
3733
</arguments>
3834
</virtualType>
3935
<virtualType name="Magento\MediaContent\Model\GetAssetIdByPageStatus" type="Magento\MediaContent\Model\GetAssetIdByContentStatus">

0 commit comments

Comments
 (0)