Skip to content

Commit b69de86

Browse files
committed
Replaced model->save() with service contract
1 parent ff2fa21 commit b69de86

File tree

3 files changed

+53
-37
lines changed

3 files changed

+53
-37
lines changed

app/code/Magento/Sales/Controller/Adminhtml/Invoice/AbstractInvoice/View.php

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
6+
7+
declare(strict_types=1);
8+
79
namespace Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice;
810

911
use Magento\Backend\App\Action\Context;
12+
use Magento\Backend\Model\View\Result\ForwardFactory;
1013
use Magento\Framework\App\ObjectManager;
1114
use Magento\Framework\Registry;
1215
use Magento\Sales\Api\InvoiceRepositoryInterface;
13-
use Magento\Sales\Model\Order\InvoiceRepository;
1416

17+
/**
18+
* Class View
19+
*/
1520
abstract class View extends \Magento\Backend\App\Action
1621
{
1722
/**
@@ -27,7 +32,7 @@ abstract class View extends \Magento\Backend\App\Action
2732
protected $registry;
2833

2934
/**
30-
* @var \Magento\Backend\Model\View\Result\ForwardFactory
35+
* @var ForwardFactory
3136
*/
3237
protected $resultForwardFactory;
3338

@@ -39,16 +44,20 @@ abstract class View extends \Magento\Backend\App\Action
3944
/**
4045
* @param Context $context
4146
* @param Registry $registry
42-
* @param \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
47+
* @param ForwardFactory $resultForwardFactory
48+
* @param InvoiceRepositoryInterface $invoiceRepository
4349
*/
4450
public function __construct(
4551
Context $context,
4652
Registry $registry,
47-
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
53+
ForwardFactory $resultForwardFactory,
54+
InvoiceRepositoryInterface $invoiceRepository = null
4855
) {
49-
$this->registry = $registry;
5056
parent::__construct($context);
57+
$this->registry = $registry;
5158
$this->resultForwardFactory = $resultForwardFactory;
59+
$this->invoiceRepository = $invoiceRepository ?:
60+
ObjectManager::getInstance()->get(InvoiceRepositoryInterface::class);
5261
}
5362

5463
/**
@@ -70,13 +79,14 @@ public function execute()
7079
}
7180

7281
/**
82+
* Get invoice using invoice Id from request params
83+
*
7384
* @return \Magento\Sales\Model\Order\Invoice|bool
7485
*/
7586
protected function getInvoice()
7687
{
7788
try {
78-
$invoice = $this->getInvoiceRepository()
79-
->get($this->getRequest()->getParam('invoice_id'));
89+
$invoice = $this->invoiceRepository->get($this->getRequest()->getParam('invoice_id'));
8090
$this->registry->register('current_invoice', $invoice);
8191
} catch (\Exception $e) {
8292
$this->messageManager->addErrorMessage(__('Invoice capturing error'));
@@ -85,19 +95,4 @@ protected function getInvoice()
8595

8696
return $invoice;
8797
}
88-
89-
/**
90-
* @return InvoiceRepository
91-
*
92-
* @deprecated 100.1.0
93-
*/
94-
private function getInvoiceRepository()
95-
{
96-
if ($this->invoiceRepository === null) {
97-
$this->invoiceRepository = ObjectManager::getInstance()
98-
->get(InvoiceRepositoryInterface::class);
99-
}
100-
101-
return $this->invoiceRepository;
102-
}
10398
}

app/code/Magento/Sales/Controller/Adminhtml/Order/Invoice/AddComment.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76

7+
declare(strict_types=1);
8+
89
namespace Magento\Sales\Controller\Adminhtml\Order\Invoice;
910

1011
use Magento\Backend\App\Action\Context;
12+
use Magento\Backend\Model\View\Result\ForwardFactory;
13+
use Magento\Framework\App\Action\HttpPostActionInterface;
14+
use Magento\Framework\App\ObjectManager;
1115
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Sales\Api\InvoiceRepositoryInterface;
1217
use Magento\Sales\Model\Order\Email\Sender\InvoiceCommentSender;
13-
use Magento\Sales\Model\Order\Invoice;
14-
use Magento\Backend\App\Action;
1518
use Magento\Framework\Registry;
1619
use Magento\Framework\Controller\Result\JsonFactory;
1720
use Magento\Framework\View\Result\PageFactory;
1821
use Magento\Framework\Controller\Result\RawFactory;
1922

20-
class AddComment extends \Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\View
23+
/**
24+
* Class AddComment
25+
*/
26+
class AddComment extends \Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\View implements
27+
HttpPostActionInterface
2128
{
2229
/**
2330
* @var InvoiceCommentSender
@@ -39,29 +46,38 @@ class AddComment extends \Magento\Sales\Controller\Adminhtml\Invoice\AbstractInv
3946
*/
4047
protected $resultRawFactory;
4148

49+
/**
50+
* @var InvoiceRepositoryInterface
51+
*/
52+
protected $invoiceRepository;
53+
4254
/**
4355
* @param Context $context
4456
* @param Registry $registry
45-
* @param \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
57+
* @param ForwardFactory $resultForwardFactory
4658
* @param InvoiceCommentSender $invoiceCommentSender
4759
* @param JsonFactory $resultJsonFactory
4860
* @param PageFactory $resultPageFactory
4961
* @param RawFactory $resultRawFactory
62+
* @param InvoiceRepositoryInterface $invoiceRepository
5063
*/
5164
public function __construct(
5265
Context $context,
5366
Registry $registry,
54-
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
67+
ForwardFactory $resultForwardFactory,
5568
InvoiceCommentSender $invoiceCommentSender,
5669
JsonFactory $resultJsonFactory,
5770
PageFactory $resultPageFactory,
58-
RawFactory $resultRawFactory
71+
RawFactory $resultRawFactory,
72+
InvoiceRepositoryInterface $invoiceRepository = null
5973
) {
6074
$this->invoiceCommentSender = $invoiceCommentSender;
6175
$this->resultJsonFactory = $resultJsonFactory;
6276
$this->resultPageFactory = $resultPageFactory;
6377
$this->resultRawFactory = $resultRawFactory;
64-
parent::__construct($context, $registry, $resultForwardFactory);
78+
$this->invoiceRepository = $invoiceRepository ?:
79+
ObjectManager::getInstance()->get(InvoiceRepositoryInterface::class);
80+
parent::__construct($context, $registry, $resultForwardFactory, $invoiceRepository);
6581
}
6682

6783
/**
@@ -90,7 +106,7 @@ public function execute()
90106
);
91107

92108
$this->invoiceCommentSender->send($invoice, !empty($data['is_customer_notified']), $data['comment']);
93-
$invoice->save();
109+
$this->invoiceRepository->save($invoice);
94110

95111
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
96112
$resultPage = $this->resultPageFactory->create();

app/code/Magento/Sales/Test/Unit/Controller/Adminhtml/Order/Invoice/AddCommentTest.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Sales\Test\Unit\Controller\Adminhtml\Order\Invoice;
710

811
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
@@ -186,7 +189,8 @@ protected function setUp()
186189
'invoiceCommentSender' => $this->commentSenderMock,
187190
'resultPageFactory' => $this->resultPageFactoryMock,
188191
'resultRawFactory' => $this->resultRawFactoryMock,
189-
'resultJsonFactory' => $this->resultJsonFactoryMock
192+
'resultJsonFactory' => $this->resultJsonFactoryMock,
193+
'invoiceRepository' => $this->invoiceRepository
190194
]
191195
);
192196

@@ -230,8 +234,9 @@ public function testExecute()
230234
$invoiceMock->expects($this->once())
231235
->method('addComment')
232236
->with($data['comment'], false, false);
233-
$invoiceMock->expects($this->once())
234-
->method('save');
237+
$this->invoiceRepository->expects($this->once())
238+
->method('save')
239+
->with($invoiceMock);
235240

236241
$this->invoiceRepository->expects($this->once())
237242
->method('get')
@@ -307,11 +312,11 @@ public function testExecuteModelException()
307312
public function testExecuteException()
308313
{
309314
$response = ['error' => true, 'message' => 'Cannot add new comment.'];
310-
$e = new \Exception('test');
315+
$error = new \Exception('test');
311316

312317
$this->requestMock->expects($this->once())
313318
->method('getParam')
314-
->will($this->throwException($e));
319+
->will($this->throwException($error));
315320

316321
$this->resultJsonFactoryMock->expects($this->once())
317322
->method('create')

0 commit comments

Comments
 (0)