PaymentTokenFactory.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Vault\Model;
  7. use Magento\Framework\ObjectManagerInterface;
  8. use Magento\Vault\Api\Data\PaymentTokenFactoryInterface;
  9. use Magento\Vault\Api\Data\PaymentTokenInterface;
  10. /**
  11. * PaymentTokenFactory class
  12. * @api
  13. * @since 101.0.0
  14. */
  15. class PaymentTokenFactory implements PaymentTokenFactoryInterface
  16. {
  17. /**
  18. * @var array
  19. */
  20. private $tokenTypes = [];
  21. /**
  22. * @var ObjectManagerInterface
  23. */
  24. private $objectManager;
  25. /**
  26. * PaymentTokenFactory constructor.
  27. * @param ObjectManagerInterface $objectManager
  28. * @param array $tokenTypes
  29. */
  30. public function __construct(ObjectManagerInterface $objectManager, array $tokenTypes = [])
  31. {
  32. $this->objectManager = $objectManager;
  33. $this->tokenTypes = $tokenTypes;
  34. }
  35. /**
  36. * Create payment token entity
  37. * @param $type string
  38. * @return PaymentTokenInterface
  39. * @since 101.0.0
  40. */
  41. public function create($type = null)
  42. {
  43. /**
  44. * This code added for Backward Compatibility reasons only, as previous implementation of Code Generated factory
  45. * accepted an array as any other code generated factory
  46. */
  47. if (is_array($type)) {
  48. return $this->objectManager->create(
  49. PaymentTokenInterface::class,
  50. $type
  51. );
  52. }
  53. if ($type !== null && !in_array($type, $this->tokenTypes, true)) {
  54. throw new \LogicException('There is no such payment token type: ' . $type);
  55. }
  56. return $this->objectManager->create(
  57. PaymentTokenInterface::class,
  58. ['data' => [PaymentTokenInterface::TYPE => $type]]
  59. );
  60. }
  61. }