CacheOutdated.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AdminNotification\Model\System\Message;
  7. /**
  8. * @api
  9. * @since 100.0.2
  10. */
  11. class CacheOutdated implements \Magento\Framework\Notification\MessageInterface
  12. {
  13. /**
  14. * @var \Magento\Framework\UrlInterface
  15. */
  16. protected $_urlBuilder;
  17. /**
  18. * @var \Magento\Framework\AuthorizationInterface
  19. */
  20. protected $_authorization;
  21. /**
  22. * @var \Magento\Framework\App\Cache\TypeListInterface
  23. */
  24. protected $_cacheTypeList;
  25. /**
  26. * @param \Magento\Framework\AuthorizationInterface $authorization
  27. * @param \Magento\Framework\UrlInterface $urlBuilder
  28. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  29. */
  30. public function __construct(
  31. \Magento\Framework\AuthorizationInterface $authorization,
  32. \Magento\Framework\UrlInterface $urlBuilder,
  33. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  34. ) {
  35. $this->_authorization = $authorization;
  36. $this->_urlBuilder = $urlBuilder;
  37. $this->_cacheTypeList = $cacheTypeList;
  38. }
  39. /**
  40. * Get array of cache types which require data refresh
  41. *
  42. * @return array
  43. */
  44. protected function _getCacheTypesForRefresh()
  45. {
  46. $output = [];
  47. foreach ($this->_cacheTypeList->getInvalidated() as $type) {
  48. $output[] = $type->getCacheType();
  49. }
  50. return $output;
  51. }
  52. /**
  53. * Retrieve unique message identity
  54. *
  55. * @return string
  56. */
  57. public function getIdentity()
  58. {
  59. return md5('cache' . implode(':', $this->_getCacheTypesForRefresh()));
  60. }
  61. /**
  62. * Check whether
  63. *
  64. * @return bool
  65. */
  66. public function isDisplayed()
  67. {
  68. return $this->_authorization->isAllowed(
  69. 'Magento_Backend::cache'
  70. ) && count(
  71. $this->_getCacheTypesForRefresh()
  72. ) > 0;
  73. }
  74. /**
  75. * Retrieve message text
  76. *
  77. * @return string
  78. */
  79. public function getText()
  80. {
  81. $cacheTypes = implode(', ', $this->_getCacheTypesForRefresh());
  82. $message = __('One or more of the Cache Types are invalidated: %1. ', $cacheTypes) . ' ';
  83. $url = $this->_urlBuilder->getUrl('adminhtml/cache');
  84. $message .= __('Please go to <a href="%1">Cache Management</a> and refresh cache types.', $url);
  85. return $message;
  86. }
  87. /**
  88. * Retrieve problem management url
  89. *
  90. * @return string|null
  91. */
  92. public function getLink()
  93. {
  94. return $this->_urlBuilder->getUrl('adminhtml/cache');
  95. }
  96. /**
  97. * Retrieve message severity
  98. *
  99. * @return int
  100. */
  101. public function getSeverity()
  102. {
  103. return \Magento\Framework\Notification\MessageInterface::SEVERITY_CRITICAL;
  104. }
  105. }