ListStatus.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Status;
  7. /**
  8. * Service model for managing statuses information. Statuses are just records with code, message and any
  9. * additional data. The model helps to keep track and manipulate statuses, that different modules want to set
  10. * to owner object of this model.
  11. *
  12. * @author Magento Core Team <core@magentocommerce.com>
  13. */
  14. class ListStatus
  15. {
  16. /**
  17. * Status information entities
  18. *
  19. * @var array
  20. */
  21. protected $_items = [];
  22. /**
  23. * Adds status information to the list of items.
  24. *
  25. * @param string|null $origin Usually a name of module, that adds this status
  26. * @param int|null $code Code of status, unique for origin, that sets it
  27. * @param string|null $message Status message
  28. * @param \Magento\Framework\DataObject|null $additionalData Any additional data, that caller would like to store
  29. * @return $this
  30. */
  31. public function addItem($origin = null, $code = null, $message = null, $additionalData = null)
  32. {
  33. $this->_items[] = [
  34. 'origin' => $origin,
  35. 'code' => $code,
  36. 'message' => $message,
  37. 'additionalData' => $additionalData,
  38. ];
  39. return $this;
  40. }
  41. /**
  42. * Retrieves all items
  43. *
  44. * @return array
  45. */
  46. public function getItems()
  47. {
  48. return $this->_items;
  49. }
  50. /**
  51. * Removes items, that have parameters equal to passed in $params.
  52. * Returns items removed.
  53. * $params can have following keys (if not set - then any item is good for this key):
  54. * 'origin', 'code', 'message'
  55. *
  56. * @param array $params
  57. * @return array
  58. */
  59. public function removeItemsByParams($params)
  60. {
  61. $items = $this->getItems();
  62. if (!$items) {
  63. return [];
  64. }
  65. $indexes = [];
  66. $paramKeys = ['origin', 'code', 'message'];
  67. foreach ($items as $index => $item) {
  68. $remove = true;
  69. foreach ($paramKeys as $key) {
  70. if (!isset($params[$key])) {
  71. continue;
  72. }
  73. if ($params[$key] != $item[$key]) {
  74. $remove = false;
  75. break;
  76. }
  77. }
  78. if ($remove) {
  79. $indexes[] = $index;
  80. }
  81. }
  82. return $this->removeItems($indexes);
  83. }
  84. /**
  85. * Removes items at mentioned index/indexes.
  86. * Returns items removed.
  87. *
  88. * @param int|array $indexes
  89. * @return array
  90. */
  91. public function removeItems($indexes)
  92. {
  93. if (![$indexes]) {
  94. $indexes = [$indexes];
  95. }
  96. if (!$indexes) {
  97. return [];
  98. }
  99. $items = $this->getItems();
  100. if (!$items) {
  101. return [];
  102. }
  103. $newItems = [];
  104. $removedItems = [];
  105. foreach ($items as $indexNow => $item) {
  106. if (in_array($indexNow, $indexes)) {
  107. $removedItems[] = $item;
  108. } else {
  109. $newItems[] = $item;
  110. }
  111. }
  112. $this->_items = $newItems;
  113. return $removedItems;
  114. }
  115. /**
  116. * Clears list from all items
  117. *
  118. * @return $this
  119. */
  120. public function clear()
  121. {
  122. $this->_items = [];
  123. return $this;
  124. }
  125. }