Skip to content

Commit 2d9631c

Browse files
committed
10414 - Recactored the code to be closer to CQRS design pattern
1 parent 72b64ea commit 2d9631c

File tree

9 files changed

+56
-125
lines changed

9 files changed

+56
-125
lines changed

app/code/Magento/Cms/Api/BlockManagementInterface.php renamed to app/code/Magento/Cms/Api/GetBlockByIdentifierInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
namespace Magento\Cms\Api;
77

88
/**
9-
* CMS Block management interface
9+
* Command to load the block data by specified identifier
1010
* @api
1111
*/
12-
interface BlockManagementInterface
12+
interface GetBlockByIdentifierInterface
1313
{
1414
/**
1515
* Load block data by given block identifier.
1616
*
1717
* @param string $identifier
18-
* @param int|null $storeId
18+
* @param int $storeId
1919
* @return \Magento\Cms\Api\Data\BlockInterface
2020
*/
21-
public function getByIdentifier(string $identifier, $storeId = null) : \Magento\Cms\Api\Data\BlockInterface;
21+
public function execute(string $identifier, int $storeId) : \Magento\Cms\Api\Data\BlockInterface;
2222
}
2323

app/code/Magento/Cms/Api/PageManagementInterface.php renamed to app/code/Magento/Cms/Api/GetPageByIdentifierInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
namespace Magento\Cms\Api;
77

88
/**
9-
* CMS Page management interface
9+
* Command to load the page data by specified identifier
1010
* @api
1111
*/
12-
interface PageManagementInterface
12+
interface GetPageByIdentifierInterface
1313
{
1414
/**
1515
* Load page data by given page identifier.
1616
*
1717
* @param string $identifier
18-
* @param int|null $storeId
18+
* @param int $storeId
1919
* @return \Magento\Cms\Api\Data\PageInterface
2020
*/
21-
public function getByIdentifier(string $identifier, $storeId = null) : \Magento\Cms\Api\Data\PageInterface;
21+
public function execute(string $identifier, int $storeId) : \Magento\Cms\Api\Data\PageInterface;
2222
}
2323

app/code/Magento/Cms/Model/BlockManagement.php renamed to app/code/Magento/Cms/Model/GetBlockByIdentifier.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
*/
66
namespace Magento\Cms\Model;
77

8-
use Magento\Cms\Api\BlockManagementInterface;
8+
use Magento\Cms\Api\GetBlockByIdentifierInterface;
99
use Magento\Cms\Api\Data\BlockInterface;
1010
use Magento\Framework\Exception\NoSuchEntityException;
1111

