FrontendActionsFlush.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Cron;
  7. use Magento\Catalog\Model\FrontendStorageConfigurationInterface;
  8. use Magento\Catalog\Model\FrontendStorageConfigurationPool;
  9. use Magento\Catalog\Model\ResourceModel\ProductFrontendAction;
  10. /**
  11. * Find deprecated frontend actions (@see \Magento\Catalog\Api\Data\ProductFrontendActionInterface)
  12. * Frontend actions deprecates by lifetime.
  13. * For each scope we have own lifetime.
  14. */
  15. class FrontendActionsFlush
  16. {
  17. /**
  18. * @var ProductFrontendAction
  19. */
  20. private $productFrontendActionResource;
  21. /**
  22. * @var FrontendStorageConfigurationPool
  23. */
  24. private $frontendStorageConfigurationPool;
  25. /**
  26. * @param ProductFrontendAction $productFrontendActionResource
  27. * @param FrontendStorageConfigurationPool $frontendStorageConfigurationPool
  28. */
  29. public function __construct(
  30. ProductFrontendAction $productFrontendActionResource,
  31. FrontendStorageConfigurationPool $frontendStorageConfigurationPool
  32. ) {
  33. $this->productFrontendActionResource = $productFrontendActionResource;
  34. $this->frontendStorageConfigurationPool = $frontendStorageConfigurationPool;
  35. }
  36. /**
  37. * Find lifetime in configuration. Configuration is hold in Stores Configuration
  38. * Also this configuration is generated by:
  39. * @see \Magento\Catalog\Model\Widget\RecentlyViewedStorageConfiguration
  40. *
  41. * @param string $namespace
  42. * @return int
  43. */
  44. private function getLifeTimeByNamespace($namespace)
  45. {
  46. $configurationObject = $this->frontendStorageConfigurationPool->get($namespace);
  47. if ($configurationObject) {
  48. $configuration = $configurationObject->get();
  49. } else {
  50. $configuration = [
  51. 'lifetime' => FrontendStorageConfigurationInterface::DEFAULT_LIFETIME
  52. ];
  53. }
  54. return (int)$configuration['lifetime'] ?? FrontendStorageConfigurationInterface::DEFAULT_LIFETIME;
  55. }
  56. /**
  57. * Retrieve unique namespaces FROM frontend actions. Namespace can be represented by:
  58. * recently_viewed_product, recently_compared_product, etc...
  59. *
  60. * @return array
  61. */
  62. private function getUniqueNamespaces()
  63. {
  64. $adapter = $this->productFrontendActionResource->getConnection();
  65. $query = $adapter->select()
  66. ->from($this->productFrontendActionResource->getMainTable(), ['action_id', 'type_id'])
  67. ->group('type_id');
  68. return $adapter->fetchPairs($query);
  69. }
  70. /**
  71. * @inheritdoc
  72. */
  73. public function execute()
  74. {
  75. $adapter = $this->productFrontendActionResource->getConnection();
  76. foreach ($this->getUniqueNamespaces() as $namespace) {
  77. $lifeTime = $this->getLifeTimeByNamespace($namespace);
  78. $where = [
  79. $adapter->quoteInto('added_at < ?', time() - $lifeTime)
  80. ];
  81. $adapter->delete($this->productFrontendActionResource->getMainTable(), $where);
  82. }
  83. }
  84. }