Skip to content

Commit 480f8ab

Browse files
ENGCOM-6942: ObjectManager cleanup - Remove usage from AdminNotification module #26939
- Merge Pull Request #26939 from ihor-sviziev/magento2:cleanup-objectmanager-fallback - Merged commits: 1. d359cdf 2. 744b0f7 3. 2598994 4. 57af3d7 5. 6ec7be9 6. b7c1d04
2 parents 5a9fcab + b7c1d04 commit 480f8ab

File tree

8 files changed

+161
-72
lines changed

8 files changed

+161
-72
lines changed

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

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,54 @@
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+
/**
16+
* AdminNotification Messages class
17+
*/
18+
class Messages extends Template
919
{
1020
/**
11-
* Message list
21+
* Synchronized Message collection
1222
*
13-
* @var \Magento\AdminNotification\Model\ResourceModel\System\Message\Collection\Synchronized
23+
* @var Synchronized
1424
*/
1525
protected $_messages;
1626

1727
/**
18-
* @var \Magento\Framework\Json\Helper\Data
28+
* @var JsonDataHelper
1929
* @deprecated
2030
*/
2131
protected $jsonHelper;
2232

2333
/**
24-
* @var \Magento\Framework\Serialize\Serializer\Json
34+
* @var JsonSerializer
2535
*/
2636
private $serializer;
2737

2838
/**
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
39+
* @param TemplateContext $context
40+
* @param Synchronized $messages
41+
* @param JsonDataHelper $jsonHelper
42+
* @param JsonSerializer $serializer
3243
* @param array $data
33-
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
3444
*/
3545
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
46+
TemplateContext $context,
47+
Synchronized $messages,
48+
JsonDataHelper $jsonHelper,
49+
JsonSerializer $serializer,
50+
array $data = []
4151
) {
4252
$this->jsonHelper = $jsonHelper;
4353
parent::__construct($context, $data);
4454
$this->_messages = $messages;
45-
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
46-
->get(\Magento\Framework\Serialize\Serializer\Json::class);
55+
$this->serializer = $serializer;
4756
}
4857

