6
6
*/
7
7
namespace Magento \AdminNotification \Controller \Adminhtml \Notification ;
8
8
9
+ use Magento \Backend \App \Action ;
10
+ use Magento \Framework \Controller \ResultFactory ;
11
+
9
12
class AjaxMarkAsRead extends \Magento \AdminNotification \Controller \Adminhtml \Notification
10
13
{
14
+ /**
15
+ * @var \Magento\AdminNotification\Model\NotificationService
16
+ */
17
+ private $ notificationService ;
18
+
19
+ /**
20
+ * @param Action\Context $context
21
+ * @param \Magento\AdminNotification\Model\NotificationService|null $notificationService
22
+ * @throws \RuntimeException
23
+ */
24
+ public function __construct (
25
+ Action \Context $ context ,
26
+ \Magento \AdminNotification \Model \NotificationService $ notificationService = null
27
+ ) {
28
+ parent ::__construct ($ context );
29
+ $ this ->notificationService = $ notificationService ?: \Magento \Framework \App \ObjectManager::getInstance ()
30
+ ->get (\Magento \AdminNotification \Model \NotificationService::class);
31
+ }
32
+
11
33
/**
12
34
* Mark notification as read (AJAX action)
13
35
*
14
- * @return void
36
+ * @return \Magento\Framework\Controller\Result\Json|void
37
+ * @throws \InvalidArgumentException
15
38
*/
16
39
public function execute ()
17
40
{
@@ -21,17 +44,15 @@ public function execute()
21
44
$ notificationId = (int )$ this ->getRequest ()->getPost ('id ' );
22
45
$ responseData = [];
23
46
try {
24
- $ this ->_objectManager ->create (
25
- \Magento \AdminNotification \Model \NotificationService::class
26
- )->markAsRead (
27
- $ notificationId
28
- );
47
+ $ this ->notificationService ->markAsRead ($ notificationId );
29
48
$ responseData ['success ' ] = true ;
30
49
} catch (\Exception $ e ) {
31
50
$ responseData ['success ' ] = false ;
32
51
}
33
- $ this ->getResponse ()->representJson (
34
- $ this ->_objectManager ->create (\Magento \Framework \Json \Helper \Data::class)->jsonEncode ($ responseData )
35
- );
52
+
53
+ /** @var \Magento\Framework\Controller\Result\Json $resultJson */
54
+ $ resultJson = $ this ->resultFactory ->create (ResultFactory::TYPE_JSON );
55
+ $ resultJson ->setData ($ responseData );
56
+ return $ resultJson ;
36
57
}
37
58
}
0 commit comments