ClientFactory.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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\Client;
  17. use Amazon\Core\Helper\Data;
  18. use Magento\Framework\ObjectManagerInterface;
  19. use Magento\Store\Model\ScopeInterface;
  20. use Psr\Log\LoggerAwareInterface;
  21. use Psr\Log\LoggerInterface;
  22. class ClientFactory implements ClientFactoryInterface
  23. {
  24. /**
  25. * @var ObjectManagerInterface
  26. */
  27. private $objectManager;
  28. /**
  29. * @var Data
  30. */
  31. private $coreHelper;
  32. /**
  33. * @var string
  34. */
  35. private $instanceName;
  36. /**
  37. * @var LoggerInterface
  38. */
  39. private $logger;
  40. /**
  41. * ClientFactory constructor.
  42. *
  43. * @param ObjectManagerInterface $objectManager
  44. * @param Data $coreHelper
  45. * @param LoggerInterface $logger
  46. * @param string $instanceName
  47. */
  48. public function __construct(
  49. ObjectManagerInterface $objectManager,
  50. Data $coreHelper,
  51. LoggerInterface $logger,
  52. $instanceName = '\\AmazonPay\\ClientInterface'
  53. ) {
  54. $this->objectManager = $objectManager;
  55. $this->coreHelper = $coreHelper;
  56. $this->instanceName = $instanceName;
  57. $this->logger = $logger;
  58. }
  59. /**
  60. * {@inheritDoc}
  61. */
  62. public function create($scopeId = null, $scope = ScopeInterface::SCOPE_STORE)
  63. {
  64. $config = [
  65. $this->coreHelper->getClientPath('secretkey') => $this->coreHelper->getSecretKey($scope, $scopeId),
  66. $this->coreHelper->getClientPath('accesskey') => $this->coreHelper->getAccessKey($scope, $scopeId),
  67. $this->coreHelper->getClientPath('merchantid') => $this->coreHelper->getMerchantId($scope, $scopeId),
  68. $this->coreHelper->getClientPath('amazonregion') => $this->coreHelper->getRegion($scope, $scopeId),
  69. $this->coreHelper->getClientPath('amazonsandbox') => $this->coreHelper->isSandboxEnabled($scope, $scopeId),
  70. $this->coreHelper->getClientPath('clientid') => $this->coreHelper->getClientId($scope, $scopeId)
  71. ];
  72. $client = $this->objectManager->create($this->instanceName, ['amazonConfig' => $config]);
  73. if ($client instanceof LoggerAwareInterface && $this->coreHelper->isLoggingEnabled($scope, $scopeId)) {
  74. $client->setLogger($this->logger);
  75. }
  76. return $client;
  77. }
  78. }