Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SwatchAttributeCodes.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Swatches\Model;
7 
11 
16 {
20  private $cacheKey;
21 
25  private $cache;
26 
30  private $resourceConnection;
31 
37  private $swatchAttributeCodes;
38 
42  private $cacheTags;
43 
52  public function __construct(
53  CacheInterface $cache,
54  ResourceConnection $resourceConnection,
55  $cacheKey,
56  array $cacheTags
57  ) {
58  $this->cache = $cache;
59  $this->resourceConnection = $resourceConnection;
60  $this->cacheKey = $cacheKey;
61  $this->cacheTags = $cacheTags;
62  }
63 
70  public function getCodes()
71  {
72  if ($this->swatchAttributeCodes === null) {
73  $swatchAttributeCodesCache = $this->cache->load($this->cacheKey);
74  if (false === $swatchAttributeCodesCache) {
75  $swatchAttributeCodes = $this->getSwatchAttributeCodes();
76  $this->cache->save(json_encode($swatchAttributeCodes), $this->cacheKey, $this->cacheTags);
77  } else {
78  $swatchAttributeCodes = json_decode($swatchAttributeCodesCache, true);
79  }
80  $this->swatchAttributeCodes = $swatchAttributeCodes;
81  }
82 
83  return $this->swatchAttributeCodes;
84  }
85 
93  private function getSwatchAttributeCodes()
94  {
95  $select = $this->resourceConnection->getConnection()->select()
96  ->from(
97  ['a' => $this->resourceConnection->getTableName('eav_attribute')],
98  [
99  'attribute_id' => 'a.attribute_id',
100  'attribute_code' => 'a.attribute_code',
101  ]
102  )->where(
103  'a.attribute_id IN (?)',
104  new \Zend_Db_Expr($this->getAttributeIdsSelect())
105  );
106  $result = $this->resourceConnection->getConnection()->fetchPairs($select);
107  return $result;
108  }
109 
117  private function getAttributeIdsSelect()
118  {
119  return $this->resourceConnection->getConnection()->select()
120  ->from(
121  ['o' => $this->resourceConnection->getTableName('eav_attribute_option')],
122  ['attribute_id' => 'o.attribute_id']
123  )->join(
124  ['s' => $this->resourceConnection->getTableName('eav_attribute_option_swatch')],
125  'o.option_id = s.option_id',
126  []
127  );
128  }
129 }
__construct(CacheInterface $cache, ResourceConnection $resourceConnection, $cacheKey, array $cacheTags)