Skip to content

Commit

Permalink
Merge pull request #4830 from magento-chaika/Chaika-PR-2019-09-26
Browse files Browse the repository at this point in the history
Chaika-PR-2019-09-26
  • Loading branch information
viktym authored Sep 30, 2019
2 parents f35c6eb + e56bf94 commit 6f1a6f7
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.ExcessiveClassLength)
* @since 101.0.0
*/
class Eav extends AbstractModifier
Expand Down Expand Up @@ -1048,6 +1049,10 @@ private function isScopeGlobal($attribute)
*/
private function getAttributeModel($attribute)
{
// The statement below solves performance issue related to loading same attribute options on different models
if ($attribute instanceof EavAttribute) {
return $attribute;
}
$attributeId = $attribute->getAttributeId();

if (!array_key_exists($attributeId, $this->attributesCache)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ protected function getAttributes()
foreach ($attributes as $key => $attribute) {
if (isset($configurableData[$key])) {
$attributes[$key] = array_replace_recursive($attribute, $configurableData[$key]);
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
$attributes[$key]['values'] = array_merge(
isset($attribute['values']) ? $attribute['values'] : [],
isset($configurableData[$key]['values'])
Expand Down Expand Up @@ -412,14 +413,15 @@ private function prepareAttributes(
'position' => $configurableAttributes[$attribute->getAttributeId()]['position'],
'chosen' => [],
];
foreach ($attribute->getOptions() as $option) {
if (!empty($option->getValue())) {
$options = $attribute->usesSource() ? $attribute->getSource()->getAllOptions() : [];
foreach ($options as $option) {
if (!empty($option['value'])) {
$attributes[$attribute->getAttributeId()]['options'][] = [
'attribute_code' => $attribute->getAttributeCode(),
'attribute_label' => $attribute->getStoreLabel(0),
'id' => $option->getValue(),
'label' => $option->getLabel(),
'value' => $option->getValue(),
'id' => $option['value'],
'label' => $option['label'],
'value' => $option['value'],
'__disableTmpl' => true,
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use Magento\Framework\Escaper;

/**
* Associated products helper
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AssociatedProducts
Expand Down Expand Up @@ -231,6 +233,8 @@ public function getConfigurableAttributesData()
*
* @return void
* @throws \Zend_Currency_Exception
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* phpcs:disable Generic.Metrics.NestingLevel
*/
protected function prepareVariations()
{
Expand Down Expand Up @@ -262,14 +266,15 @@ protected function prepareVariations()
'position' => $configurableAttributes[$attribute->getAttributeId()]['position'],
'chosen' => [],
];
foreach ($attribute->getOptions() as $option) {
if (!empty($option->getValue())) {
$attributes[$attribute->getAttributeId()]['options'][$option->getValue()] = [
$options = $attribute->usesSource() ? $attribute->getSource()->getAllOptions() : [];
foreach ($options as $option) {
if (!empty($option['value'])) {
$attributes[$attribute->getAttributeId()]['options'][$option['value']] = [
'attribute_code' => $attribute->getAttributeCode(),
'attribute_label' => $attribute->getStoreLabel(0),
'id' => $option->getValue(),
'label' => $option->getLabel(),
'value' => $option->getValue(),
'id' => $option['value'],
'label' => $option['label'],
'value' => $option['value'],
];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,12 @@ protected function buildQueries(array $matches, array $queryValue)

$transformedTypes = [];
foreach ($matches as $match) {
$attributeAdapter = $this->attributeProvider->getByAttributeCode($match['field']);
$resolvedField = $this->fieldMapper->getFieldName(
$match['field'],
['type' => FieldMapperInterface::TYPE_QUERY]
);

$attributeAdapter = $this->attributeProvider->getByAttributeCode($resolvedField);
$fieldType = $this->fieldTypeResolver->getFieldType($attributeAdapter);
$valueTransformer = $this->valueTransformerPool->get($fieldType ?? 'text');
$valueTransformerHash = \spl_object_hash($valueTransformer);
Expand All @@ -151,10 +156,6 @@ protected function buildQueries(array $matches, array $queryValue)
continue;
}

$resolvedField = $this->fieldMapper->getFieldName(
$match['field'],
['type' => FieldMapperInterface::TYPE_QUERY]
);
$conditions[] = [
'condition' => $queryValue['condition'],
'body' => [
Expand Down
70 changes: 17 additions & 53 deletions app/code/Magento/Sales/Cron/CleanExpiredQuotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,35 @@
*/
namespace Magento\Sales\Cron;

use Magento\Store\Model\StoresConfig;
use Magento\Quote\Model\ResourceModel\Quote\Collection;
use Magento\Sales\Model\ResourceModel\Collection\ExpiredQuotesCollection;
use Magento\Store\Model\StoreManagerInterface;

/**
* Class CleanExpiredQuotes
*/
class CleanExpiredQuotes
{
const LIFETIME = 86400;

/**
* @var StoresConfig
*/
protected $storesConfig;

/**
* @var \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory
* @var ExpiredQuotesCollection
*/
protected $quoteCollectionFactory;
private $expiredQuotesCollection;

/**
* @var array
* @var StoreManagerInterface
*/
protected $expireQuotesFilterFields = [];
private $storeManager;

/**
* @param StoresConfig $storesConfig
* @param \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory $collectionFactory
* @param StoreManagerInterface $storeManager
* @param ExpiredQuotesCollection $expiredQuotesCollection
*/
public function __construct(
StoresConfig $storesConfig,
\Magento\Quote\Model\ResourceModel\Quote\CollectionFactory $collectionFactory
StoreManagerInterface $storeManager,
ExpiredQuotesCollection $expiredQuotesCollection
) {
$this->storesConfig = $storesConfig;
$this->quoteCollectionFactory = $collectionFactory;
$this->storeManager = $storeManager;
$this->expiredQuotesCollection = $expiredQuotesCollection;
}

/**
Expand All @@ -48,42 +43,11 @@ public function __construct(
*/
public function execute()
{
$lifetimes = $this->storesConfig->getStoresConfigByPath('checkout/cart/delete_quote_after');
foreach ($lifetimes as $storeId => $lifetime) {
$lifetime *= self::LIFETIME;

/** @var $quotes \Magento\Quote\Model\ResourceModel\Quote\Collection */
$quotes = $this->quoteCollectionFactory->create();

$quotes->addFieldToFilter('store_id', $storeId);
$quotes->addFieldToFilter('updated_at', ['to' => date("Y-m-d", time() - $lifetime)]);

foreach ($this->getExpireQuotesAdditionalFilterFields() as $field => $condition) {
$quotes->addFieldToFilter($field, $condition);
}

$stores = $this->storeManager->getStores(true);
foreach ($stores as $store) {
/** @var $quotes Collection */
$quotes = $this->expiredQuotesCollection->getExpiredQuotes($store);
$quotes->walk('delete');
}
}

/**
* Retrieve expire quotes additional fields to filter
*
* @return array
*/
protected function getExpireQuotesAdditionalFilterFields()
{
return $this->expireQuotesFilterFields;
}

/**
* Set expire quotes additional fields to filter
*
* @param array $fields
* @return void
*/
public function setExpireQuotesAdditionalFilterFields(array $fields)
{
$this->expireQuotesFilterFields = $fields;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Sales\Model\ResourceModel\Collection;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\Quote\Model\ResourceModel\Quote\Collection;
use Magento\Quote\Model\ResourceModel\Quote\CollectionFactory;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\ScopeInterface;

/**
* Class ExpiredQuotesCollection
*/
class ExpiredQuotesCollection
{
/**
* @var int
*/
private $secondsInDay = 86400;

/**
* @var string
*/
private $quoteLifetime = 'checkout/cart/delete_quote_after';

/**
* @var CollectionFactory
*/
private $quoteCollectionFactory;

/**
* @var ScopeConfigInterface
*/
private $config;

/**
* @param ScopeConfigInterface $config
* @param CollectionFactory $collectionFactory
*/
public function __construct(
ScopeConfigInterface $config,
CollectionFactory $collectionFactory
) {
$this->config = $config;
$this->quoteCollectionFactory = $collectionFactory;
}

/**
* Gets expired quotes
*
* Quote is considered expired if the latest update date
* of the quote is greater than lifetime threshold
*
* @param StoreInterface $store
* @return AbstractCollection
*/
public function getExpiredQuotes(StoreInterface $store): AbstractCollection
{
$lifetime = $this->config->getValue(
$this->quoteLifetime,
ScopeInterface::SCOPE_STORE,
$store->getCode()
);
$lifetime *= $this->secondsInDay;

/** @var $quotes Collection */
$quotes = $this->quoteCollectionFactory->create();
$quotes->addFieldToFilter('store_id', $store->getId());
$quotes->addFieldToFilter('updated_at', ['to' => date("Y-m-d", time() - $lifetime)]);

return $quotes;
}
}
84 changes: 0 additions & 84 deletions app/code/Magento/Sales/Test/Unit/Cron/CleanExpiredQuotesTest.php

This file was deleted.

0 comments on commit 6f1a6f7

Please sign in to comment.