SourceSelectionService.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventorySourceSelectionApi\Model;
  8. use Magento\Framework\ObjectManagerInterface;
  9. use Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestInterface;
  10. use Magento\InventorySourceSelectionApi\Api\Data\SourceSelectionResultInterface;
  11. use Magento\InventorySourceSelectionApi\Api\SourceSelectionServiceInterface;
  12. use Magento\InventorySourceSelectionApi\Model\SourceSelectionInterface;
  13. class SourceSelectionService implements SourceSelectionServiceInterface
  14. {
  15. /**
  16. * @var ObjectManagerInterface
  17. */
  18. private $objectManager;
  19. /**
  20. * @var array
  21. */
  22. private $sourceSelectionMethods;
  23. /**
  24. * @param ObjectManagerInterface $objectManager
  25. * @param array $sourceSelectionMethods
  26. * @SuppressWarnings(PHPMD.LongVariable)
  27. */
  28. public function __construct(
  29. ObjectManagerInterface $objectManager,
  30. array $sourceSelectionMethods = []
  31. ) {
  32. $this->objectManager = $objectManager;
  33. $this->sourceSelectionMethods = $sourceSelectionMethods;
  34. }
  35. /**
  36. * @inheritdoc
  37. */
  38. public function execute(
  39. InventoryRequestInterface $inventoryRequest,
  40. string $algorithmCode
  41. ): SourceSelectionResultInterface {
  42. if (!isset($this->sourceSelectionMethods[$algorithmCode])) {
  43. throw new \LogicException(
  44. __('There is no such Source Selection Algorithm implemented: %1', $algorithmCode)
  45. );
  46. }
  47. $sourceSelectionClassName = $this->sourceSelectionMethods[$algorithmCode];
  48. $sourceSelectionAlgorithm = $this->objectManager->create($sourceSelectionClassName);
  49. if (false === $sourceSelectionAlgorithm instanceof SourceSelectionInterface) {
  50. throw new \LogicException(
  51. __('%1 doesn\'t implement SourceSelectionInterface', $sourceSelectionClassName)
  52. );
  53. }
  54. return $sourceSelectionAlgorithm->execute($inventoryRequest);
  55. }
  56. }