Skip to content

Commit 8378eff

Browse files
committed
Move retrieval of custom attributes to dedicated services
1 parent 98a9c36 commit 8378eff

File tree

10 files changed

+134
-75
lines changed

10 files changed

+134
-75
lines changed

app/code/Magento/Catalog/Model/Category.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77

88
use Magento\Catalog\Api\CategoryRepositoryInterface;
99
use Magento\Catalog\Api\Data\CategoryInterface;
10+
use Magento\Catalog\Model\Entity\GetCategoryCustomAttributeCodes;
1011
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
12+
use Magento\Eav\Model\Entity\GetCustomAttributeCodesInterface;
1113
use Magento\Framework\Api\AttributeValueFactory;
14+
use Magento\Framework\App\ObjectManager;
1215
use Magento\Framework\Convert\ConvertArray;
1316
use Magento\Framework\Exception\NoSuchEntityException;
1417
use Magento\Framework\Profiler;
@@ -205,11 +208,15 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements
205208
protected $categoryRepository;
206209

207210
/**
208-
* @deprecated not used anymore, related functionality has been moved to resource model
209211
* @var \Magento\Framework\Api\MetadataServiceInterface
210212
*/
211213
protected $metadataService;
212214

215+
/**
216+
* @var GetCustomAttributeCodesInterface
217+
*/
218+
private $getCustomAttributeCodes;
219+
213220
/**
214221
* @param \Magento\Framework\Model\Context $context
215222
* @param \Magento\Framework\Registry $registry
@@ -255,7 +262,8 @@ public function __construct(
255262
CategoryRepositoryInterface $categoryRepository,
256263
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
257264
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
258-
array $data = []
265+
array $data = [],
266+
GetCustomAttributeCodesInterface $getCustomAttributeCodes = null
259267
) {
260268
$this->metadataService = $metadataService;
261269
$this->_treeModel = $categoryTreeResource;
@@ -270,6 +278,9 @@ public function __construct(
270278
$this->urlFinder = $urlFinder;
271279
$this->indexerRegistry = $indexerRegistry;
272280
$this->categoryRepository = $categoryRepository;
281+
$this->getCustomAttributeCodes = $getCustomAttributeCodes ?? ObjectManager::getInstance()->get(
282+
GetCategoryCustomAttributeCodes::class
283+
);
273284
parent::__construct(
274285
$context,
275286
$registry,
@@ -303,7 +314,7 @@ protected function _construct()
303314
*/
304315
protected function getCustomAttributesCodes()
305316
{
306-
return $this->_getResource()->getCustomAttributesCodes();
317+
return $this->getCustomAttributeCodes->execute($this->metadataService);
307318
}
308319

