Skip to content

Commit f8474ec

Browse files
committed
Static test fix - reduce coupling in page data provider with service
1 parent bd6c526 commit f8474ec

File tree

2 files changed

+77
-35
lines changed

2 files changed

+77
-35
lines changed

app/code/Magento/Cms/Model/Page/DataProvider.php

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
namespace Magento\Cms\Model\Page;
77

88
use Magento\Cms\Api\Data\PageInterface;
9-
use Magento\Cms\Api\PageRepositoryInterface;
10-
use Magento\Cms\Model\PageFactory;
9+
use Magento\Cms\Model\Page\Service\PageService;
1110
use Magento\Cms\Model\ResourceModel\Page\CollectionFactory;
1211
use Magento\Framework\App\ObjectManager;
1312
use Magento\Framework\App\Request\DataPersistorInterface;
@@ -33,11 +32,6 @@ class DataProvider extends ModifierPoolDataProvider
3332
*/
3433
protected $loadedData;
3534

36-
/**
37-
* @var PageRepositoryInterface
38-
*/
39-
private $pageRepository;
40-
4135
/**
4236
* @var AuthorizationInterface
4337
*/
@@ -49,14 +43,9 @@ class DataProvider extends ModifierPoolDataProvider
4943
private $request;
5044

5145
/**
52-
* @var CustomLayoutManagerInterface
53-
*/
54-
private $customLayoutManager;
55-
56-
/**
57-
* @var PageFactory
46+
* @var PageService
5847
*/
59-
private $pageFactory;
48+
private $pageService;
6049

6150
/**
6251
* @var LoggerInterface
@@ -74,9 +63,7 @@ class DataProvider extends ModifierPoolDataProvider
7463
* @param PoolInterface|null $pool
7564
* @param AuthorizationInterface|null $auth
7665
* @param RequestInterface|null $request
77-
* @param CustomLayoutManagerInterface|null $customLayoutManager
78-
* @param PageRepositoryInterface|null $pageRepository
79-
* @param PageFactory|null $pageFactory
66+
* @param PageService|null $pageService
8067
* @param LoggerInterface|null $logger
8168
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8269
*/
@@ -91,9 +78,7 @@ public function __construct(
9178
?PoolInterface $pool = null,
9279
?AuthorizationInterface $auth = null,
9380
?RequestInterface $request = null,
94-
?CustomLayoutManagerInterface $customLayoutManager = null,
95-
?PageRepositoryInterface $pageRepository = null,
96-
?PageFactory $pageFactory = null,
81+
?PageService $pageService = null,
9782
?LoggerInterface $logger = null
9883
) {
9984
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data, $pool);
@@ -102,10 +87,7 @@ public function __construct(
10287
$this->auth = $auth ?? ObjectManager::getInstance()->get(AuthorizationInterface::class);
10388
$this->meta = $this->prepareMeta($this->meta);
10489
$this->request = $request ?? ObjectManager::getInstance()->get(RequestInterface::class);
105-
$this->customLayoutManager = $customLayoutManager
106-
?? ObjectManager::getInstance()->get(CustomLayoutManagerInterface::class);
107-
$this->pageRepository = $pageRepository ?? ObjectManager::getInstance()->get(PageRepositoryInterface::class);
108-
$this->pageFactory = $pageFactory ?: ObjectManager::getInstance()->get(PageFactory::class);
90+
$this->pageService = $pageService ?: ObjectManager::getInstance()->get(PageService::class);
10991
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
11092
}
11193

@@ -150,22 +132,16 @@ private function getCurrentPage(): PageInterface
150132
{
151133
$pageId = $this->getPageId();
152134
if ($pageId) {
153-
try {
154-
$page = $this->pageRepository->getById($pageId);
155-
} catch (LocalizedException $exception) {
156-
$page = $this->pageFactory->create();
157-
}
158-
159-
return $page;
135+
return $this->pageService->getPageById($pageId);
160136
}
161137

162138
$data = $this->dataPersistor->get('cms_page');
163139
if (empty($data)) {
164-
return $this->pageFactory->create();
140+
return $this->pageService->createPageFactory();
165141
}
166142
$this->dataPersistor->clear('cms_page');
167143

168-
return $this->pageFactory->create()
144+
return $this->pageService->createPageFactory()
169145
->setData($data);
170146
}
171147

@@ -217,11 +193,11 @@ public function getMeta()
217193
try {
218194
$pageId = $this->getPageId();
219195
if ($pageId) {
220-
$page = $this->pageRepository->getById($pageId);
196+
$page = $this->pageService->getPageById($pageId);
221197
if ($page->getCustomLayoutUpdateXml() || $page->getLayoutUpdateXml()) {
222198
$options[] = ['label' => 'Use existing layout update XML', 'value' => '_existing_'];
223199
}
224-
foreach ($this->customLayoutManager->fetchAvailableFiles($page) as $layoutFile) {
200+
foreach ($this->pageService->fetchAvailableCustomLayouts($page) as $layoutFile) {
225201
$options[] = ['label' => $layoutFile, 'value' => $layoutFile];
226202
}
227203
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright 2015 Adobe
4+
* All Rights Reserved.
5+
*/
6+
namespace Magento\Cms\Model\Page\Service;
7+
8+
use Magento\Cms\Api\Data\PageInterface;
9+
use Magento\Cms\Api\PageRepositoryInterface;
10+
use Magento\Cms\Model\Page\CustomLayoutManagerInterface;
11+
use Magento\Cms\Model\PageFactory;
12+
use Magento\Framework\Exception\LocalizedException;
13+
14+
/**
15+
* Cms Page Service Class
16+
*/
17+
class PageService
18+
{
19+
/**
20+
* @param PageRepositoryInterface $pageRepository
21+
* @param PageFactory $pageFactory
22+
* @param CustomLayoutManagerInterface $customLayoutManager
23+
*/
24+
public function __construct(
25+
private PageRepositoryInterface $pageRepository,
26+
private PageFactory $pageFactory,
27+
private CustomLayoutManagerInterface $customLayoutManager,
28+
) {
29+
}
30+
31+
/**
32+
* To get the page by its ID. If the page not exists, a new page instance is returned
33+
*
34+
* @param int $id
35+
* @return PageInterface
36+
*/
37+
public function getPageById(int $id): PageInterface
38+
{
39+
try {
40+
return $this->pageRepository->getById($id);
41+
} catch (LocalizedException) {
42+
return $this->pageFactory->create();
43+
}
44+
}
45+
46+
/**
47+
* To create pagefactory class
48+
*
49+
* @return PageInterface
50+
*/
51+
public function createPageFactory(): PageInterface
52+
{
53+
return $this->pageFactory->create();
54+
}
55+
56+
/**
57+
* Fetches the available custom layouts for a given page.
58+
*
59+
* @param PageInterface $page
60+
* @return array
61+
*/
62+
public function fetchAvailableCustomLayouts(PageInterface $page): array
63+
{
64+
return $this->customLayoutManager->fetchAvailableFiles($page);
65+
}
66+
}

0 commit comments

Comments
 (0)