4958
/**
@@ -62,16 +71,14 @@ protected function _toHtml()
6271
/**
6372
* Retrieve message list
6473
*
65-
* @return \Magento\Framework\Notification\MessageInterface[]
74+
* @return MessageInterface[]|null
6675
*/
6776
public function getLastCritical()
6877
{
6978
$items = array_values($this->_messages->getItems());
70-
if (isset(
71-
$items[0]
72-
) && $items[0]->getSeverity() == \Magento\Framework\Notification\MessageInterface::SEVERITY_CRITICAL
73-
) {
74-
return $items[0];
79+
80+
if (!empty($items) && current($items)->getSeverity() === MessageInterface::SEVERITY_CRITICAL) {
81+
return current($items);
7582
}
7683
return null;
7784
}
@@ -83,9 +90,7 @@ public function getLastCritical()
8390
*/
8491
public function getCriticalCount()
8592
{
86-
return $this->_messages->getCountBySeverity(
87-
\Magento\Framework\Notification\MessageInterface::SEVERITY_CRITICAL
88-
);
93+
return $this->_messages->getCountBySeverity(MessageInterface::SEVERITY_CRITICAL);
8994
}
9095

9196
/**
@@ -95,9 +100,7 @@ public function getCriticalCount()
95100
*/
96101
public function getMajorCount()
97102
{
98-
return $this->_messages->getCountBySeverity(
99-
\Magento\Framework\Notification\MessageInterface::SEVERITY_MAJOR
100-
);
103+
return $this->_messages->getCountBySeverity(MessageInterface::SEVERITY_MAJOR);
101104
}
102105

103106
/**

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
namespace Magento\AdminNotification\Controller\Adminhtml\Notification;
87

8+
use Magento\AdminNotification\Controller\Adminhtml\Notification;
9+
use Magento\AdminNotification\Model\NotificationService;
910
use Magento\Backend\App\Action;
11+
use Magento\Framework\App\Action\HttpPostActionInterface;
1012
use Magento\Framework\Controller\ResultFactory;
1113

12-
class AjaxMarkAsRead extends \Magento\AdminNotification\Controller\Adminhtml\Notification
14+
/**
15+
* AdminNotification AjaxMarkAsRead controller
16+
*/
17+
class AjaxMarkAsRead extends Notification implements HttpPostActionInterface
1318
{
1419
/**
15-
* @var \Magento\AdminNotification\Model\NotificationService
20+
* @var NotificationService
1621
*/
1722
private $notificationService;
1823

1924
/**
2025
* @param Action\Context $context
21-
* @param \Magento\AdminNotification\Model\NotificationService|null $notificationService
22-
* @throws \RuntimeException
26+
* @param NotificationService $notificationService
2327
*/
24-
public function __construct(
25-
Action\Context $context,
26-
\Magento\AdminNotification\Model\NotificationService $notificationService = null
27-
) {
28+
public function __construct(Action\Context $context, NotificationService $notificationService)
29+
{
2830
parent::__construct($context);
29-
$this->notificationService = $notificationService?: \Magento\Framework\App\ObjectManager::getInstance()
30-
->get(\Magento\AdminNotification\Model\NotificationService::class);
31+
$this->notificationService = $notificationService;
3132
}
3233

3334
/**
Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
namespace Magento\AdminNotification\Controller\Adminhtml\Notification;
87

9-
class MarkAsRead extends \Magento\AdminNotification\Controller\Adminhtml\Notification
8+
use Magento\AdminNotification\Controller\Adminhtml\Notification;
9+
use Magento\AdminNotification\Model\NotificationService;
10+
use Magento\Backend\App\Action;
11+
use Magento\Framework\App\Action\HttpGetActionInterface;
12+
use Magento\Framework\Exception\LocalizedException;
13+
14+
/**
15+
* AdminNotification MarkAsRead controller
16+
*/
17+
class MarkAsRead extends Notification implements HttpGetActionInterface
1018
{
1119
/**
1220
* Authorization level of a basic admin session
@@ -16,20 +24,31 @@ class MarkAsRead extends \Magento\AdminNotification\Controller\Adminhtml\Notific
1624
const ADMIN_RESOURCE = 'Magento_AdminNotification::mark_as_read';
1725

1826
/**
19-
* @return void
27+
* @var NotificationService
28+
*/
29+
private $notificationService;
30+
31+
/**
32+
* @param Action\Context $context
33+
* @param NotificationService $notificationService
34+
*/
35+
public function __construct(Action\Context $context, NotificationService $notificationService)
36+
{
37+
parent::__construct($context);
38+
$this->notificationService = $notificationService;
39+
}
40+
41+
/**
42+
* @inheritdoc
2043
*/
2144
public function execute()
2245
{
2346
$notificationId = (int)$this->getRequest()->getParam('id');
2447
if ($notificationId) {
2548
try {
26-
$this->_objectManager->create(
27-
\Magento\AdminNotification\Model\NotificationService::class
28-
)->markAsRead(
29-
$notificationId
30-
);
49+
$this->notificationService->markAsRead($notificationId);
3150
$this->messageManager->addSuccessMessage(__('The message has been marked as Read.'));
32-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
51+
} catch (LocalizedException $e) {
3352
$this->messageManager->addErrorMessage($e->getMessage());
3453
} catch (\Exception $e) {
3554
$this->messageManager->addExceptionMessage(
@@ -38,9 +57,8 @@ public function execute()
3857
);
3958
}
4059

41-
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl($this->getUrl('*')));
42-
return;
60+
return $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl($this->getUrl('*')));
4361
}
44-
$this->_redirect('adminhtml/*/');
62+
return $this->_redirect('adminhtml/*/');
4563
}
4664
}

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
namespace Magento\AdminNotification\Controller\Adminhtml\Notification;
87

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

13+
/**
14+
* AdminNotification MassMarkAsRead controller
15+
*/
16+
class MassMarkAsRead extends Notification implements HttpPostActionInterface
17+
{
1218
/**
1319
* Authorization level of a basic admin session
1420
*
@@ -17,7 +23,22 @@ class MassMarkAsRead extends \Magento\AdminNotification\Controller\Adminhtml\Not
1723
const ADMIN_RESOURCE = 'Magento_AdminNotification::mark_as_read';
1824

1925
/**
20-
* @return void
26+
* @var InboxModelFactory
27+
*/
28+
private $inboxModelFactory;
29+
30+
/**
31+
* @param Action\Context $context
32+
* @param InboxModelFactory $inboxModelFactory
33+
*/
34+
public function __construct(Action\Context $context, InboxModelFactory $inboxModelFactory)
35+
{
36+
parent::__construct($context);
37+
$this->inboxModelFactory = $inboxModelFactory;
38+
}
39+
40+
/**
41+
* @inheritdoc
2142
*/
2243
public function execute()
2344
{
@@ -27,7 +48,7 @@ public function execute()
2748
} else {
2849
try {
2950
foreach ($ids as $id) {
30-
$model = $this->_objectManager->create(\Magento\AdminNotification\Model\Inbox::class)->load($id);
51+
$model = $this->inboxModelFactory->create()->load($id);
3152
if ($model->getId()) {
3253
$model->setIsRead(1)->save();
3354
}
@@ -44,6 +65,6 @@ public function execute()
4465
);
4566
}
4667
}
47-
$this->_redirect('adminhtml/*/');
68+
return $this->_redirect('adminhtml/*/');
4869
}
4970
}

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
namespace Magento\AdminNotification\Controller\Adminhtml\Notification;
87

