CommandManager.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Gateway\Command;
  7. use Magento\Framework\Exception\NotFoundException;
  8. use Magento\Payment\Gateway\Command;
  9. use Magento\Payment\Gateway\CommandInterface;
  10. use Magento\Payment\Gateway\Data\PaymentDataObjectFactoryInterface;
  11. use Magento\Payment\Model\InfoInterface;
  12. /**
  13. * Class CommandManager
  14. * @package Magento\Payment\Gateway\Command
  15. * @api
  16. * @since 100.1.0
  17. */
  18. class CommandManager implements CommandManagerInterface
  19. {
  20. /**
  21. * @var CommandPoolInterface
  22. */
  23. private $commandPool;
  24. /**
  25. * @var PaymentDataObjectFactoryInterface
  26. */
  27. private $paymentDataObjectFactory;
  28. /**
  29. * CommandExecutor constructor.
  30. * @param CommandPoolInterface $commandPool
  31. * @param PaymentDataObjectFactoryInterface $paymentDataObjectFactory
  32. */
  33. public function __construct(
  34. CommandPoolInterface $commandPool,
  35. PaymentDataObjectFactoryInterface $paymentDataObjectFactory
  36. ) {
  37. $this->commandPool = $commandPool;
  38. $this->paymentDataObjectFactory = $paymentDataObjectFactory;
  39. }
  40. /**
  41. * Executes command by code
  42. *
  43. * @param string $commandCode
  44. * @param InfoInterface|null $payment
  45. * @param array $arguments
  46. * @return ResultInterface|null
  47. * @throws NotFoundException
  48. * @throws CommandException
  49. * @since 100.1.0
  50. */
  51. public function executeByCode($commandCode, InfoInterface $payment = null, array $arguments = [])
  52. {
  53. $commandSubject = $arguments;
  54. if ($payment !== null) {
  55. $commandSubject['payment'] = $this->paymentDataObjectFactory->create($payment);
  56. }
  57. return $this->commandPool
  58. ->get($commandCode)
  59. ->execute($commandSubject);
  60. }
  61. /**
  62. * Executes command
  63. *
  64. * @param CommandInterface $command
  65. * @param InfoInterface|null $payment
  66. * @param array $arguments
  67. * @return ResultInterface|null
  68. * @throws CommandException
  69. * @since 100.1.0
  70. */
  71. public function execute(CommandInterface $command, InfoInterface $payment = null, array $arguments = [])
  72. {
  73. $commandSubject = $arguments;
  74. if ($payment !== null) {
  75. $commandSubject['payment'] = $this->paymentDataObjectFactory->create($payment);
  76. }
  77. return $command->execute($commandSubject);
  78. }
  79. /**
  80. * Retrieves operation
  81. *
  82. * @param string $commandCode
  83. * @return CommandInterface
  84. * @throws NotFoundException
  85. * @since 100.1.0
  86. */
  87. public function get($commandCode)
  88. {
  89. return $this->commandPool->get($commandCode);
  90. }
  91. }