HashMapPool.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogUrlRewrite\Model\Map;
  7. use Magento\Framework\ObjectManagerInterface;
  8. /**
  9. * Pool for hash maps
  10. */
  11. class HashMapPool
  12. {
  13. /**
  14. * @var HashMapInterface[]
  15. */
  16. private $dataArray = [];
  17. /**
  18. * @var ObjectManagerInterface
  19. */
  20. private $objectManager;
  21. /**
  22. * Constructor
  23. *
  24. * @param ObjectManagerInterface $objectManager
  25. */
  26. public function __construct(
  27. ObjectManagerInterface $objectManager
  28. ) {
  29. $this->objectManager = $objectManager;
  30. }
  31. /**
  32. * Gets a map by instance and category Id
  33. *
  34. * @param string $instanceName
  35. * @param int $categoryId
  36. * @return HashMapInterface
  37. * @throws \Exception
  38. */
  39. public function getDataMap($instanceName, $categoryId)
  40. {
  41. $key = $instanceName . '-' . $categoryId;
  42. if (!isset($this->dataArray[$key])) {
  43. $instance = $this->objectManager->create(
  44. $instanceName,
  45. [
  46. 'category' => $categoryId
  47. ]
  48. );
  49. if (!$instance instanceof HashMapInterface) {
  50. throw new \InvalidArgumentException(
  51. $instanceName . ' does not implement interface ' . HashMapInterface::class
  52. );
  53. }
  54. $this->dataArray[$key] = $instance;
  55. }
  56. return $this->dataArray[$key];
  57. }
  58. /**
  59. * Resets data in a hash map by instance name and category Id
  60. *
  61. * @param string $instanceName
  62. * @param int $categoryId
  63. * @return void
  64. */
  65. public function resetMap($instanceName, $categoryId)
  66. {
  67. $key = $instanceName . '-' . $categoryId;
  68. if (isset($this->dataArray[$key])) {
  69. $this->dataArray[$key]->resetData($categoryId);
  70. unset($this->dataArray[$key]);
  71. }
  72. }
  73. }