PaymentConfigSaveAfter.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Core\Observer;
  17. use Amazon\Core\Helper\Data;
  18. use Amazon\Core\Model\Validation\ApiCredentialsValidatorFactory;
  19. use Amazon\Core\Model\Config\Credentials\Json;
  20. use Magento\Framework\Event\Observer;
  21. use Magento\Framework\Event\ObserverInterface;
  22. use Magento\Framework\Message\ManagerInterface;
  23. use Magento\Framework\App\Config\ReinitableConfigInterface;
  24. use Magento\Framework\App\RequestInterface;
  25. use Magento\Store\Model\ScopeInterface;
  26. use Magento\Framework\App\Config\Storage\WriterInterface;
  27. class PaymentConfigSaveAfter implements ObserverInterface
  28. {
  29. /**
  30. * @var ApiCredentialsValidatorFactory
  31. */
  32. private $apiCredentialsValidatorFactory;
  33. /**
  34. * @var ManagerInterface
  35. */
  36. private $messageManager;
  37. /**
  38. * @var Json
  39. */
  40. private $jsonCredentials;
  41. /**
  42. * @var Data
  43. */
  44. private $amazonCoreHelper;
  45. /**
  46. * Application config
  47. *
  48. * @var ReinitableConfigInterface
  49. */
  50. private $appConfig;
  51. /**
  52. * @var RequestInterface
  53. */
  54. private $request;
  55. /**
  56. * PaymentConfigSaveAfter constructor.
  57. *
  58. * @param ApiCredentialsValidatorFactory $apiCredentialsValidatorFactory
  59. * @param ManagerInterface $messageManager
  60. * @param Json $jsonCredentials
  61. * @param Data $amazonCoreHelper
  62. * @param ReinitableConfigInterface $config
  63. * @param RequestInterface $request
  64. */
  65. public function __construct(
  66. ApiCredentialsValidatorFactory $apiCredentialsValidatorFactory,
  67. ManagerInterface $messageManager,
  68. Json $jsonCredentials,
  69. Data $amazonCoreHelper,
  70. ReinitableConfigInterface $config,
  71. RequestInterface $request
  72. ) {
  73. $this->apiCredentialsValidatorFactory = $apiCredentialsValidatorFactory;
  74. $this->messageManager = $messageManager;
  75. $this->amazonCoreHelper = $amazonCoreHelper;
  76. $this->jsonCredentials = $jsonCredentials;
  77. $this->appConfig = $config;
  78. $this->request = $request;
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function execute(Observer $observer)
  84. {
  85. if (!$this->request->getParam('amazon_test_creds')) {
  86. return;
  87. }
  88. $scopeData = $this->getScopeData($observer);
  89. $jsonCredentials = $this->amazonCoreHelper->getCredentialsJson($scopeData['scope'], $scopeData['scope_id']);
  90. if (!empty($jsonCredentials)) {
  91. $this->appConfig->reinit();
  92. $this->jsonCredentials->processCredentialsJson($jsonCredentials, $scopeData);
  93. }
  94. /** @see \Magento\Config\Model\Config::save() */
  95. $validator = $this->apiCredentialsValidatorFactory->create();
  96. $messageManagerMethod = 'addErrorMessage';
  97. if ($validator->isValid($scopeData['scope_id'], $scopeData['scope'])) {
  98. $messageManagerMethod = 'addSuccessMessage';
  99. }
  100. foreach ($validator->getMessages() as $message) {
  101. $this->messageManager->$messageManagerMethod($message);
  102. }
  103. }
  104. protected function getScopeData($observer)
  105. {
  106. $scopeData = [];
  107. $scopeData['scope'] = 'default';
  108. $scopeData['scope_id'] = null;
  109. $website = $observer->getWebsite();
  110. $store = $observer->getStore();
  111. if ($website) {
  112. $scopeData['scope'] = ScopeInterface::SCOPE_WEBSITES;
  113. $scopeData['scope_id'] = $website;
  114. }
  115. if ($store) {
  116. $scopeData['scope'] = ScopeInterface::SCOPE_STORES;
  117. $scopeData['scope_id'] = $store;
  118. }
  119. return $scopeData;
  120. }
  121. }