NotificationStorage.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Customer;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Cache\FrontendInterface;
  9. use Magento\Framework\Serialize\SerializerInterface;
  10. class NotificationStorage
  11. {
  12. const UPDATE_CUSTOMER_SESSION = 'update_customer_session';
  13. /**
  14. * @var FrontendInterface
  15. */
  16. private $cache;
  17. /**
  18. * @var SerializerInterface
  19. */
  20. private $serializer;
  21. /**
  22. * NotificationStorage constructor.
  23. * @param FrontendInterface $cache
  24. * @param SerializerInterface $serializer
  25. */
  26. public function __construct(
  27. FrontendInterface $cache,
  28. SerializerInterface $serializer = null
  29. ) {
  30. $this->cache = $cache;
  31. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
  32. }
  33. /**
  34. * Add notification in cache
  35. *
  36. * @param string $notificationType
  37. * @param string $customerId
  38. * @return void
  39. */
  40. public function add($notificationType, $customerId)
  41. {
  42. $this->cache->save(
  43. $this->serializer->serialize([
  44. 'customer_id' => $customerId,
  45. 'notification_type' => $notificationType
  46. ]),
  47. $this->getCacheKey($notificationType, $customerId)
  48. );
  49. }
  50. /**
  51. * Check whether notification is exists in cache
  52. *
  53. * @param string $notificationType
  54. * @param string $customerId
  55. * @return bool
  56. */
  57. public function isExists($notificationType, $customerId)
  58. {
  59. return $this->cache->test($this->getCacheKey($notificationType, $customerId));
  60. }
  61. /**
  62. * Remove notification from cache
  63. *
  64. * @param string $notificationType
  65. * @param string $customerId
  66. * @return void
  67. */
  68. public function remove($notificationType, $customerId)
  69. {
  70. $this->cache->remove($this->getCacheKey($notificationType, $customerId));
  71. }
  72. /**
  73. * Retrieve cache key
  74. *
  75. * @param string $notificationType
  76. * @param string $customerId
  77. * @return string
  78. */
  79. private function getCacheKey($notificationType, $customerId)
  80. {
  81. return 'notification_' . $notificationType . '_' . $customerId;
  82. }
  83. }