Skip to content

Commit 6f1a6f7

Browse files
authored
Merge pull request #4830 from magento-chaika/Chaika-PR-2019-09-26
Chaika-PR-2019-09-26
2 parents f35c6eb + e56bf94 commit 6f1a6f7

File tree

7 files changed

+125
-153
lines changed

7 files changed

+125
-153
lines changed

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Eav.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
4747
* @SuppressWarnings(PHPMD.TooManyFields)
4848
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
49+
* @SuppressWarnings(PHPMD.ExcessiveClassLength)
4950
* @since 101.0.0
5051
*/
5152
class Eav extends AbstractModifier
@@ -1048,6 +1049,10 @@ private function isScopeGlobal($attribute)
10481049
*/
10491050
private function getAttributeModel($attribute)
10501051
{
1052+
// The statement below solves performance issue related to loading same attribute options on different models
1053+
if ($attribute instanceof EavAttribute) {
1054+
return $attribute;
1055+
}
10511056
$attributeId = $attribute->getAttributeId();
10521057

10531058
if (!array_key_exists($attributeId, $this->attributesCache)) {

app/code/Magento/ConfigurableProduct/Block/Adminhtml/Product/Edit/Tab/Variations/Config/Matrix.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ protected function getAttributes()
197197
foreach ($attributes as $key => $attribute) {
198198
if (isset($configurableData[$key])) {
199199
$attributes[$key] = array_replace_recursive($attribute, $configurableData[$key]);
200+
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
200201
$attributes[$key]['values'] = array_merge(
201202
isset($attribute['values']) ? $attribute['values'] : [],
202203
isset($configurableData[$key]['values'])
@@ -412,14 +413,15 @@ private function prepareAttributes(
412413
'position' => $configurableAttributes[$attribute->getAttributeId()]['position'],
413414
'chosen' => [],
414415
];
415-
foreach ($attribute->getOptions() as $option) {
416-
if (!empty($option->getValue())) {
416+
$options = $attribute->usesSource() ? $attribute->getSource()->getAllOptions() : [];
417+
foreach ($options as $option) {
418+
if (!empty($option['value'])) {
417419
$attributes[$attribute->getAttributeId()]['options'][] = [
418420
'attribute_code' => $attribute->getAttributeCode(),
419421
'attribute_label' => $attribute->getStoreLabel(0),
420-
'id' => $option->getValue(),
421-
'label' => $option->getLabel(),
422-
'value' => $option->getValue(),
422+
'id' => $option['value'],
423+
'label' => $option['label'],
424+
'value' => $option['value'],
423425
'__disableTmpl' => true,
424426
];
425427
}

app/code/Magento/ConfigurableProduct/Ui/DataProvider/Product/Form/Modifier/Data/AssociatedProducts.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use Magento\Framework\Escaper;
2222

2323
/**
24+
* Associated products helper
25+
*
2426
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2527
*/
2628
class AssociatedProducts
@@ -231,6 +233,8 @@ public function getConfigurableAttributesData()
231233
*
232234
* @return void
233235
* @throws \Zend_Currency_Exception
236+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
237+
* phpcs:disable Generic.Metrics.NestingLevel
234238
*/
235239
protected function prepareVariations()
236240
{
@@ -262,14 +266,15 @@ protected function prepareVariations()
262266
'position' => $configurableAttributes[$attribute->getAttributeId()]['position'],
263267
'chosen' => [],
264268
];
265-
foreach ($attribute->getOptions() as $option) {
266-
if (!empty($option->getValue())) {
267-
$attributes[$attribute->getAttributeId()]['options'][$option->getValue()] = [
269+
$options = $attribute->usesSource() ? $attribute->getSource()->getAllOptions() : [];
270+
foreach ($options as $option) {
271+
if (!empty($option['value'])) {
272+
$attributes[$attribute->getAttributeId()]['options'][$option['value']] = [
268273
'attribute_code' => $attribute->getAttributeCode(),
269274
'attribute_label' => $attribute->getStoreLabel(0),
270-
'id' => $option->getValue(),
271-
'label' => $option->getLabel(),
272-
'value' => $option->getValue(),
275+
'id' => $option['value'],
276+
'label' => $option['label'],
277+
'value' => $option['value'],
273278
];
274279
}
275280
}

app/code/Magento/Elasticsearch/SearchAdapter/Query/Builder/Match.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,12 @@ protected function buildQueries(array $matches, array $queryValue)
138138

139139
$transformedTypes = [];
140140
foreach ($matches as $match) {
141-
$attributeAdapter = $this->attributeProvider->getByAttributeCode($match['field']);
141+
$resolvedField = $this->fieldMapper->getFieldName(
142+
$match['field'],
143+
['type' => FieldMapperInterface::TYPE_QUERY]
144+
);
145+
146+
$attributeAdapter = $this->attributeProvider->getByAttributeCode($resolvedField);
142147
$fieldType = $this->fieldTypeResolver->getFieldType($attributeAdapter);
143148
$valueTransformer = $this->valueTransformerPool->get($fieldType ?? 'text');
144149
$valueTransformerHash = \spl_object_hash($valueTransformer);
@@ -151,10 +156,6 @@ protected function buildQueries(array $matches, array $queryValue)
151156
continue;
152157
}
153158

154-
$resolvedField = $this->fieldMapper->getFieldName(
155-
$match['field'],
156-
['type' => FieldMapperInterface::TYPE_QUERY]
157-
);
158159
$conditions[] = [
159160
'condition' => $queryValue['condition'],
160161
'body' => [

app/code/Magento/Sales/Cron/CleanExpiredQuotes.php

Lines changed: 17 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,35 @@
55
*/
66
namespace Magento\Sales\Cron;
77

8-
use Magento\Store\Model\StoresConfig;
8+
use Magento\Quote\Model\ResourceModel\Quote\Collection;
9+
use Magento\Sales\Model\ResourceModel\Collection\ExpiredQuotesCollection;
10+
use Magento\Store\Model\StoreManagerInterface;
911

1012
/**
1113
* Class CleanExpiredQuotes
1214
*/
1315
class CleanExpiredQuotes
1416
{
15-
const LIFETIME = 86400;
16-
17-
/**
18-
* @var StoresConfig
19-
*/
20-
protected $storesConfig;
21-
2217
/**
23-
* @var \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory
18+
* @var ExpiredQuotesCollection
2419
*/
25-
protected $quoteCollectionFactory;
20+
private $expiredQuotesCollection;
2621

2722
/**
28-
* @var array
23+
* @var StoreManagerInterface
2924
*/
30-
protected $expireQuotesFilterFields = [];
25+
private $storeManager;
3126

3227
/**
33-
* @param StoresConfig $storesConfig
34-
* @param \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory $collectionFactory
28+
* @param StoreManagerInterface $storeManager
29+
* @param ExpiredQuotesCollection $expiredQuotesCollection
3530
*/
3631
public function __construct(
37-
StoresConfig $storesConfig,
38-
\Magento\Quote\Model\ResourceModel\Quote\CollectionFactory $collectionFactory
32+
StoreManagerInterface $storeManager,
33+
ExpiredQuotesCollection $expiredQuotesCollection
3934
) {
40-
$this->storesConfig = $storesConfig;
41-
$this->quoteCollectionFactory = $collectionFactory;
35+
$this->storeManager = $storeManager;
36+
$this->expiredQuotesCollection = $expiredQuotesCollection;
4237
}
4338

4439
/**
@@ -48,42 +43,11 @@ public function __construct(
4843
*/
4944
public function execute()
5045
{
51-
$lifetimes = $this->storesConfig->getStoresConfigByPath('checkout/cart/delete_quote_after');
52-
foreach ($lifetimes as $storeId => $lifetime) {
53-
$lifetime *= self::LIFETIME;
54-
55-
/** @var $quotes \Magento\Quote\Model\ResourceModel\Quote\Collection */
56-
$quotes = $this->quoteCollectionFactory->create();
57-
58-
$quotes->addFieldToFilter('store_id', $storeId);
59-
$quotes->addFieldToFilter('updated_at', ['to' => date("Y-m-d", time() - $lifetime)]);
60-
61-
foreach ($this->getExpireQuotesAdditionalFilterFields() as $field => $condition) {
62-
$quotes->addFieldToFilter($field, $condition);
63-
}
64-
46+
$stores = $this->storeManager->getStores(true);
47+
foreach ($stores as $store) {
48+
/** @var $quotes Collection */
49+
$quotes = $this->expiredQuotesCollection->getExpiredQuotes($store);
6550
$quotes->walk('delete');
6651
}
6752
}
68-
69-
/**
70-
* Retrieve expire quotes additional fields to filter
71-
*
72-
* @return array
73-
*/
74-
protected function getExpireQuotesAdditionalFilterFields()
75-
{
76-
return $this->expireQuotesFilterFields;
77-
}
78-
79-
/**
80-
* Set expire quotes additional fields to filter
81-
*
82-
* @param array $fields
83-
* @return void
84-
*/
85-
public function setExpireQuotesAdditionalFilterFields(array $fields)
86-
{
87-
$this->expireQuotesFilterFields = $fields;
88-
}
8953
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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\Sales\Model\ResourceModel\Collection;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
12+
use Magento\Quote\Model\ResourceModel\Quote\Collection;
13+
use Magento\Quote\Model\ResourceModel\Quote\CollectionFactory;
14+
use Magento\Store\Api\Data\StoreInterface;
15+
use Magento\Store\Model\ScopeInterface;
16+
17+
/**
18+
* Class ExpiredQuotesCollection
19+
*/
20+
class ExpiredQuotesCollection
21+
{
22+
/**
23+
* @var int
24+
*/
25+
private $secondsInDay = 86400;
26+
27+
/**
28+
* @var string
29+
*/
30+
private $quoteLifetime = 'checkout/cart/delete_quote_after';
31+
32+
/**
33+
* @var CollectionFactory
34+
*/
35+
private $quoteCollectionFactory;
36+
37+
/**
38+
* @var ScopeConfigInterface
39+
*/
40+
private $config;
41+
42+
/**
43+
* @param ScopeConfigInterface $config
44+
* @param CollectionFactory $collectionFactory
45+
*/
46+
public function __construct(
47+
ScopeConfigInterface $config,
48+
CollectionFactory $collectionFactory
49+
) {
50+
$this->config = $config;
51+
$this->quoteCollectionFactory = $collectionFactory;
52+
}
53+
54+
/**
55+
* Gets expired quotes
56+
*
57+
* Quote is considered expired if the latest update date
58+
* of the quote is greater than lifetime threshold
59+
*
60+
* @param StoreInterface $store
61+
* @return AbstractCollection
62+
*/
63+
public function getExpiredQuotes(StoreInterface $store): AbstractCollection
64+
{
65+
$lifetime = $this->config->getValue(
66+
$this->quoteLifetime,
67+
ScopeInterface::SCOPE_STORE,
68+
$store->getCode()
69+
);
70+
$lifetime *= $this->secondsInDay;
71+
72+
/** @var $quotes Collection */
73+
$quotes = $this->quoteCollectionFactory->create();
74+
$quotes->addFieldToFilter('store_id', $store->getId());
75+
$quotes->addFieldToFilter('updated_at', ['to' => date("Y-m-d", time() - $lifetime)]);
76+
77+
return $quotes;
78+
}
79+
}

app/code/Magento/Sales/Test/Unit/Cron/CleanExpiredQuotesTest.php

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

0 commit comments

Comments
 (0)