Skip to content

Commit d359cdf

Browse files
committed
ObjectManager cleanup - Remove usage from AdminNotification module
1 parent a1fad2b commit d359cdf

File tree

6 files changed

+102
-51
lines changed

6 files changed

+102
-51
lines changed

app/code/Magento/AdminNotification/Block/System/Messages.php

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,51 @@
55
*/
66
namespace Magento\AdminNotification\Block\System;
77

8-
class Messages extends \Magento\Backend\Block\Template
8+
use Magento\AdminNotification\Model\ResourceModel\System\Message\Collection\Synchronized;
9+
use Magento\Backend\Block\Template;
10+
use Magento\Backend\Block\Template\Context as TemplateContext;
11+
use Magento\Framework\Json\Helper\Data as JsonDataHelper;
12+
use Magento\Framework\Notification\MessageInterface;
13+
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
14+
15+
class Messages extends Template
916
{
1017
/**
11-
* Message list
18+
* Synchronized Message collection
1219
*
13-
* @var \Magento\AdminNotification\Model\ResourceModel\System\Message\Collection\Synchronized
20+
* @var Synchronized
1421
*/
1522
protected $_messages;
1623

1724
/**
18-
* @var \Magento\Framework\Json\Helper\Data
25+
* @var JsonDataHelper
1926
* @deprecated
2027
*/
2128
protected $jsonHelper;
2229

2330
/**
24-
* @var \Magento\Framework\Serialize\Serializer\Json
31+
* @var JsonSerializer
2532
*/
2633
private $serializer;
2734

2835
/**
29-
* @param \Magento\Backend\Block\Template\Context $context
30-
* @param \Magento\AdminNotification\Model\ResourceModel\System\Message\Collection\Synchronized $messages
31-
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
36+
* @param TemplateContext $context
37+
* @param Synchronized $messages
38+
* @param JsonDataHelper $jsonHelper
39+
* @param JsonSerializer $serializer
3240
* @param array $data
33-
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
3441
*/
3542
public function __construct(
36-
\Magento\Backend\Block\Template\Context $context,
37-
\Magento\AdminNotification\Model\ResourceModel\System\Message\Collection\Synchronized $messages,
38-
\Magento\Framework\Json\Helper\Data $jsonHelper,
39-
array $data = [],
40-
\Magento\Framework\Serialize\Serializer\Json $serializer = null
43+
TemplateContext $context,
44+
Synchronized $messages,
45+
JsonDataHelper $jsonHelper,
46+
JsonSerializer $serializer,
47+
array $data = []
4148
) {
4249
$this->jsonHelper = $jsonHelper;
4350
parent::__construct($context, $data);
4451
$this->_messages = $messages;
45-
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
46-
->get(\Magento\Framework\Serialize\Serializer\Json::class);
52+
$this->serializer = $serializer;
4753
}
4854

4955
/**
@@ -62,15 +68,13 @@ protected function _toHtml()
6268
/**
6369
* Retrieve message list
6470
*
65-
* @return \Magento\Framework\Notification\MessageInterface[]
71+
* @return MessageInterface[]|null
6672
*/
6773
public function getLastCritical()
6874
{
6975
$items = array_values($this->_messages->getItems());
70-
if (isset(
71-
$items[0]
72-
) && $items[0]->getSeverity() == \Magento\Framework\Notification\MessageInterface::SEVERITY_CRITICAL
73-
) {
76+
77+
if (isset($items[0]) && (int)$items[0]->getSeverity() === MessageInterface::SEVERITY_CRITICAL) {
7478
return $items[0];
7579
}
7680
return null;
@@ -83,9 +87,7 @@ public function getLastCritical()
8387
*/
8488
public function getCriticalCount()
8589
{
86-
return $this->_messages->getCountBySeverity(
87-
\Magento\Framework\Notification\MessageInterface::SEVERITY_CRITICAL
88-
);
90+
return $this->_messages->getCountBySeverity(MessageInterface::SEVERITY_CRITICAL);
8991
}
9092

9193
/**
@@ -95,9 +97,7 @@ public function getCriticalCount()
9597
*/
9698
public function getMajorCount()
9799
{
98-
return $this->_messages->getCountBySeverity(
99-
\Magento\Framework\Notification\MessageInterface::SEVERITY_MAJOR
100-
);
100+
return $this->_messages->getCountBySeverity(MessageInterface::SEVERITY_MAJOR);
101101
}
102102

103103
/**

app/code/Magento/AdminNotification/Controller/Adminhtml/Notification/AjaxMarkAsRead.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,26 @@
66
*/
77
namespace Magento\AdminNotification\Controller\Adminhtml\Notification;
88

9+
use Magento\AdminNotification\Controller\Adminhtml\Notification;
10+
use Magento\AdminNotification\Model\NotificationService;
911
use Magento\Backend\App\Action;
1012
use Magento\Framework\Controller\ResultFactory;
1113

12-
class AjaxMarkAsRead extends \Magento\AdminNotification\Controller\Adminhtml\Notification
14+
class AjaxMarkAsRead extends Notification
1315
{
1416
/**
15-
* @var \Magento\AdminNotification\Model\NotificationService
17+
* @var NotificationService
1618
*/
1719
private $notificationService;
1820

1921
/**
2022
* @param Action\Context $context
21-
* @param \Magento\AdminNotification\Model\NotificationService|null $notificationService
22-
* @throws \RuntimeException
23+
* @param NotificationService $notificationService
2324
*/
24-
public function __construct(
25-
Action\Context $context,
26-
\Magento\AdminNotification\Model\NotificationService $notificationService = null
27-
) {
25+
public function __construct(Action\Context $context, NotificationService $notificationService)
26+
{
2827
parent::__construct($context);
29-
$this->notificationService = $notificationService?: \Magento\Framework\App\ObjectManager::getInstance()
30-
->get(\Magento\AdminNotification\Model\NotificationService::class);
28+
$this->notificationService = $notificationService;
3129
}
3230

3331
/**

app/code/Magento/AdminNotification/Controller/Adminhtml/Notification/MarkAsRead.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
*/
77
namespace Magento\AdminNotification\Controller\Adminhtml\Notification;
88

9-
class MarkAsRead extends \Magento\AdminNotification\Controller\Adminhtml\Notification
9+
use Magento\AdminNotification\Controller\Adminhtml\Notification;
10+
use Magento\AdminNotification\Model\NotificationService;
11+
use Magento\Backend\App\Action;
12+
13+
class MarkAsRead extends Notification
1014
{
1115
/**
1216
* Authorization level of a basic admin session
@@ -15,6 +19,17 @@ class MarkAsRead extends \Magento\AdminNotification\Controller\Adminhtml\Notific
1519
*/
1620
const ADMIN_RESOURCE = 'Magento_AdminNotification::mark_as_read';
1721

22+
/**
23+
* @var NotificationService
24+
*/
25+
private $notificationService;
26+
27+
public function __construct(Action\Context $context, NotificationService $notificationService)
28+
{
29+
parent::__construct($context);
30+
$this->notificationService = $notificationService;
31+
}
32+
1833
/**
1934
* @return void
2035
*/
@@ -23,11 +38,7 @@ public function execute()
2338
$notificationId = (int)$this->getRequest()->getParam('id');
2439
if ($notificationId) {
2540
try {
26-
$this->_objectManager->create(
27-
\Magento\AdminNotification\Model\NotificationService::class
28-
)->markAsRead(
29-
$notificationId
30-
);
41+
$this->notificationService->markAsRead($notificationId);
3142
$this->messageManager->addSuccessMessage(__('The message has been marked as Read.'));
3243
} catch (\Magento\Framework\Exception\LocalizedException $e) {
3344
$this->messageManager->addErrorMessage($e->getMessage());

app/code/Magento/AdminNotification/Controller/Adminhtml/Notification/MassMarkAsRead.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,30 @@
66
*/
77
namespace Magento\AdminNotification\Controller\Adminhtml\Notification;
88

9-
class MassMarkAsRead extends \Magento\AdminNotification\Controller\Adminhtml\Notification
10-
{
9+
use Magento\AdminNotification\Controller\Adminhtml\Notification;
10+
use Magento\AdminNotification\Model\InboxFactory as InboxModelFactory;
11+
use Magento\Backend\App\Action;
1112

13+
class MassMarkAsRead extends Notification
14+
{
1215
/**
1316
* Authorization level of a basic admin session
1417
*
1518
* @see _isAllowed()
1619
*/
1720
const ADMIN_RESOURCE = 'Magento_AdminNotification::mark_as_read';
1821

22+
/**
23+
* @var InboxModelFactory
24+
*/
25+
private $inboxModelFactory;
26+
27+
public function __construct(Action\Context $context, InboxModelFactory $inboxModelFactory)
28+
{
29+
parent::__construct($context);
30+
$this->inboxModelFactory = $inboxModelFactory;
31+
}
32+
1933
/**
2034
* @return void
2135
*/
@@ -27,7 +41,7 @@ public function execute()
2741
} else {
2842
try {
2943
foreach ($ids as $id) {
30-
$model = $this->_objectManager->create(\Magento\AdminNotification\Model\Inbox::class)->load($id);
44+
$model = $this->inboxModelFactory->create()->load($id);
3145
if ($model->getId()) {
3246
$model->setIsRead(1)->save();
3347
}

app/code/Magento/AdminNotification/Controller/Adminhtml/Notification/MassRemove.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
*/
77
namespace Magento\AdminNotification\Controller\Adminhtml\Notification;
88

9-
class MassRemove extends \Magento\AdminNotification\Controller\Adminhtml\Notification
9+
use Magento\AdminNotification\Controller\Adminhtml\Notification;
10+
use Magento\AdminNotification\Model\InboxFactory as InboxModelFactory;
11+
use Magento\Backend\App\Action;
12+
13+
class MassRemove extends Notification
1014
{
1115

1216
/**
@@ -15,6 +19,16 @@ class MassRemove extends \Magento\AdminNotification\Controller\Adminhtml\Notific
1519
* @see _isAllowed()
1620
*/
1721
const ADMIN_RESOURCE = 'Magento_AdminNotification::adminnotification_remove';
22+
/**
23+
* @var InboxModelFactory
24+
*/
25+
private $inboxModelFactory;
26+
27+
public function __construct(Action\Context $context, InboxModelFactory $inboxModelFactory)
28+
{
29+
parent::__construct($context);
30+
$this->inboxModelFactory = $inboxModelFactory;
31+
}
1832

1933
/**
2034
* @return void
@@ -27,7 +41,7 @@ public function execute()
2741
} else {
2842
try {
2943
foreach ($ids as $id) {
30-
$model = $this->_objectManager->create(\Magento\AdminNotification\Model\Inbox::class)->load($id);
44+
$model = $this->inboxModelFactory->create()->load($id);
3145
if ($model->getId()) {
3246
$model->setIsRemove(1)->save();
3347
}

app/code/Magento/AdminNotification/Controller/Adminhtml/Notification/Remove.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,37 @@
66
*/
77
namespace Magento\AdminNotification\Controller\Adminhtml\Notification;
88

9-
class Remove extends \Magento\AdminNotification\Controller\Adminhtml\Notification
10-
{
9+
use Magento\AdminNotification\Controller\Adminhtml\Notification;
10+
use Magento\AdminNotification\Model\InboxFactory as InboxModelFactory;
11+
use Magento\Backend\App\Action;
1112

13+
class Remove extends Notification
14+
{
1215
/**
1316
* Authorization level of a basic admin session
1417
*
1518
* @see _isAllowed()
1619
*/
1720
const ADMIN_RESOURCE = 'Magento_AdminNotification::adminnotification_remove';
1821

22+
/**
23+
* @var InboxModelFactory
24+
*/
25+
private $inboxModelFactory;
26+
27+
public function __construct(Action\Context $context, InboxModelFactory $inboxModelFactory)
28+
{
29+
parent::__construct($context);
30+
$this->inboxModelFactory = $inboxModelFactory;
31+
}
32+
1933
/**
2034
* @return void
2135
*/
2236
public function execute()
2337
{
2438
if ($id = $this->getRequest()->getParam('id')) {
25-
$model = $this->_objectManager->create(\Magento\AdminNotification\Model\Inbox::class)->load($id);
39+
$model = $this->inboxModelFactory->create()->load($id);
2640

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

0 commit comments

Comments
 (0)