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 3 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
59 changes: 31 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,54 @@
*/
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;

/**
* AdminNotification Messages class
*/
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 +71,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 +90,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 +100,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
@@ -1,33 +1,34 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
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\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\ResultFactory;

class AjaxMarkAsRead extends \Magento\AdminNotification\Controller\Adminhtml\Notification
/**
* AdminNotification AjaxMarkAsRead controller
*/
class AjaxMarkAsRead extends Notification implements HttpPostActionInterface
{
/**
* @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
@@ -1,12 +1,20 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
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\App\Action\HttpGetActionInterface;
use Magento\Framework\Exception\LocalizedException;

/**
* AdminNotification MarkAsRead controller
*/
class MarkAsRead extends Notification implements HttpGetActionInterface
{
/**
* Authorization level of a basic admin session
Expand All @@ -16,20 +24,33 @@ class MarkAsRead extends \Magento\AdminNotification\Controller\Adminhtml\Notific
const ADMIN_RESOURCE = 'Magento_AdminNotification::mark_as_read';

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

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

/**
* @inheritdoc
*
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface
*/
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 All @@ -38,9 +59,8 @@ public function execute()
);
}

$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl($this->getUrl('*')));
return;
return $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl($this->getUrl('*')));
}
$this->_redirect('adminhtml/*/');
return $this->_redirect('adminhtml/*/');
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
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;
use Magento\Framework\App\Action\HttpPostActionInterface;

/**
* AdminNotification MassMarkAsRead controller
*/
class MassMarkAsRead extends Notification implements HttpPostActionInterface
{
/**
* Authorization level of a basic admin session
*
Expand All @@ -17,7 +23,24 @@ class MassMarkAsRead extends \Magento\AdminNotification\Controller\Adminhtml\Not
const ADMIN_RESOURCE = 'Magento_AdminNotification::mark_as_read';

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

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

/**
* @inheritdoc
*
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@return is not required if there already @inheritdoc

*/
public function execute()
{
Expand All @@ -27,7 +50,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 All @@ -44,6 +67,6 @@ public function execute()
);
}
}
$this->_redirect('adminhtml/*/');
return $this->_redirect('adminhtml/*/');
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
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;
use Magento\Framework\App\Action\HttpPostActionInterface;

/**
* AdminNotification MassRemove controller
*/
class MassRemove extends Notification implements HttpPostActionInterface
{

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

/**
* @return void
* @param Action\Context $context
* @param InboxModelFactory $inboxModelFactory
*/
public function __construct(Action\Context $context, InboxModelFactory $inboxModelFactory)
{
parent::__construct($context);
$this->inboxModelFactory = $inboxModelFactory;
}

/**
* @inheritdoc
*
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

*/
public function execute()
{
Expand All @@ -27,7 +50,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 All @@ -42,6 +65,6 @@ public function execute()
);
}
}
$this->_redirect('adminhtml/*/');
return $this->_redirect('adminhtml/*/');
}
}
Loading