diff --git a/app/code/Magento/AdminNotification/Controller/Adminhtml/Notification/AjaxMarkAsRead.php b/app/code/Magento/AdminNotification/Controller/Adminhtml/Notification/AjaxMarkAsRead.php index 31a129d57ab90..da797fe12e75a 100644 --- a/app/code/Magento/AdminNotification/Controller/Adminhtml/Notification/AjaxMarkAsRead.php +++ b/app/code/Magento/AdminNotification/Controller/Adminhtml/Notification/AjaxMarkAsRead.php @@ -6,12 +6,35 @@ */ namespace Magento\AdminNotification\Controller\Adminhtml\Notification; +use Magento\Backend\App\Action; +use Magento\Framework\Controller\ResultFactory; + class AjaxMarkAsRead extends \Magento\AdminNotification\Controller\Adminhtml\Notification { + /** + * @var \Magento\AdminNotification\Model\NotificationService + */ + private $notificationService; + + /** + * @param Action\Context $context + * @param \Magento\AdminNotification\Model\NotificationService|null $notificationService + * @throws \RuntimeException + */ + public function __construct( + Action\Context $context, + \Magento\AdminNotification\Model\NotificationService $notificationService = null + ) { + parent::__construct($context); + $this->notificationService = $notificationService?: \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\AdminNotification\Model\NotificationService::class); + } + /** * Mark notification as read (AJAX action) * - * @return void + * @return \Magento\Framework\Controller\Result\Json|void + * @throws \InvalidArgumentException */ public function execute() { @@ -21,17 +44,15 @@ public function execute() $notificationId = (int)$this->getRequest()->getPost('id'); $responseData = []; try { - $this->_objectManager->create( - \Magento\AdminNotification\Model\NotificationService::class - )->markAsRead( - $notificationId - ); + $this->notificationService->markAsRead($notificationId); $responseData['success'] = true; } catch (\Exception $e) { $responseData['success'] = false; } - $this->getResponse()->representJson( - $this->_objectManager->create(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($responseData) - ); + + /** @var \Magento\Framework\Controller\Result\Json $resultJson */ + $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON); + $resultJson->setData($responseData); + return $resultJson; } }