Save.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Controller\Adminhtml\Settings\Checkout;
  6. use Magento\Backend\App\Action;
  7. use Magento\Backend\App\Action\Context;
  8. use Magento\Framework\Controller\Result\Raw;
  9. use Magento\Framework\Controller\ResultInterface;
  10. use Magento\Framework\Controller\ResultFactory;
  11. use Magento\Framework\Serialize\Serializer\Json;
  12. use Temando\Shipping\Model\Config\ModuleConfigInterface;
  13. /**
  14. * Save Checkout Settings Action
  15. *
  16. * @package Temando\Shipping\Controller
  17. * @author Max Melzer <max.melzer@netresearch.de>
  18. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. * @link http://www.temando.com/
  20. */
  21. class Save extends Action
  22. {
  23. /**
  24. * @var Json
  25. */
  26. private $decoder;
  27. /**
  28. * @var ModuleConfigInterface
  29. */
  30. private $moduleConfig;
  31. /**
  32. * Save constructor.
  33. * @param Action\Context $context
  34. * @param Json $decoder
  35. * @param ModuleConfigInterface $moduleConfig
  36. */
  37. public function __construct(
  38. Context $context,
  39. Json $decoder,
  40. ModuleConfigInterface $moduleConfig
  41. ) {
  42. $this->moduleConfig = $moduleConfig;
  43. $this->decoder = $decoder;
  44. parent::__construct($context);
  45. }
  46. /**
  47. * @return ResultInterface
  48. */
  49. public function execute()
  50. {
  51. /** @var Raw $rawResponse */
  52. $rawResponse = $this->resultFactory->create(ResultFactory::TYPE_RAW);
  53. $fieldsDefinition = $this->getRequest()->getParam('fields', '[]');
  54. try {
  55. // sanitize input
  56. $this->decoder->unserialize($fieldsDefinition);
  57. $this->moduleConfig->saveCheckoutFieldsDefinition($fieldsDefinition);
  58. $rawResponse->setContents('OK');
  59. } catch (\InvalidArgumentException $e) {
  60. $rawResponse->setHttpResponseCode(422);
  61. $rawResponse->setContents('NOK');
  62. }
  63. return $rawResponse;
  64. }
  65. }