Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ObjectManager cleanup - Remove usage from AdminNotification module #26939

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 28 additions & 28 deletions app/code/Magento/AdminNotification/Block/System/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,51 @@
*/
namespace Magento\AdminNotification\Block\System;

class Messages extends \Magento\Backend\Block\Template
use Magento\AdminNotification\Model\ResourceModel\System\Message\Collection\Synchronized;
use Magento\Backend\Block\Template;
use Magento\Backend\Block\Template\Context as TemplateContext;
use Magento\Framework\Json\Helper\Data as JsonDataHelper;
use Magento\Framework\Notification\MessageInterface;
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;

class Messages extends Template
{
/**
* Message list
* Synchronized Message collection
*
* @var \Magento\AdminNotification\Model\ResourceModel\System\Message\Collection\Synchronized
* @var Synchronized
*/
protected $_messages;

/**
* @var \Magento\Framework\Json\Helper\Data
* @var JsonDataHelper
* @deprecated
*/
protected $jsonHelper;

/**
* @var \Magento\Framework\Serialize\Serializer\Json
* @var JsonSerializer
*/
private $serializer;

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\AdminNotification\Model\ResourceModel\System\Message\Collection\Synchronized $messages
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
* @param TemplateContext $context
* @param Synchronized $messages
* @param JsonDataHelper $jsonHelper
* @param JsonSerializer $serializer
* @param array $data
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\AdminNotification\Model\ResourceModel\System\Message\Collection\Synchronized $messages,
\Magento\Framework\Json\Helper\Data $jsonHelper,
array $data = [],
\Magento\Framework\Serialize\Serializer\Json $serializer = null
TemplateContext $context,
Synchronized $messages,
JsonDataHelper $jsonHelper,
JsonSerializer $serializer,
ihor-sviziev marked this conversation as resolved.
Show resolved Hide resolved
array $data = []
) {
$this->jsonHelper = $jsonHelper;
parent::__construct($context, $data);
$this->_messages = $messages;
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
$this->serializer = $serializer;
}

