SwatchAttributeCodes.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Swatches\Model;
  7. use Magento\Framework\App\CacheInterface;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\Framework\DB\Select;
  10. /**
  11. * Class SwatchAttributeCodes for getting codes of swatch attributes.
  12. */
  13. class SwatchAttributeCodes
  14. {
  15. /**
  16. * @var string
  17. */
  18. private $cacheKey;
  19. /**
  20. * @var CacheInterface
  21. */
  22. private $cache;
  23. /**
  24. * @var ResourceConnection
  25. */
  26. private $resourceConnection;
  27. /**
  28. * Key is attribute_id, value is attribute_code
  29. *
  30. * @var array
  31. */
  32. private $swatchAttributeCodes;
  33. /**
  34. * @var array
  35. */
  36. private $cacheTags;
  37. /**
  38. * SwatchAttributeList constructor.
  39. *
  40. * @param CacheInterface $cache
  41. * @param ResourceConnection $resourceConnection
  42. * @param string $cacheKey
  43. * @param array $cacheTags
  44. */
  45. public function __construct(
  46. CacheInterface $cache,
  47. ResourceConnection $resourceConnection,
  48. $cacheKey,
  49. array $cacheTags
  50. ) {
  51. $this->cache = $cache;
  52. $this->resourceConnection = $resourceConnection;
  53. $this->cacheKey = $cacheKey;
  54. $this->cacheTags = $cacheTags;
  55. }
  56. /**
  57. * Returns list of known swatch attribute codes. Check cache and database.
  58. * Key is attribute_id, value is attribute_code
  59. *
  60. * @return array
  61. */
  62. public function getCodes()
  63. {
  64. if ($this->swatchAttributeCodes === null) {
  65. $swatchAttributeCodesCache = $this->cache->load($this->cacheKey);
  66. if (false === $swatchAttributeCodesCache) {
  67. $swatchAttributeCodes = $this->getSwatchAttributeCodes();
  68. $this->cache->save(json_encode($swatchAttributeCodes), $this->cacheKey, $this->cacheTags);
  69. } else {
  70. $swatchAttributeCodes = json_decode($swatchAttributeCodesCache, true);
  71. }
  72. $this->swatchAttributeCodes = $swatchAttributeCodes;
  73. }
  74. return $this->swatchAttributeCodes;
  75. }
  76. /**
  77. * Returns list of known swatch attributes.
  78. *
  79. * Returns a map of id and code for all EAV attributes with swatches
  80. *
  81. * @return array
  82. */
  83. private function getSwatchAttributeCodes()
  84. {
  85. $select = $this->resourceConnection->getConnection()->select()
  86. ->from(
  87. ['a' => $this->resourceConnection->getTableName('eav_attribute')],
  88. [
  89. 'attribute_id' => 'a.attribute_id',
  90. 'attribute_code' => 'a.attribute_code',
  91. ]
  92. )->where(
  93. 'a.attribute_id IN (?)',
  94. new \Zend_Db_Expr($this->getAttributeIdsSelect())
  95. );
  96. $result = $this->resourceConnection->getConnection()->fetchPairs($select);
  97. return $result;
  98. }
  99. /**
  100. * Returns Select for attributes Ids.
  101. *
  102. * Builds a "Select" object which loads all EAV attributes that has "swatch" options
  103. *
  104. * @return Select
  105. */
  106. private function getAttributeIdsSelect()
  107. {
  108. return $this->resourceConnection->getConnection()->select()
  109. ->from(
  110. ['o' => $this->resourceConnection->getTableName('eav_attribute_option')],
  111. ['attribute_id' => 'o.attribute_id']
  112. )->join(
  113. ['s' => $this->resourceConnection->getTableName('eav_attribute_option_swatch')],
  114. 'o.option_id = s.option_id',
  115. []
  116. );
  117. }
  118. }