309320
/**
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Model\Entity;
8+
9+
use Magento\Catalog\Api\Data\CategoryInterface;
10+
use Magento\Eav\Model\Entity\GetCustomAttributeCodesInterface;
11+
use Magento\Framework\Api\MetadataServiceInterface;
12+
13+
class GetCategoryCustomAttributeCodes implements GetCustomAttributeCodesInterface
14+
{
15+
/**
16+
* @var GetCustomAttributeCodesInterface
17+
*/
18+
private $baseCustomAttributeCodes;
19+
20+
/**
21+
* @param GetCustomAttributeCodesInterface $baseCustomAttributeCodes
22+
*/
23+
public function __construct(
24+
GetCustomAttributeCodesInterface $baseCustomAttributeCodes
25+
) {
26+
$this->baseCustomAttributeCodes = $baseCustomAttributeCodes;
27+
}
28+
29+
30+
public function execute(MetadataServiceInterface $metadataService): array
31+
{
32+
$customAttributesCodes = $this->baseCustomAttributeCodes->execute($metadataService);
33+
return array_diff($customAttributesCodes, CategoryInterface::ATTRIBUTES);
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Model\Entity;
8+
9+
use Magento\Catalog\Api\Data\ProductInterface;
10+
use Magento\Eav\Model\Entity\GetCustomAttributeCodesInterface;
11+
use Magento\Framework\Api\MetadataServiceInterface;
12+
13+
class GetProductCustomAttributeCodes implements GetCustomAttributeCodesInterface
14+
{
15+
/**
16+
* @var GetCustomAttributeCodesInterface
17+
*/
18+
private $baseCustomAttributeCodes;
19+
20+
/**
21+
* @param GetCustomAttributeCodesInterface $baseCustomAttributeCodes
22+
*/
23+
public function __construct(
24+
GetCustomAttributeCodesInterface $baseCustomAttributeCodes
25+
) {
26+
$this->baseCustomAttributeCodes = $baseCustomAttributeCodes;
27+
}
28+
29+
30+
public function execute(MetadataServiceInterface $metadataService): array
31+
{
32+
$customAttributesCodes = $this->baseCustomAttributeCodes->execute($metadataService);
33+
return array_diff($customAttributesCodes, ProductInterface::ATTRIBUTES);
34+
}
35+
}

app/code/Magento/Catalog/Model/Product.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
use Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface;
1010
use Magento\Catalog\Api\Data\ProductInterface;
1111
use Magento\Catalog\Api\ProductLinkRepositoryInterface;
12+
use Magento\Catalog\Model\Entity\GetProductCustomAttributeCodes;
1213
use Magento\Catalog\Model\Product\Attribute\Backend\Media\EntryConverterPool;
14+
use Magento\Eav\Model\Entity\GetCustomAttributeCodesInterface;
1315
use Magento\Framework\Api\AttributeValueFactory;
1416
use Magento\Framework\App\Filesystem\DirectoryList;
17+
use Magento\Framework\App\ObjectManager;
1518
use Magento\Framework\DataObject\IdentityInterface;
1619
use Magento\Framework\Pricing\SaleableInterface;
1720

@@ -274,7 +277,6 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements
274277
protected $imageCacheFactory;
275278

276279
/**
277-
* @deprecated not used anymore, related functionality has been moved to resource model
278280
* @var \Magento\Catalog\Api\ProductAttributeRepositoryInterface
279281
*/
280282
protected $metadataService;
@@ -342,6 +344,11 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements
342344
*/
343345
protected $linkTypeProvider;
344346

347+
/**
348+
* @var GetCustomAttributeCodesInterface
349+
*/
350+
private $getCustomAttributeCodes;
351+
345352
/**
346353
* Product constructor.
347354
* @param \Magento\Framework\Model\Context $context
@@ -418,7 +425,8 @@ public function __construct(
418425
EntryConverterPool $mediaGalleryEntryConverterPool,
419426
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
420427
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor,
421-
array $data = []
428+
array $data = [],
429+
GetCustomAttributeCodesInterface $getCustomAttributeCodes = null
422430
) {
423431
$this->metadataService = $metadataService;
424432
$this->_itemOptionFactory = $itemOptionFactory;
@@ -447,6 +455,9 @@ public function __construct(
447455
$this->mediaGalleryEntryConverterPool = $mediaGalleryEntryConverterPool;
448456
$this->dataObjectHelper = $dataObjectHelper;
449457
$this->joinProcessor = $joinProcessor;
458+
$this->getCustomAttributeCodes = $getCustomAttributeCodes ?? ObjectManager::getInstance()->get(
459+
GetProductCustomAttributeCodes::class
460+
);
450461
parent::__construct(
451462
$context,
452463
$registry,
@@ -474,7 +485,7 @@ protected function _construct()
474485
*/
475486
protected function getCustomAttributesCodes()
476487
{
477-
return $this->_resource->getCustomAttributesCodes();
488+
return $this->getCustomAttributeCodes->execute($this->metadataService);
478489
}
479490

480491
/**

app/code/Magento/Catalog/Model/ResourceModel/Category.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,6 @@ class Category extends AbstractResource
8585
*/
8686
protected $aggregateCount;
8787

88-
/**
89-
* @var CategoryAttributeRepositoryInterface
90-
*/
91-
private $metadataService;
92-
93-
/**
94-
* @var string[]
95-
*/
96-
private $customAttributesCodes;
97-
9888
/**
9989
* Category constructor.
10090
* @param \Magento\Eav\Model\Entity\Context $context
@@ -115,8 +105,7 @@ public function __construct(
115105
\Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryTreeFactory,
116106
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
117107
$data = [],
118-
\Magento\Framework\Serialize\Serializer\Json $serializer = null,
119-
CategoryAttributeRepositoryInterface $metaDataService = null
108+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
120109
) {
121110
parent::__construct(
122111
$context,
@@ -130,19 +119,6 @@ public function __construct(
130119
$this->connectionName = 'catalog';
131120
$this->serializer = $serializer ?: ObjectManager::getInstance()
132121
->get(\Magento\Framework\Serialize\Serializer\Json::class);
133-
$this->metadataService = $metaDataService ?? ObjectManager::getInstance()
134-
->get(CategoryAttributeRepositoryInterface::class);
135-
}
136-
137-
public function getCustomAttributesCodes()
138-
{
139-
if ($this->customAttributesCodes === null) {
140-
$this->customAttributesCodes = $this->getEavAttributesCodes($this->metadataService);
141-
$this->customAttributesCodes = array_values(
142-
array_diff($this->customAttributesCodes, CategoryInterface::ATTRIBUTES)
143-
);
144-
}
145-
return $this->customAttributesCodes;
146122
}
147123

148124
/**

app/code/Magento/Catalog/Model/ResourceModel/Product.php

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,6 @@ class Product extends AbstractResource
8585
*/
8686
private $productCategoryLink;
8787

88-
/**
89-
* @var ProductAttributeRepositoryInterface
90-
*/
91-
private $metadataService;
92-
93-
/**
94-
* @var string[]
95-
*/
96-
private $customAttributesCodes;
97-
9888
/**
9989
* @param \Magento\Eav\Model\Entity\Context $context
10090
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
@@ -106,7 +96,6 @@ class Product extends AbstractResource
10696
* @param \Magento\Eav\Model\Entity\TypeFactory $typeFactory
10797
* @param \Magento\Catalog\Model\Product\Attribute\DefaultAttributes $defaultAttributes
10898
* @param array $data
109-
* @param ProductAttributeRepositoryInterface|null $metadataService
11099
*
111100
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
112101
*/
@@ -120,18 +109,14 @@ public function __construct(
120109
\Magento\Eav\Model\Entity\Attribute\SetFactory $setFactory,
121110
\Magento\Eav\Model\Entity\TypeFactory $typeFactory,
122111
\Magento\Catalog\Model\Product\Attribute\DefaultAttributes $defaultAttributes,
123-
$data = [],
124-
ProductAttributeRepositoryInterface $metadataService = null
112+
$data = []
125113
) {
126114
$this->_categoryCollectionFactory = $categoryCollectionFactory;
127115
$this->_catalogCategory = $catalogCategory;
128116
$this->eventManager = $eventManager;
129117
$this->setFactory = $setFactory;
130118
$this->typeFactory = $typeFactory;
131119
$this->defaultAttributes = $defaultAttributes;
132-
$this->metadataService = $metadataService ?? ObjectManager::getInstance()->get(
133-
ProductAttributeRepositoryInterface::class
134-
);
135120
parent::__construct(
136121
$context,
137122
$storeManager,
@@ -249,17 +234,6 @@ public function getCategoryIds($product)
249234
return array_column($result, 'category_id');
250235
}
251236

252-
public function getCustomAttributesCodes()
253-
{
254-
if ($this->customAttributesCodes === null) {
255-
$this->customAttributesCodes = $this->getEavAttributesCodes($this->metadataService);
256-
$this->customAttributesCodes = array_values(
257-
array_diff($this->customAttributesCodes, ProductInterface::ATTRIBUTES)
258-
);
259-
}
260-
return $this->customAttributesCodes;
261-
}
262-
263237
/**
264238
* Get product identifier by sku
265239
*

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@
143143
<arguments>
144144
<argument name="catalogProductStatus" xsi:type="object">Magento\Catalog\Model\Product\Attribute\Source\Status\Proxy</argument>
145145
<argument name="productLink" xsi:type="object">Magento\Catalog\Model\Product\Link\Proxy</argument>
146+
<argument name="getCustomAttributeCodes" xsi:type="object">Magento\Catalog\Model\Entity\GetProductCustomAttributeCodes</argument>
147+
</arguments>
148+
</type>
149+
<type name="Magento\Catalog\Model\Category">
150+
<arguments>
151+
<argument name="getCustomAttributeCodes" xsi:type="object">Magento\Catalog\Model\Entity\GetCategoryCustomAttributeCodes</argument>
146152
</arguments>
147153
</type>
148154
<type name="Magento\Catalog\Model\ResourceModel\Product\Collection">

app/code/Magento/Eav/Model/Entity/GetCustomAttributeCodes.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,26 @@
99

1010
use Magento\Framework\Api\MetadataServiceInterface;
1111

12-
class GetCustomAttributeCodes
12+
class GetCustomAttributeCodes implements GetCustomAttributeCodesInterface
1313
{
1414
/**
15-
* @var string[]
15+
* @var string[][]
1616
*/
1717
private $customAttributesCodes;
1818

1919
/**
2020
* Receive a list of custom EAV attributes using provided metadata service. The results are cached per entity type
2121
*
2222
* @param MetadataServiceInterface $metadataService Custom attribute metadata service to be used
23-
* @param string[] $interfaceAttributes Attribute codes that are part of the interface and should not be
24-
* considered custom
25-
* @param string|null $entityType Entity type (class name), only needed if metadata service handles different
26-
* entities
2723
* @return string[]
2824
*/
29-
public function execute(
30-
MetadataServiceInterface $metadataService,
31-
array $interfaceAttributes,
32-
string $entityType = null
33-
): array {
34-
$cacheKey = get_class($metadataService) . '|' . $entityType;
25+
public function execute(MetadataServiceInterface $metadataService): array
26+
{
27+
$cacheKey = get_class($metadataService);
3528
if (!isset($this->customAttributesCodes[$cacheKey])) {
36-
$customAttributesCodes = $this->getEavAttributesCodes($metadataService, $entityType);
37-
$this->customAttributesCodes[$cacheKey] = array_values(
38-
array_diff($customAttributesCodes, $interfaceAttributes)
39-
);
29+
$this->customAttributesCodes[$cacheKey] = $this->getEavAttributesCodes($metadataService);
4030
}
41-
return $this->customAttributesCodes;
31+
return $this->customAttributesCodes[$cacheKey];
4232
}
4333

4434
/**
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Eav\Model\Entity;
8+
9+
use Magento\Framework\Api\MetadataServiceInterface;
10+
11+
interface GetCustomAttributeCodesInterface
12+
{
13+
/**
14+
* Receive a list of custom EAV attributes using provided metadata service.
15+
*
16+
* @param MetadataServiceInterface $metadataService Custom attribute metadata service to be used
17+
* @return string[]
18+
*/
19+
public function execute(MetadataServiceInterface $metadataService): array;
20+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
99
<preference for="Magento\Eav\Model\Entity\Setup\PropertyMapperInterface" type="Magento\Eav\Model\Entity\Setup\PropertyMapper\Composite" />
1010
<preference for="Magento\Eav\Model\Entity\AttributeLoaderInterface" type="Magento\Eav\Model\Entity\AttributeLoader" />
11+
<preference for="Magento\Eav\Model\Entity\GetCustomAttributeCodesInterface" type="Magento\Eav\Model\Entity\GetCustomAttributeCodes" />
1112
<preference for="Magento\Eav\Api\Data\AttributeInterface" type="Magento\Eav\Model\Entity\Attribute" />
1213
<preference for="Magento\Eav\Api\AttributeRepositoryInterface" type="Magento\Eav\Model\AttributeRepository" />
1314
<preference for="Magento\Eav\Api\Data\AttributeGroupInterface" type="Magento\Eav\Model\Entity\Attribute\Group" />

0 commit comments

Comments
 (0)