99
1010use Bloomreach \EngagementConnector \Model \ExportQueueModel ;
1111use Bloomreach \EngagementConnector \Model \ResourceModel \ExportQueue \CollectionFactory ;
12+ use Bloomreach \EngagementConnector \System \ConfigProvider ;
13+ use Magento \Framework \App \CacheInterface ;
1214use Magento \Framework \Notification \MessageInterface ;
1315use Magento \Framework \Phrase ;
1416
@@ -21,19 +23,38 @@ class ErrorsMessageNotification implements MessageInterface
2123 * Message identity
2224 */
2325 private const MESSAGE_IDENTITY = 'bloomreach_errors_system_notification ' ;
26+ private const HAS_ERRORS_CACHE_VALUE = 'has_errors ' ;
27+ private const NO_ERRORS_CACHE_VALUE = 'no_errors ' ;
28+ private const CACHE_LIFETIME = 600 ;
2429
2530 /**
2631 * @var CollectionFactory
2732 */
2833 private $ exportQueueCollectionFactory ;
2934
35+ /**
36+ * @var ConfigProvider
37+ */
38+ private $ configProvider ;
39+
40+ /**
41+ * @var CacheInterface
42+ */
43+ private $ cacheManagement ;
44+
3045 /**
3146 * @param CollectionFactory $exportQueueCollectionFactory
47+ * @param ConfigProvider $configProvider
48+ * @param CacheInterface $cacheManagement
3249 */
3350 public function __construct (
34- CollectionFactory $ exportQueueCollectionFactory
51+ CollectionFactory $ exportQueueCollectionFactory ,
52+ ConfigProvider $ configProvider ,
53+ CacheInterface $ cacheManagement
3554 ) {
55+ $ this ->configProvider = $ configProvider ;
3656 $ this ->exportQueueCollectionFactory = $ exportQueueCollectionFactory ;
57+ $ this ->cacheManagement = $ cacheManagement ;
3758 }
3859
3960 /**
@@ -53,7 +74,9 @@ public function getIdentity(): string
5374 */
5475 public function isDisplayed (): bool
5576 {
56- return $ this ->isNeedToShow ();
77+ return $ this ->configProvider ->isEnabled ()
78+ && $ this ->configProvider ->isSystemNotificationEnabled ()
79+ && $ this ->isNeedToShow ();
5780 }
5881
5982 /**
@@ -84,10 +107,35 @@ public function getSeverity(): int
84107
85108 /**
86109 * Show the error message if need it
110+ * - save data to cache
87111 *
88112 * @return bool
89113 */
90114 private function isNeedToShow (): bool
115+ {
116+ $ result = $ this ->cacheManagement ->load ($ this ->getIdentity ());
117+
118+ if (is_string ($ result ) && $ result ) {
119+ return $ result === self ::HAS_ERRORS_CACHE_VALUE ;
120+ }
121+
122+ $ hasErrors = $ this ->hasRecentErrorRecords ();
123+ $ this ->cacheManagement ->save (
124+ $ hasErrors ? self ::HAS_ERRORS_CACHE_VALUE : self ::NO_ERRORS_CACHE_VALUE ,
125+ $ this ->getIdentity (),
126+ ['BLOCK_HTML ' ],
127+ self ::CACHE_LIFETIME
128+ );
129+
130+ return $ hasErrors ;
131+ }
132+
133+ /**
134+ * Has recent error records
135+ *
136+ * @return bool
137+ */
138+ private function hasRecentErrorRecords (): bool
91139 {
92140 $ periodOfTime = date ('Y-m-d H:i:s ' , strtotime ('-24 hours ' ));
93141 return (bool ) $ this ->exportQueueCollectionFactory ->create ()
0 commit comments