IntegrationFactory.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\InstantPurchase\PaymentMethodIntegration;
  7. use Magento\Framework\ObjectManagerInterface;
  8. use Magento\Vault\Model\VaultPaymentInterface;
  9. /**
  10. * Payment method integration facade factory.
  11. */
  12. class IntegrationFactory
  13. {
  14. /**
  15. * @var ObjectManagerInterface
  16. */
  17. private $objectManager;
  18. /**
  19. * IntegrationFactory constructor.
  20. * @param ObjectManagerInterface $objectManager
  21. */
  22. public function __construct(ObjectManagerInterface $objectManager)
  23. {
  24. $this->objectManager = $objectManager;
  25. }
  26. /**
  27. * Creates instance of integration facade.
  28. *
  29. * @param VaultPaymentInterface $paymentMethod
  30. * @param int $storeId
  31. * @return Integration
  32. */
  33. public function create(VaultPaymentInterface $paymentMethod, int $storeId): Integration
  34. {
  35. $config = $paymentMethod->getConfigData('instant_purchase', $storeId);
  36. $availabilityChecker = $this->extractFromConfig(
  37. $config,
  38. 'available',
  39. AvailabilityCheckerInterface::class
  40. );
  41. $paymentTokenFormatter = $this->extractFromConfig(
  42. $config,
  43. 'tokenFormat',
  44. PaymentTokenFormatterInterface::class
  45. );
  46. $paymentAdditionalInformationProvider = $this->extractFromConfig(
  47. $config,
  48. 'additionalInformation',
  49. PaymentAdditionalInformationProviderInterface::class
  50. );
  51. $integration = $this->objectManager->create(Integration::class, [
  52. 'vaultPaymentMethod' => $paymentMethod,
  53. 'availabilityChecker' => $this->objectManager->get($availabilityChecker),
  54. 'paymentTokenFormatter' => $this->objectManager->get($paymentTokenFormatter),
  55. 'paymentAdditionalInformationProvider' => $this->objectManager->get($paymentAdditionalInformationProvider),
  56. ]);
  57. return $integration;
  58. }
  59. /**
  60. * Reads value from config.
  61. *
  62. * @param array $config
  63. * @param string $field
  64. * @param string $default
  65. * @return string
  66. */
  67. private function extractFromConfig($config, string $field, string $default): string
  68. {
  69. return $config[$field] ?? $default;
  70. }
  71. }