Synchronized.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AdminNotification\Model\ResourceModel\System\Message\Collection;
  7. /**
  8. * @api
  9. * @since 100.0.2
  10. */
  11. class Synchronized extends \Magento\AdminNotification\Model\ResourceModel\System\Message\Collection
  12. {
  13. /**
  14. * Unread message list
  15. *
  16. * @var \Magento\Framework\Notification\MessageInterface[]
  17. */
  18. protected $_unreadMessages = [];
  19. /**
  20. * Store new messages in database and remove outdated messages
  21. *
  22. * @return $this|\Magento\Framework\Model\ResourceModel\Db\AbstractDb
  23. */
  24. public function _afterLoad()
  25. {
  26. $messages = $this->_messageList->asArray();
  27. $persisted = [];
  28. $unread = [];
  29. foreach ($messages as $message) {
  30. if ($message->isDisplayed()) {
  31. foreach ($this->_items as $persistedKey => $persistedMessage) {
  32. if ($message->getIdentity() == $persistedMessage->getIdentity()) {
  33. $persisted[$persistedKey] = $persistedMessage;
  34. continue 2;
  35. }
  36. }
  37. $unread[] = $message;
  38. }
  39. }
  40. $removed = array_diff_key($this->_items, $persisted);
  41. foreach ($removed as $removedItem) {
  42. $removedItem->delete();
  43. }
  44. foreach ($unread as $unreadItem) {
  45. $item = $this->getNewEmptyItem();
  46. $item->setIdentity($unreadItem->getIdentity())->setSeverity($unreadItem->getSeverity())->save();
  47. }
  48. if (count($removed) || count($unread)) {
  49. $this->_unreadMessages = $unread;
  50. $this->clear();
  51. $this->load();
  52. } else {
  53. parent::_afterLoad();
  54. }
  55. return $this;
  56. }
  57. /**
  58. * Retrieve list of unread messages
  59. *
  60. * @return \Magento\Framework\Notification\MessageInterface[]
  61. */
  62. public function getUnread()
  63. {
  64. return $this->_unreadMessages;
  65. }
  66. }