ServiceActionPerformerFactoryPlugin.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Model\Plugin;
  7. use Magento\Framework\ObjectManagerInterface;
  8. use Vertex\Utility\ServiceActionPerformer;
  9. use Vertex\Utility\ServiceActionPerformerFactory;
  10. use Vertex\Utility\SoapClientFactory;
  11. /**
  12. * Replaces a Vertex SDK Factory with Magento 2 dependency injection
  13. */
  14. class ServiceActionPerformerFactoryPlugin
  15. {
  16. /** @var ObjectManagerInterface */
  17. private $objectManager;
  18. /**
  19. * @param ObjectManagerInterface $objectManager
  20. */
  21. public function __construct(ObjectManagerInterface $objectManager)
  22. {
  23. $this->objectManager = $objectManager;
  24. }
  25. /**
  26. * Change Vertex SDK's Factory to utilize ObjectManager
  27. *
  28. * @todo Convert to afterCreate once we only support Magento 2.2+
  29. *
  30. * @param ServiceActionPerformerFactory $factory
  31. * @param callable $proceed
  32. * @param array $parameters
  33. * @return ServiceActionPerformer
  34. */
  35. public function aroundCreate(ServiceActionPerformerFactory $factory, callable $proceed, array $parameters)
  36. {
  37. // Call the original to trigger its checks & exceptions
  38. $performer = $proceed($parameters);
  39. unset($performer);
  40. if (!isset($parameters['soapClientFactory'])) {
  41. // This is necessary to ensure that the plugins for the SoapClientFactory are utilized
  42. $parameters['soapClientFactory'] = $this->objectManager->get(SoapClientFactory::class);
  43. }
  44. return $this->objectManager->create(ServiceActionPerformer::class, $parameters);
  45. }
  46. }