Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataCategoryHashMap.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Catalog\Model\ResourceModel\CategoryFactory;
11 
16 {
20  private $hashMap = [];
21 
25  private $categoryRepository;
26 
30  private $categoryResourceFactory;
31 
36  public function __construct(
37  CategoryRepository $categoryRepository,
38  CategoryFactory $categoryResourceFactory
39  ) {
40  $this->categoryRepository = $categoryRepository;
41  $this->categoryResourceFactory = $categoryResourceFactory;
42  }
43 
50  public function getAllData($categoryId)
51  {
52  if (!isset($this->hashMap[$categoryId])) {
53  $category = $this->categoryRepository->get($categoryId);
54  $this->hashMap[$categoryId] = $this->getAllCategoryChildrenIds($category);
55  }
56  return $this->hashMap[$categoryId];
57  }
58 
62  public function getData($categoryId, $key)
63  {
64  $categorySpecificData = $this->getAllData($categoryId);
65  if (isset($categorySpecificData[$key])) {
66  return $categorySpecificData[$key];
67  }
68  return [];
69  }
70 
77  private function getAllCategoryChildrenIds(CategoryInterface $category)
78  {
79  $categoryResource = $this->categoryResourceFactory->create();
80  $connection = $categoryResource->getConnection();
81  $select = $connection->select()
82  ->from($categoryResource->getEntityTable(), 'entity_id')
83  ->where($connection->quoteIdentifier('path') . ' LIKE :c_path');
84  $bind = ['c_path' => $category->getPath() . '%'];
85  return $connection->fetchCol($select, $bind);
86  }
87 
91  public function resetData($categoryId)
92  {
93  unset($this->hashMap[$categoryId]);
94  }
95 }
__construct(CategoryRepository $categoryRepository, CategoryFactory $categoryResourceFactory)
$connection
Definition: bulk.php:13