Skip to content

admin checkout agreement controllers refactor #16505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\CheckoutAgreements\Controller\Adminhtml;

use Magento\Framework\App\ObjectManager;
use Magento\CheckoutAgreements\Api\CheckoutAgreementsRepositoryInterface;

abstract class Agreement extends \Magento\Backend\App\Action
{
/**
Expand All @@ -20,15 +23,35 @@ abstract class Agreement extends \Magento\Backend\App\Action
* @var \Magento\Framework\Registry
*/
protected $_coreRegistry = null;

/**
* @var CheckoutAgreementsRepositoryInterface
*/
protected $_agreementRepository;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, inject dependencies in classes when they are really needed.
Make new properties private and try to avoid usage leading underscore in var and methods name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been fixed.


/**
* @var \Magento\CheckoutAgreements\Model\AgreementFactory
*/
protected $_agreementFactory;

/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\Registry $coreRegistry
* @param CheckoutAgreementsRepositoryInterface $agreementRepository
* @param \Magento\CheckoutAgreements\Model\AgreementFactory $agreementFactory
* @codeCoverageIgnore
*/
public function __construct(\Magento\Backend\App\Action\Context $context, \Magento\Framework\Registry $coreRegistry)
{
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Registry $coreRegistry,
CheckoutAgreementsRepositoryInterface $agreementRepository = null,
\Magento\CheckoutAgreements\Model\AgreementFactory $agreementFactory = null
) {
$this->_coreRegistry = $coreRegistry;
$this->_agreementRepository = $agreementRepository ?:
ObjectManager::getInstance()->get(CheckoutAgreementsRepositoryInterface::class);
$this->_agreementFactory = $agreementFactory ?:
ObjectManager::getInstance()->get(\Magento\CheckoutAgreements\Model\AgreementFactory::class);
parent::__construct($context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ class Delete extends \Magento\CheckoutAgreements\Controller\Adminhtml\Agreement
public function execute()
{
$id = (int)$this->getRequest()->getParam('id');
$model = $this->_objectManager->get(\Magento\CheckoutAgreements\Model\Agreement::class)->load($id);
if (!$model->getId()) {
$repository = $this->_agreementRepository->get($id);
if (!$repository->getAgreementId()) {
$this->messageManager->addError(__('This condition no longer exists.'));
$this->_redirect('checkout/*/');
return;
}

try {
$model->delete();
$repository->delete();
$this->messageManager->addSuccess(__('You deleted the condition.'));
$this->_redirect('checkout/*/');
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Edit extends \Magento\CheckoutAgreements\Controller\Adminhtml\Agreement
public function execute()
{
$id = $this->getRequest()->getParam('id');
$agreementModel = $this->_objectManager->create(\Magento\CheckoutAgreements\Model\Agreement::class);
$agreementModel = $this->_agreementFactory->create();

if ($id) {
$agreementModel->load($id);
Expand All @@ -26,7 +26,7 @@ public function execute()
}
}

$data = $this->_objectManager->get(\Magento\Backend\Model\Session::class)->getAgreementData(true);
$data = $this->_session->getAgreementData(true);
if (!empty($data)) {
$agreementModel->setData($data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function execute()
{
$postData = $this->getRequest()->getPostValue();
if ($postData) {
$model = $this->_objectManager->get(\Magento\CheckoutAgreements\Model\Agreement::class);
$model = $this->_agreementFactory->create();
$model->setData($postData);

try {
Expand All @@ -36,7 +36,7 @@ public function execute()
$this->messageManager->addError(__('Something went wrong while saving this condition.'));
}

$this->_objectManager->get(\Magento\Backend\Model\Session::class)->setAgreementData($postData);
$this->_session->setAgreementData($postData);
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl($this->getUrl('*')));
}
}
Expand Down