Skip to content

Commit e8679fa

Browse files
authored
ENGCOM-7173: Cleanup ObjectManager usage - Magento_Sitemap #27384
2 parents d238a65 + 42adec4 commit e8679fa

File tree

6 files changed

+141
-130
lines changed

6 files changed

+141
-130
lines changed

app/code/Magento/Sitemap/Block/Adminhtml/Grid/Renderer/Link.php

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,76 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Sitemap\Block\Adminhtml\Grid\Renderer;
79

8-
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\Backend\Block\Context;
11+
use Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer;
912
use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot;
10-
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\DataObject;
14+
use Magento\Framework\Filesystem;
15+
use Magento\Sitemap\Model\Sitemap;
16+
use Magento\Sitemap\Model\SitemapFactory;
1117

1218
/**
1319
* Sitemap grid link column renderer
1420
*/
15-
class Link extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
21+
class Link extends AbstractRenderer
1622
{
1723
/**
18-
* @var \Magento\Framework\Filesystem $filesystem
24+
* @var Filesystem
1925
*/
20-
protected $_filesystem;
26+
private $filesystem;
2127

2228
/**
23-
* @var \Magento\Sitemap\Model\SitemapFactory
29+
* @var SitemapFactory
2430
*/
25-
protected $_sitemapFactory;
31+
private $sitemapFactory;
2632

2733
/**
2834
* @var DocumentRoot
2935
*/
30-
protected $documentRoot;
36+
private $documentRoot;
3137

3238
/**
33-
* @param \Magento\Backend\Block\Context $context
34-
* @param \Magento\Sitemap\Model\SitemapFactory $sitemapFactory
35-
* @param \Magento\Framework\Filesystem $filesystem
36-
* @param array $data
39+
* @param Context $context
40+
* @param SitemapFactory $sitemapFactory
41+
* @param Filesystem $filesystem
3742
* @param DocumentRoot $documentRoot
43+
* @param array $data
3844
*/
3945
public function __construct(
40-
\Magento\Backend\Block\Context $context,
41-
\Magento\Sitemap\Model\SitemapFactory $sitemapFactory,
42-
\Magento\Framework\Filesystem $filesystem,
43-
array $data = [],
44-
DocumentRoot $documentRoot = null
46+
Context $context,
47+
SitemapFactory $sitemapFactory,
48+
Filesystem $filesystem,
49+
DocumentRoot $documentRoot,
50+
array $data = []
4551
) {
46-
$this->_sitemapFactory = $sitemapFactory;
47-
$this->_filesystem = $filesystem;
48-
$this->documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(DocumentRoot::class);
52+
$this->sitemapFactory = $sitemapFactory;
53+
$this->filesystem = $filesystem;
54+
$this->documentRoot = $documentRoot;
4955

5056
parent::__construct($context, $data);
5157
}
5258

5359
/**
5460
* Prepare link to display in grid
5561
*
56-
* @param \Magento\Framework\DataObject $row
62+
* @param DataObject $row
63+
*
5764
* @return string
5865
*/
59-
public function render(\Magento\Framework\DataObject $row)
66+
public function render(DataObject $row)
6067
{
61-
/** @var $sitemap \Magento\Sitemap\Model\Sitemap */
62-
$sitemap = $this->_sitemapFactory->create();
68+
/** @var $sitemap Sitemap */
69+
$sitemap = $this->sitemapFactory->create();
6370
$sitemap->setStoreId($row->getStoreId());
64-
$url = $this->escapeHtml($sitemap->getSitemapUrl($row->getSitemapPath(), $row->getSitemapFilename()));
71+
$url = $this->_escaper->escapeHtml($sitemap->getSitemapUrl($row->getSitemapPath(), $row->getSitemapFilename()));
6572

6673
$fileName = preg_replace('/^\//', '', $row->getSitemapPath() . $row->getSitemapFilename());
6774
$documentRootPath = $this->documentRoot->getPath();
68-
$directory = $this->_filesystem->getDirectoryRead($documentRootPath);
75+
$directory = $this->filesystem->getDirectoryRead($documentRootPath);
6976
if ($directory->isFile($fileName)) {
7077
return sprintf('<a href="%1$s">%1$s</a>', $url);
7178
}

app/code/Magento/Sitemap/Controller/Adminhtml/Sitemap/Delete.php

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,45 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Sitemap\Controller\Adminhtml\Sitemap;
79

10+
use Magento\Backend\App\Action\Context;
811
use Magento\Framework\App\Action\HttpPostActionInterface;
912
use Magento\Framework\App\Filesystem\DirectoryList;
10-
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\Filesystem;
14+
use Magento\Sitemap\Controller\Adminhtml\Sitemap;
15+
use Magento\Sitemap\Model\SitemapFactory;
1116

1217
/**
1318
* Controller class Delete. Represents adminhtml request flow for a sitemap deletion
1419
*/
15-
class Delete extends \Magento\Sitemap\Controller\Adminhtml\Sitemap implements HttpPostActionInterface
20+
class Delete extends Sitemap implements HttpPostActionInterface
1621
{
1722
/**
18-
* @var \Magento\Framework\Filesystem
23+
* @var SitemapFactory
1924
*/
20-
private $filesystem;
25+
private $sitemapFactory;
2126

2227
/**
23-
* @var \Magento\Sitemap\Model\SitemapFactory
28+
* @var Filesystem
2429
*/
25-
private $sitemapFactory;
30+
private $filesystem;
2631

2732
/**
28-
* Constructor
29-
*
30-
* @param \Magento\Backend\App\Action\Context $context
31-
* @param \Magento\Sitemap\Model\SitemapFactory|null $sitemapFactory
33+
* @param Context $context
34+
* @param SitemapFactory $sitemapFactory
35+
* @param Filesystem $filesystem
3236
*/
3337
public function __construct(
34-
\Magento\Backend\App\Action\Context $context,
35-
\Magento\Sitemap\Model\SitemapFactory $sitemapFactory = null
38+
Context $context,
39+
SitemapFactory $sitemapFactory,
40+
Filesystem $filesystem
3641
) {
3742
parent::__construct($context);
38-
$this->sitemapFactory = $sitemapFactory ?: ObjectManager::getInstance()
39-
->get(\Magento\Sitemap\Model\SitemapFactory::class);
43+
$this->sitemapFactory = $sitemapFactory;
44+
$this->filesystem = $filesystem;
4045
}
4146

4247
/**
@@ -46,7 +51,7 @@ public function __construct(
4651
*/
4752
public function execute()
4853
{
49-
$directory = $this->getFilesystem()->getDirectoryWrite(DirectoryList::ROOT);
54+
$directory = $this->filesystem->getDirectoryWrite(DirectoryList::ROOT);
5055
// check if we know what should be deleted
5156
$id = $this->getRequest()->getParam('sitemap_id');
5257
if ($id) {
@@ -86,20 +91,4 @@ public function execute()
8691
// go to grid
8792
$this->_redirect('adminhtml/*/');
8893
}
89-
90-
/**
91-
* The getter function to get Filesystem object for real application code
92-
*
93-
* @return \Magento\Framework\Filesystem
94-
* @deprecated 100.2.0
95-
*/
96-
private function getFilesystem()
97-
{
98-
if (null === $this->filesystem) {
99-
$this->filesystem = \Magento\Framework\App\ObjectManager::getInstance()->get(
100-
\Magento\Framework\Filesystem::class
101-
);
102-
}
103-
return $this->filesystem;
104-
}
10594
}

app/code/Magento/Sitemap/Controller/Adminhtml/Sitemap/Generate.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,36 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Sitemap\Controller\Adminhtml\Sitemap;
89

910
use Magento\Backend\App\Action;
1011
use Magento\Framework\App\Action\HttpGetActionInterface;
12+
use Magento\Framework\App\Area;
1113
use Magento\Sitemap\Controller\Adminhtml\Sitemap;
1214
use Magento\Store\Model\App\Emulation;
13-
use Magento\Framework\App\ObjectManager;
1415

1516
/**
1617
* Generate sitemap file
1718
*/
1819
class Generate extends Sitemap implements HttpGetActionInterface
1920
{
20-
/** @var \Magento\Store\Model\App\Emulation $appEmulation */
21+
/**
22+
* @var Emulation
23+
*/
2124
private $appEmulation;
2225

2326
/**
24-
* Generate constructor.
2527
* @param Action\Context $context
26-
* @param \Magento\Store\Model\App\Emulation|null $appEmulation
28+
* @param Emulation $appEmulation
2729
*/
2830
public function __construct(
2931
Action\Context $context,
30-
Emulation $appEmulation = null
32+
Emulation $appEmulation
3133
) {
3234
parent::__construct($context);
33-
$this->appEmulation = $appEmulation ?: ObjectManager::getInstance()
34-
->get(\Magento\Store\Model\App\Emulation::class);
35+
$this->appEmulation = $appEmulation;
3536
}
3637

3738
/**
@@ -51,7 +52,7 @@ public function execute()
5152
try {
5253
$this->appEmulation->startEnvironmentEmulation(
5354
$sitemap->getStoreId(),
54-
\Magento\Framework\App\Area::AREA_FRONTEND,
55+
Area::AREA_FRONTEND,
5556
true
5657
);
5758
$sitemap->generateXml();

app/code/Magento/Sitemap/Controller/Adminhtml/Sitemap/Save.php

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,27 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Sitemap\Controller\Adminhtml\Sitemap;
79

810
use Magento\Backend\App\Action\Context;
11+
use Magento\Backend\Model\View\Result\Redirect;
912
use Magento\Framework\App\Action\HttpPostActionInterface;
1013
use Magento\Framework\App\Filesystem\DirectoryList;
1114
use Magento\Framework\Controller;
15+
use Magento\Framework\Controller\ResultInterface;
16+
use Magento\Framework\Filesystem;
1217
use Magento\Framework\Validator\StringLength;
1318
use Magento\MediaStorage\Model\File\Validator\AvailablePath;
19+
use Magento\Sitemap\Controller\Adminhtml\Sitemap;
20+
use Magento\Sitemap\Helper\Data;
1421
use Magento\Sitemap\Model\SitemapFactory;
1522

1623
/**
1724
* Save sitemap controller.
1825
*/
19-
class Save extends \Magento\Sitemap\Controller\Adminhtml\Sitemap implements HttpPostActionInterface
26+
class Save extends Sitemap implements HttpPostActionInterface
2027
{
2128
/**
2229
* Maximum length of sitemap filename
@@ -34,12 +41,12 @@ class Save extends \Magento\Sitemap\Controller\Adminhtml\Sitemap implements Http
3441
private $pathValidator;
3542

3643
/**
37-
* @var \Magento\Sitemap\Helper\Data
44+
* @var Data
3845
*/
3946
private $sitemapHelper;
4047

4148
/**
42-
* @var \Magento\Framework\Filesystem
49+
* @var Filesystem
4350
*/
4451
private $filesystem;
4552

@@ -53,24 +60,24 @@ class Save extends \Magento\Sitemap\Controller\Adminhtml\Sitemap implements Http
5360
* @param Context $context
5461
* @param StringLength $stringValidator
5562
* @param AvailablePath $pathValidator
56-
* @param \Magento\Sitemap\Helper\Data $sitemapHelper
57-
* @param \Magento\Framework\Filesystem $filesystem
63+
* @param Data $sitemapHelper
64+
* @param Filesystem $filesystem
5865
* @param SitemapFactory $sitemapFactory
5966
*/
6067
public function __construct(
6168
Context $context,
62-
StringLength $stringValidator = null,
63-
AvailablePath $pathValidator = null,
64-
\Magento\Sitemap\Helper\Data $sitemapHelper = null,
65-
\Magento\Framework\Filesystem $filesystem = null,
66-
SitemapFactory $sitemapFactory = null
69+
StringLength $stringValidator,
70+
AvailablePath $pathValidator,
71+
Data $sitemapHelper,
72+
Filesystem $filesystem,
73+
SitemapFactory $sitemapFactory
6774
) {
6875
parent::__construct($context);
69-
$this->stringValidator = $stringValidator ?: $this->_objectManager->get(StringLength::class);
70-
$this->pathValidator = $pathValidator ?: $this->_objectManager->get(AvailablePath::class);
71-
$this->sitemapHelper = $sitemapHelper ?: $this->_objectManager->get(\Magento\Sitemap\Helper\Data::class);
72-
$this->filesystem = $filesystem ?: $this->_objectManager->get(\Magento\Framework\Filesystem::class);
73-
$this->sitemapFactory = $sitemapFactory ?: $this->_objectManager->get(SitemapFactory::class);
76+
$this->stringValidator = $stringValidator;
77+
$this->pathValidator = $pathValidator;
78+
$this->sitemapHelper = $sitemapHelper;
79+
$this->filesystem = $filesystem;
80+
$this->sitemapFactory = $sitemapFactory;
7481
}
7582

7683
/**
@@ -115,11 +122,12 @@ protected function validatePath(array $data)
115122
* Clear sitemap
116123
*
117124
* @param \Magento\Sitemap\Model\Sitemap $model
125+
*
118126
* @return void
119127
*/
120128
protected function clearSiteMap(\Magento\Sitemap\Model\Sitemap $model)
121129
{
122-
/** @var \Magento\Framework\Filesystem $directory */
130+
/** @var Filesystem $directory */
123131
$directory = $this->filesystem->getDirectoryWrite(DirectoryList::ROOT);
124132

125133
if ($this->getRequest()->getParam('sitemap_id')) {
@@ -169,12 +177,13 @@ protected function saveData($data)
169177
* Get result after saving data
170178
*
171179
* @param string|bool $id
172-
* @return \Magento\Framework\Controller\ResultInterface
180+
* @return ResultInterface
173181
*/
174182
protected function getResult($id)
175183
{
176-
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
184+
/** @var Redirect $resultRedirect */
177185
$resultRedirect = $this->resultFactory->create(Controller\ResultFactory::TYPE_REDIRECT);
186+
178187
if ($id) {
179188
// check if 'Save and Continue'
180189
if ($this->getRequest()->getParam('back')) {
@@ -194,19 +203,20 @@ protected function getResult($id)
194203
'adminhtml/*/edit',
195204
['sitemap_id' => $this->getRequest()->getParam('sitemap_id')]
196205
);
206+
197207
return $resultRedirect;
198208
}
199209

200210
/**
201211
* Save action
202212
*
203-
* @return \Magento\Backend\Model\View\Result\Redirect
213+
* @return Redirect
204214
*/
205215
public function execute()
206216
{
207217
// check if data sent
208218
$data = $this->getRequest()->getPostValue();
209-
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
219+
/** @var Redirect $resultRedirect */
210220
$resultRedirect = $this->resultFactory->create(Controller\ResultFactory::TYPE_REDIRECT);
211221
if ($data) {
212222
if (!$this->validatePath($data)) {

0 commit comments

Comments
 (0)