EntityStorage.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model;
  7. use Magento\Framework\Model\AbstractModel as FrameworkAbstractModel;
  8. /**
  9. * Class EntityStorage store only one type of entity per instance
  10. */
  11. class EntityStorage
  12. {
  13. /**
  14. * @var array
  15. */
  16. protected $registry = [];
  17. /**
  18. * Using for mapping hashes of identifying fields to entity ids
  19. *
  20. * @var array
  21. */
  22. protected $storageMapper = [];
  23. /**
  24. * Using for array concatenation
  25. */
  26. const GLUE = '';
  27. /**
  28. * Adds entity using identifying fields mapping, entity should have an id
  29. *
  30. * @param FrameworkAbstractModel $object
  31. * @param array $identifyingFields
  32. * @param string $storageName
  33. * @return void
  34. * @throws \Magento\Framework\Exception\InputException
  35. */
  36. public function addByIdentifyingFields(FrameworkAbstractModel $object, array $identifyingFields, $storageName)
  37. {
  38. if (empty($identifyingFields)) {
  39. throw new \Magento\Framework\Exception\InputException(__('Identifying Fields required'));
  40. }
  41. if (!$object->getId()) {
  42. throw new \Magento\Framework\Exception\InputException(__('An ID is needed. Set the ID and try again.'));
  43. }
  44. $this->storageMapper[$storageName][$this->getHash($identifyingFields)] = $object->getId();
  45. $this->registry[$object->getId()] = $object;
  46. }
  47. /**
  48. * Add entity to registry if entity in it
  49. *
  50. * @param \Magento\Framework\Model\AbstractModel $entity
  51. * @return void
  52. */
  53. public function add($entity)
  54. {
  55. $this->registry[$entity->getId()] = $entity;
  56. }
  57. /**
  58. * Retrieve entity from registry if entity in it
  59. *
  60. * @param int $id
  61. * @return bool|FrameworkAbstractModel
  62. */
  63. public function get($id)
  64. {
  65. if ($this->has($id)) {
  66. return $this->registry[$id];
  67. }
  68. return false;
  69. }
  70. /**
  71. * Gets entity by identifying fields
  72. *
  73. * @param array $identifyingFields
  74. * @param string $storageName
  75. * @return bool|FrameworkAbstractModel
  76. */
  77. public function getByIdentifyingFields(array $identifyingFields, $storageName)
  78. {
  79. $hash = $this->getHash($identifyingFields);
  80. if (isset($this->storageMapper[$storageName][$hash])) {
  81. return $this->get($this->storageMapper[$storageName][$hash]);
  82. }
  83. return false;
  84. }
  85. /**
  86. * Remove entity from storage
  87. *
  88. * @param int $id
  89. * @return void
  90. */
  91. public function remove($id)
  92. {
  93. if ($this->has($id)) {
  94. unset($this->registry[$id]);
  95. }
  96. }
  97. /**
  98. * Checks if entity is in storage
  99. *
  100. * @param int $id
  101. * @return bool
  102. */
  103. public function has($id)
  104. {
  105. return isset($this->registry[$id]);
  106. }
  107. /**
  108. * Gets hash using concatenation of identifying fields
  109. *
  110. * @param array $fields
  111. * @return string
  112. */
  113. protected function getHash(array $fields)
  114. {
  115. $stringForKey = implode(self::GLUE, $fields);
  116. return sha1($stringForKey);
  117. }
  118. }