1212
/**
13-
* Class BlockManagement
13+
* Class GetBlockByIdentifier
1414
*/
15-
class BlockManagement implements BlockManagementInterface
15+
class GetBlockByIdentifier implements GetBlockByIdentifierInterface
1616
{
1717
/**
1818
* @var \Magento\Cms\Model\BlockFactory
@@ -24,41 +24,29 @@ class BlockManagement implements BlockManagementInterface
2424
*/
2525
private $blockResource;
2626

27-
/**
28-
* @var \Magento\Store\Model\StoreManagerInterface
29-
*/
30-
private $storeManager;
31-
3227
/**
3328
* BlockManagement constructor.
3429
* @param BlockFactory $blockFactory
3530
* @param ResourceModel\Block $blockResource
36-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
3731
*/
3832
public function __construct(
3933
\Magento\Cms\Model\BlockFactory $blockFactory,
40-
\Magento\Cms\Model\ResourceModel\Block $blockResource,
41-
\Magento\Store\Model\StoreManagerInterface $storeManager
34+
\Magento\Cms\Model\ResourceModel\Block $blockResource
4235
) {
4336
$this->blockFactory = $blockFactory;
4437
$this->blockResource = $blockResource;
45-
$this->storeManager = $storeManager;
4638
}
4739

4840
/**
4941
* Load block data by given block identifier.
5042
*
5143
* @param string $identifier
52-
* @param int|null $storeId
44+
* @param int $storeId
5345
* @return BlockInterface
5446
* @throws NoSuchEntityException
5547
*/
56-
public function getByIdentifier(string $identifier, $storeId = null) : BlockInterface
48+
public function execute(string $identifier, int $storeId) : BlockInterface
5749
{
58-
if ($storeId === null) {
59-
$storeId = $this->storeManager->getStore()->getId();
60-
}
61-
6250
$block = $this->blockFactory->create();
6351
$block->setStoreId($storeId);
6452
$this->blockResource->load($block, $identifier, BlockInterface::IDENTIFIER);

app/code/Magento/Cms/Model/PageManagement.php renamed to app/code/Magento/Cms/Model/GetPageByIdentifier.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
namespace Magento\Cms\Model;
77

88
use Magento\Cms\Api\Data\PageInterface;
9-
use Magento\Cms\Api\PageManagementInterface;
9+
use Magento\Cms\Api\GetPageByIdentifierInterface;
1010
use Magento\Framework\Exception\NoSuchEntityException;
1111

1212
/**
13-
* Class PageManagement
13+
* Class GetPageByIdentifier
1414
*/
15-
class PageManagement implements PageManagementInterface
15+
class GetPageByIdentifier implements GetPageByIdentifierInterface
1616
{
1717
/**
1818
* @var \Magento\Cms\Model\PageFactory
@@ -24,41 +24,29 @@ class PageManagement implements PageManagementInterface
2424
*/
2525
private $pageResource;
2626

27-
/**
28-
* @var \Magento\Store\Model\StoreManagerInterface
29-
*/
30-
private $storeManager;
31-
3227
/**
3328
* PageManagement constructor.
3429
* @param PageFactory $pageFactory
3530
* @param ResourceModel\Page $pageResource
36-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
3731
*/
3832
public function __construct(
3933
\Magento\Cms\Model\PageFactory $pageFactory,
40-
\Magento\Cms\Model\ResourceModel\Page $pageResource,
41-
\Magento\Store\Model\StoreManagerInterface $storeManager
34+
\Magento\Cms\Model\ResourceModel\Page $pageResource
4235
) {
4336
$this->pageFactory = $pageFactory;
4437
$this->pageResource = $pageResource;
45-
$this->storeManager = $storeManager;
4638
}
4739

4840
/**
4941
* Load page data by given page identifier.
5042
*
5143
* @param string $identifier
52-
* @param int|null $storeId
44+
* @param int $storeId
5345
* @return PageInterface
5446
* @throws NoSuchEntityException
5547
*/
56-
public function getByIdentifier(string $identifier, $storeId = null) : PageInterface
48+
public function execute(string $identifier, int $storeId) : PageInterface
5749
{
58-
if ($storeId === null) {
59-
$storeId = $this->storeManager->getStore()->getId();
60-
}
61-
6250
$page = $this->pageFactory->create();
6351
$page->setStoreId($storeId);
6452
$this->pageResource->load($page, $identifier, PageInterface::IDENTIFIER);

app/code/Magento/Cms/Test/Unit/Model/BlockManagementTest.php renamed to app/code/Magento/Cms/Test/Unit/Model/GetBlockByIdentifierTest.php

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
*/
66
namespace Magento\Cms\Test\Unit\Model;
77

8-
use Magento\Cms\Model\BlockManagement;
8+
use Magento\Cms\Model\GetBlockByIdentifier;
99

1010
/**
11-
* Test for Magento\Cms\Model\BlockManagment
11+
* Test for Magento\Cms\Model\GetBlockByIdentifier
1212
*/
1313

14-
class BlockManagementTest extends \PHPUnit\Framework\TestCase
14+
class GetBlockByIdentifierTest extends \PHPUnit\Framework\TestCase
1515
{
1616
/**
17-
* @var BlockManagement
17+
* @var GetBlockByIdentifier
1818
*/
19-
private $blockManagement;
19+
private $getBlockByIdentifierCommand;
2020

2121
/**
2222
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Cms\Model\Block
@@ -33,16 +33,6 @@ class BlockManagementTest extends \PHPUnit\Framework\TestCase
3333
*/
3434
private $blockResource;
3535

36-
/**
37-
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\StoreManagerInterface
38-
*/
39-
private $storeManager;
40-
41-
/**
42-
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Api\Data\StoreInterface
43-
*/
44-
private $store;
45-
4636
protected function setUp()
4737
{
4838
$this->blockFactory = $this->getMockBuilder(\Magento\Cms\Model\BlockFactory::class)
@@ -54,20 +44,12 @@ protected function setUp()
5444
->disableOriginalConstructor()
5545
->getMock();
5646

57-
$this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
58-
->disableOriginalConstructor()
59-
->getMock();
60-
61-
$this->store = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
62-
->disableOriginalConstructor(true)
63-
->getMock();
64-
6547
$this->block = $this->getMockBuilder(\Magento\Cms\Model\Block::class)
6648
->disableOriginalConstructor()
6749
->setMethods(['setStoreId', 'getId'])
6850
->getMock();
6951

70-
$this->blockManagement = new BlockManagement($this->blockFactory, $this->blockResource, $this->storeManager);
52+
$this->getBlockByIdentifierCommand = new GetBlockByIdentifier($this->blockFactory, $this->blockResource);
7153
}
7254

7355
/**
@@ -76,15 +58,7 @@ protected function setUp()
7658
public function testGetByIdentifier()
7759
{
7860
$identifier = 'banner';
79-
$storeId = null;
80-
81-
$this->storeManager->expects($this->once())
82-
->method('getStore')
83-
->willReturn($this->store);
84-
85-
$this->store->expects($this->once())
86-
->method('getId')
87-
->willReturn(1);
61+
$storeId = 0;
8862

8963
$this->blockFactory->expects($this->once())
9064
->method('create')
@@ -103,7 +77,7 @@ public function testGetByIdentifier()
10377
->with($this->block, $identifier)
10478
->willReturn($this->block);
10579

106-
$this->blockManagement->getByIdentifier($identifier, $storeId);
80+
$this->getBlockByIdentifierCommand->execute($identifier, $storeId);
10781
}
10882
}
10983

app/code/Magento/Cms/Test/Unit/Model/PageManagementTest.php renamed to app/code/Magento/Cms/Test/Unit/Model/GetPageByIdentifierTest.php

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
*/
66
namespace Magento\Cms\Test\Unit\Model;
77

8-
use Magento\Cms\Model\PageManagement;
8+
use Magento\Cms\Model\GetPageByIdentifier;
99

1010
/**
11-
* Test for Magento\Cms\Model\PageManagment
11+
* Test for Magento\Cms\Model\GetPageByIdentifier
1212
*/
1313

14-
class PageManagementTest extends \PHPUnit\Framework\TestCase
14+
class GetPageByIdentifierTest extends \PHPUnit\Framework\TestCase
1515
{
1616
/**
17-
* @var PageManagement
17+
* @var GetPageByIdentifier
1818
*/
19-
protected $pageManagment;
19+
protected $getPageByIdentifierCommand;
2020

2121
/**
2222
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Cms\Model\Page
@@ -33,16 +33,6 @@ class PageManagementTest extends \PHPUnit\Framework\TestCase
3333
*/
3434
protected $pageResource;
3535

36-
/**
37-
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\StoreManagerInterface
38-
*/
39-
protected $storeManager;
40-
41-
/**
42-
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Api\Data\StoreInterface
43-
*/
44-
protected $store;
45-
4636
public function setUp()
4737
{
4838
$this->pageFactory = $this->getMockBuilder(\Magento\Cms\Model\PageFactory::class)
@@ -54,20 +44,12 @@ public function setUp()
5444
->disableOriginalConstructor(true)
5545
->getMock();
5646

57-
$this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
58-
->disableOriginalConstructor(true)
59-
->getMock();
60-
61-
$this->store = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
62-
->disableOriginalConstructor(true)
63-
->getMock();
64-
6547
$this->page = $this->getMockBuilder(\Magento\Cms\Model\Page::class)
6648
->disableOriginalConstructor()
6749
->setMethods(['setStoreId', 'getId'])
6850
->getMock();
6951

70-
$this->pageManagment = new PageManagement($this->pageFactory, $this->pageResource, $this->storeManager);
52+
$this->getPageByIdentifierCommand = new GetPageByIdentifier($this->pageFactory, $this->pageResource);
7153
}
7254

7355
/**
@@ -76,15 +58,7 @@ public function setUp()
7658
public function testGetByIdentifier()
7759
{
7860
$identifier = 'home';
79-
$storeId = null;
80-
81-
$this->storeManager->expects($this->once())
82-
->method('getStore')
83-
->willReturn($this->store);
84-
85-
$this->store->expects($this->once())
86-
->method('getId')
87-
->willReturn(1);
61+
$storeId = 0;
8862

8963
$this->pageFactory->expects($this->once())
9064
->method('create')
@@ -103,6 +77,6 @@ public function testGetByIdentifier()
10377
->with($this->page, $identifier)
10478
->willReturn($this->page);
10579

106-
$this->pageManagment->getByIdentifier($identifier, $storeId);
80+
$this->getPageByIdentifierCommand->execute($identifier, $storeId);
10781
}
10882
}

app/code/Magento/Cms/etc/di.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
type="Magento\Framework\Api\SearchResults" />
1111
<preference for="Magento\Cms\Api\Data\BlockSearchResultsInterface"
1212
type="Magento\Framework\Api\SearchResults" />
13+
<preference for="Magento\Cms\Api\GetBlockByIdentifierInterface" type="Magento\Cms\Model\GetBlockByIdentifier" />
14+
<preference for="Magento\Cms\Api\GetPageByIdentifierInterface" type="Magento\Cms\Model\GetPageByIdentifier" />
1315
<preference for="Magento\Cms\Api\Data\PageInterface" type="Magento\Cms\Model\Page" />
1416
<preference for="Magento\Cms\Api\Data\BlockInterface" type="Magento\Cms\Model\Block" />
1517
<preference for="Magento\Cms\Api\BlockRepositoryInterface" type="Magento\Cms\Model\BlockRepository" />

0 commit comments

Comments
 (0)