1+ <?php
2+ /**
3+ * Copyright © 2017 Magefan (support@magefan.com). All rights reserved.
4+ * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+ *
6+ * Glory to Ukraine! Glory to the heroes!
7+ */
8+
9+ namespace Magefan \Notifications \Observer \Controller ;
10+
11+ class ActionPredispatch implements \Magento \Framework \Event \ObserverInterface
12+ {
13+ /**
14+ * Check every 10 min
15+ */
16+ const TIMEOUT = 600 ;
17+
18+ /**
19+ * @var \Magento\Framework\App\Cache\TypeListInterface
20+ */
21+ private $ cacheTypeList ;
22+
23+ /**
24+ * @var \Magento\Framework\Message\ManagerInterface
25+ */
26+ private $ messageManager ;
27+
28+ /**
29+ * @var \Magento\Backend\Model\Auth\Session
30+ */
31+ private $ backendSession ;
32+
33+ /**
34+ * @var \Magento\Framework\Stdlib\DateTime\DateTime
35+ */
36+ private $ date ;
37+
38+ /**
39+ * @var \Magento\Framework\UrlInterface
40+ */
41+ private $ url ;
42+
43+ /**
44+ * @var \Magento\Review\Model\ResourceModel\Review\CollectionFactory
45+ */
46+ protected $ reviewCollectionFactory ;
47+
48+ /**
49+ * Initialization
50+ * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
51+ * @param \Magento\Framework\Message\ManagerInterface $messageManager
52+ * @param \Magento\Backend\Model\Auth\Session $backendSession
53+ * @param \Magento\Framework\Stdlib\DateTime\DateTime $date,
54+ * @param \Magento\Framework\UrlInterface $url
55+ * @param \Magento\Review\Model\ResourceModel\Review\CollectionFactory $reviewCollectionFactory
56+ */
57+ public function __construct (
58+ \Magento \Framework \App \Cache \TypeListInterface $ cacheTypeList ,
59+ \Magento \Framework \Message \ManagerInterface $ messageManager ,
60+ \Magento \Backend \Model \Auth \Session $ backendSession ,
61+ \Magento \Framework \Stdlib \DateTime \DateTime $ date ,
62+ \Magento \Framework \UrlInterface $ url ,
63+ \Magento \Review \Model \ResourceModel \Review \CollectionFactory $ reviewCollectionFactory
64+ ) {
65+ $ this ->cacheTypeList = $ cacheTypeList ;
66+ $ this ->messageManager = $ messageManager ;
67+ $ this ->backendSession = $ backendSession ;
68+ $ this ->date = $ date ;
69+ $ this ->url = $ url ;
70+ $ this ->reviewCollectionFactory = $ reviewCollectionFactory ;
71+ }
72+
73+ /**
74+ * Execute observer
75+ *
76+ * @param \Magento\Framework\Event\Observer $observer
77+ * @return void
78+ */
79+ public function execute (
80+ \Magento \Framework \Event \Observer $ observer
81+ ) {
82+ if (!$ this ->backendSession ->isLoggedIn ()) {
83+ return ; // Isn't logged in
84+ }
85+
86+ if ($ observer ->getRequest ()->isXmlHttpRequest ()) {
87+ return ; // It's ajax request
88+ }
89+
90+ $ time = $ this ->date ->gmtTimestamp ();
91+ if ($ this ->backendSession ->getLastMfNtfCheck () > $ time - self ::TIMEOUT ) {
92+ return ; // It's not time
93+ }
94+
95+ $ this ->checkCacheTypes ();
96+ $ this ->checkReviews ();
97+
98+ $ this ->backendSession ->setLastMfNtfCheck ($ time );
99+ }
100+
101+ /**
102+ * Check if cache types are enabled
103+ * @return void
104+ */
105+ protected function checkCacheTypes ()
106+ {
107+ $ disabled = [];
108+ foreach ($ this ->cacheTypeList ->getTypes () as $ cacheType ) {
109+ if (!$ cacheType ->getStatus ()) {
110+ $ disabled [] = $ cacheType ->getCacheType ();
111+ }
112+ }
113+
114+ if (count ($ disabled )) {
115+ $ this ->messageManager ->addNotice (
116+ __ ('The following Cache Type(s) are disabled: %1. <a href="%2">Manage Cache</a>. ' ,
117+ implode (', ' , $ disabled ),
118+ $ this ->url ->getUrl ('adminhtml/cache ' )
119+ )
120+ );
121+ }
122+ }
123+
124+ /**
125+ * Check if any pending review exists
126+ * @return void
127+ */
128+ protected function checkReviews ()
129+ {
130+ $ pendignReview = $ this ->reviewCollectionFactory ->create ()
131+ ->addFieldToFilter ('status_id ' , \Magento \Review \Model \Review::STATUS_PENDING )
132+ ->setPageSize (1 )
133+ ->getFirstItem ();
134+
135+ if ($ pendignReview ->getId ()) {
136+ $ this ->messageManager ->addNotice (
137+ __ ('Some customer reviews are pending for approval. <a href="%1">Manage Reviews</a>. ' ,
138+ $ this ->url ->getUrl ('review/product/index ' )
139+ )
140+ );
141+ }
142+ }
143+ }
0 commit comments