/**
Expand All @@ -62,16 +68,14 @@ protected function _toHtml()
/**
* Retrieve message list
*
* @return \Magento\Framework\Notification\MessageInterface[]
* @return MessageInterface[]|null
*/
public function getLastCritical()
{
$items = array_values($this->_messages->getItems());
if (isset(
$items[0]
) && $items[0]->getSeverity() == \Magento\Framework\Notification\MessageInterface::SEVERITY_CRITICAL
) {
return $items[0];

if (!empty($items) && current($items)->getSeverity() === MessageInterface::SEVERITY_CRITICAL) {
return current($items);
}
return null;
}
Expand All @@ -83,9 +87,7 @@ public function getLastCritical()
*/
public function getCriticalCount()
{
return $this->_messages->getCountBySeverity(
\Magento\Framework\Notification\MessageInterface::SEVERITY_CRITICAL
);
return $this->_messages->getCountBySeverity(MessageInterface::SEVERITY_CRITICAL);
}

/**
Expand All @@ -95,9 +97,7 @@ public function getCriticalCount()
*/
public function getMajorCount()
{
return $this->_messages->getCountBySeverity(
\Magento\Framework\Notification\MessageInterface::SEVERITY_MAJOR
);
return $this->_messages->getCountBySeverity(MessageInterface::SEVERITY_MAJOR);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,26 @@
*/
namespace Magento\AdminNotification\Controller\Adminhtml\Notification;

use Magento\AdminNotification\Controller\Adminhtml\Notification;
use Magento\AdminNotification\Model\NotificationService;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\ResultFactory;

class AjaxMarkAsRead extends \Magento\AdminNotification\Controller\Adminhtml\Notification
class AjaxMarkAsRead extends Notification
{
/**
* @var \Magento\AdminNotification\Model\NotificationService
* @var NotificationService
*/
private $notificationService;

/**
* @param Action\Context $context
* @param \Magento\AdminNotification\Model\NotificationService|null $notificationService
* @throws \RuntimeException
* @param NotificationService $notificationService
*/
public function __construct(
Action\Context $context,
\Magento\AdminNotification\Model\NotificationService $notificationService = null
) {
public function __construct(Action\Context $context, NotificationService $notificationService)
{
parent::__construct($context);
$this->notificationService = $notificationService?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\AdminNotification\Model\NotificationService::class);
$this->notificationService = $notificationService;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
*/
namespace Magento\AdminNotification\Controller\Adminhtml\Notification;

class MarkAsRead extends \Magento\AdminNotification\Controller\Adminhtml\Notification
use Magento\AdminNotification\Controller\Adminhtml\Notification;
use Magento\AdminNotification\Model\NotificationService;
use Magento\Backend\App\Action;
use Magento\Framework\Exception\LocalizedException;

class MarkAsRead extends Notification
{
/**
* Authorization level of a basic admin session
Expand All @@ -15,6 +20,17 @@ class MarkAsRead extends \Magento\AdminNotification\Controller\Adminhtml\Notific
*/
const ADMIN_RESOURCE = 'Magento_AdminNotification::mark_as_read';

/**
* @var NotificationService
*/
private $notificationService;

public function __construct(Action\Context $context, NotificationService $notificationService)
{
parent::__construct($context);
$this->notificationService = $notificationService;
}

/**
* @return void
*/
Expand All @@ -23,13 +39,9 @@ public function execute()
$notificationId = (int)$this->getRequest()->getParam('id');
if ($notificationId) {
try {
$this->_objectManager->create(
\Magento\AdminNotification\Model\NotificationService::class
)->markAsRead(
$notificationId
);
$this->notificationService->markAsRead($notificationId);
$this->messageManager->addSuccessMessage(__('The message has been marked as Read.'));
} catch (\Magento\Framework\Exception\LocalizedException $e) {
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addExceptionMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,30 @@
*/
namespace Magento\AdminNotification\Controller\Adminhtml\Notification;

class MassMarkAsRead extends \Magento\AdminNotification\Controller\Adminhtml\Notification
{
use Magento\AdminNotification\Controller\Adminhtml\Notification;
use Magento\AdminNotification\Model\InboxFactory as InboxModelFactory;
use Magento\Backend\App\Action;

class MassMarkAsRead extends Notification
{
/**
* Authorization level of a basic admin session
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_AdminNotification::mark_as_read';

/**
* @var InboxModelFactory
*/
private $inboxModelFactory;

public function __construct(Action\Context $context, InboxModelFactory $inboxModelFactory)
{
parent::__construct($context);
$this->inboxModelFactory = $inboxModelFactory;
}

/**
* @return void
*/
Expand All @@ -27,7 +41,7 @@ public function execute()
} else {
try {
foreach ($ids as $id) {
$model = $this->_objectManager->create(\Magento\AdminNotification\Model\Inbox::class)->load($id);
$model = $this->inboxModelFactory->create()->load($id);
if ($model->getId()) {
$model->setIsRead(1)->save();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
*/
namespace Magento\AdminNotification\Controller\Adminhtml\Notification;

class MassRemove extends \Magento\AdminNotification\Controller\Adminhtml\Notification
use Magento\AdminNotification\Controller\Adminhtml\Notification;
use Magento\AdminNotification\Model\InboxFactory as InboxModelFactory;
use Magento\Backend\App\Action;

class MassRemove extends Notification
{

/**
Expand All @@ -15,6 +19,16 @@ class MassRemove extends \Magento\AdminNotification\Controller\Adminhtml\Notific
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_AdminNotification::adminnotification_remove';
/**
* @var InboxModelFactory
*/
private $inboxModelFactory;

public function __construct(Action\Context $context, InboxModelFactory $inboxModelFactory)
{
parent::__construct($context);
$this->inboxModelFactory = $inboxModelFactory;
}

/**
* @return void
Expand All @@ -27,7 +41,7 @@ public function execute()
} else {
try {
foreach ($ids as $id) {
$model = $this->_objectManager->create(\Magento\AdminNotification\Model\Inbox::class)->load($id);
$model = $this->inboxModelFactory->create()->load($id);
if ($model->getId()) {
$model->setIsRemove(1)->save();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,37 @@
*/
namespace Magento\AdminNotification\Controller\Adminhtml\Notification;

class Remove extends \Magento\AdminNotification\Controller\Adminhtml\Notification
{
use Magento\AdminNotification\Controller\Adminhtml\Notification;
use Magento\AdminNotification\Model\InboxFactory as InboxModelFactory;
use Magento\Backend\App\Action;

class Remove extends Notification
{
/**
* Authorization level of a basic admin session
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_AdminNotification::adminnotification_remove';

/**
* @var InboxModelFactory
*/
private $inboxModelFactory;

public function __construct(Action\Context $context, InboxModelFactory $inboxModelFactory)
{
parent::__construct($context);
$this->inboxModelFactory = $inboxModelFactory;
}

/**
* @return void
*/
public function execute()
{
if ($id = $this->getRequest()->getParam('id')) {
$model = $this->_objectManager->create(\Magento\AdminNotification\Model\Inbox::class)->load($id);
$model = $this->inboxModelFactory->create()->load($id);

if (!$model->getId()) {
$this->_redirect('adminhtml/*/');
Expand Down