9-
class MassRemove extends \Magento\AdminNotification\Controller\Adminhtml\Notification
8+
use Magento\AdminNotification\Controller\Adminhtml\Notification;
9+
use Magento\AdminNotification\Model\InboxFactory as InboxModelFactory;
10+
use Magento\Backend\App\Action;
11+
use Magento\Framework\App\Action\HttpPostActionInterface;
12+
13+
/**
14+
* AdminNotification MassRemove controller
15+
*/
16+
class MassRemove extends Notification implements HttpPostActionInterface
1017
{
1118

1219
/**
@@ -15,9 +22,23 @@ class MassRemove extends \Magento\AdminNotification\Controller\Adminhtml\Notific
1522
* @see _isAllowed()
1623
*/
1724
const ADMIN_RESOURCE = 'Magento_AdminNotification::adminnotification_remove';
25+
/**
26+
* @var InboxModelFactory
27+
*/
28+
private $inboxModelFactory;
29+
30+
/**
31+
* @param Action\Context $context
32+
* @param InboxModelFactory $inboxModelFactory
33+
*/
34+
public function __construct(Action\Context $context, InboxModelFactory $inboxModelFactory)
35+
{
36+
parent::__construct($context);
37+
$this->inboxModelFactory = $inboxModelFactory;
38+
}
1839

1940
/**
20-
* @return void
41+
* @inheritdoc
2142
*/
2243
public function execute()
2344
{
@@ -27,7 +48,7 @@ public function execute()
2748
} else {
2849
try {
2950
foreach ($ids as $id) {
30-
$model = $this->_objectManager->create(\Magento\AdminNotification\Model\Inbox::class)->load($id);
51+
$model = $this->inboxModelFactory->create()->load($id);
3152
if ($model->getId()) {
3253
$model->setIsRemove(1)->save();
3354
}
@@ -42,6 +63,6 @@ public function execute()
4263
);
4364
}
4465
}
45-
$this->_redirect('adminhtml/*/');
66+
return $this->_redirect('adminhtml/*/');
4667
}
4768
}

0 commit comments

Comments
 (0)