AjaxMarkAsRead.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\AdminNotification\Controller\Adminhtml\Notification;
  8. use Magento\Backend\App\Action;
  9. use Magento\Framework\Controller\ResultFactory;
  10. class AjaxMarkAsRead extends \Magento\AdminNotification\Controller\Adminhtml\Notification
  11. {
  12. /**
  13. * @var \Magento\AdminNotification\Model\NotificationService
  14. */
  15. private $notificationService;
  16. /**
  17. * @param Action\Context $context
  18. * @param \Magento\AdminNotification\Model\NotificationService|null $notificationService
  19. * @throws \RuntimeException
  20. */
  21. public function __construct(
  22. Action\Context $context,
  23. \Magento\AdminNotification\Model\NotificationService $notificationService = null
  24. ) {
  25. parent::__construct($context);
  26. $this->notificationService = $notificationService?: \Magento\Framework\App\ObjectManager::getInstance()
  27. ->get(\Magento\AdminNotification\Model\NotificationService::class);
  28. }
  29. /**
  30. * Mark notification as read (AJAX action)
  31. *
  32. * @return \Magento\Framework\Controller\Result\Json|void
  33. * @throws \InvalidArgumentException
  34. */
  35. public function execute()
  36. {
  37. if (!$this->getRequest()->getPostValue()) {
  38. return;
  39. }
  40. $notificationId = (int)$this->getRequest()->getPost('id');
  41. $responseData = [];
  42. try {
  43. $this->notificationService->markAsRead($notificationId);
  44. $responseData['success'] = true;
  45. } catch (\Exception $e) {
  46. $responseData['success'] = false;
  47. }
  48. /** @var \Magento\Framework\Controller\Result\Json $resultJson */
  49. $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
  50. $resultJson->setData($responseData);
  51. return $resultJson;
  52. }
  53. }