CanViewNotification.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ReleaseNotification\Model\Condition;
  7. use Magento\ReleaseNotification\Model\ResourceModel\Viewer\Logger;
  8. use Magento\Backend\Model\Auth\Session;
  9. use Magento\Framework\App\ProductMetadataInterface;
  10. use Magento\Framework\View\Layout\Condition\VisibilityConditionInterface;
  11. use Magento\Framework\App\CacheInterface;
  12. /**
  13. * Dynamic validator for UI release notification, manage UI component visibility.
  14. * Return true if the logged in user has not seen the notification.
  15. */
  16. class CanViewNotification implements VisibilityConditionInterface
  17. {
  18. /**
  19. * Unique condition name.
  20. *
  21. * @var string
  22. */
  23. private static $conditionName = 'can_view_notification';
  24. /**
  25. * Prefix for cache
  26. *
  27. * @var string
  28. */
  29. private static $cachePrefix = 'release-notification-popup-';
  30. /**
  31. * @var Logger
  32. */
  33. private $viewerLogger;
  34. /**
  35. * @var Session
  36. */
  37. private $session;
  38. /**
  39. * @var ProductMetadataInterface
  40. */
  41. private $productMetadata;
  42. /**
  43. * @var CacheInterface
  44. */
  45. private $cacheStorage;
  46. /**
  47. * CanViewNotification constructor.
  48. *
  49. * @param Logger $viewerLogger
  50. * @param Session $session
  51. * @param ProductMetadataInterface $productMetadata
  52. * @param CacheInterface $cacheStorage
  53. */
  54. public function __construct(
  55. Logger $viewerLogger,
  56. Session $session,
  57. ProductMetadataInterface $productMetadata,
  58. CacheInterface $cacheStorage
  59. ) {
  60. $this->viewerLogger = $viewerLogger;
  61. $this->session = $session;
  62. $this->productMetadata = $productMetadata;
  63. $this->cacheStorage = $cacheStorage;
  64. }
  65. /**
  66. * Validate if notification popup can be shown and set the notification flag
  67. *
  68. * @inheritdoc
  69. */
  70. public function isVisible(array $arguments)
  71. {
  72. $userId = $this->session->getUser()->getId();
  73. $cacheKey = self::$cachePrefix . $userId;
  74. $value = $this->cacheStorage->load($cacheKey);
  75. if ($value === false) {
  76. $value = version_compare(
  77. $this->viewerLogger->get($userId)->getLastViewVersion(),
  78. $this->productMetadata->getVersion(),
  79. '<'
  80. );
  81. $this->cacheStorage->save(false, $cacheKey);
  82. }
  83. return (bool)$value;
  84. }
  85. /**
  86. * Get condition name
  87. *
  88. * @return string
  89. */
  90. public function getName()
  91. {
  92. return self::$conditionName;
  93. }
  94. }