SearchWeight.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogSearch\Model\Attribute;
  7. /**
  8. * This plugin is responsible for processing of search_weight property of a product attribute.
  9. *
  10. * 'search_weight' is used to boost matches by specific attributes.
  11. *
  12. * This is part of search accuracy customization functionality.
  13. */
  14. class SearchWeight
  15. {
  16. /**
  17. * @param \Magento\Framework\Search\Request\Config $config
  18. */
  19. public function __construct(
  20. \Magento\Framework\Search\Request\Config $config
  21. ) {
  22. $this->config = $config;
  23. }
  24. /**
  25. * Cleans a cache of search requests when attribute's search weight is changed.
  26. *
  27. * A product attribute in Magento contains a property named 'search_weight'.
  28. * This property should be passed to a search adapter.
  29. * And container which is responsible for this is the Search Request.
  30. *
  31. * However, search requests are dynamically generated and therefore cached in the Configuration cache.
  32. *
  33. * But, as they're cached, there is a problem when search weight is changed for an attribute
  34. * as it will not change in the cache.
  35. *
  36. * This plugin solves this issue by resetting cache of search requests
  37. * when an attribute's search weight is changed.
  38. *
  39. * @param \Magento\Catalog\Model\ResourceModel\Attribute $subject
  40. * @param \Closure $proceed
  41. * @param \Magento\Framework\Model\AbstractModel $attribute
  42. * @return \Magento\Catalog\Model\ResourceModel\Attribute
  43. *
  44. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  45. */
  46. public function aroundSave(
  47. \Magento\Catalog\Model\ResourceModel\Attribute $subject,
  48. \Closure $proceed,
  49. \Magento\Framework\Model\AbstractModel $attribute
  50. ) {
  51. $isNew = $attribute->isObjectNew();
  52. $isWeightChanged = $attribute->dataHasChangedFor('search_weight');
  53. $result = $proceed($attribute);
  54. if ($isNew || $isWeightChanged) {
  55. $this->config->reset();
  56. }
  57. return $result;
  58. }
  59. }