DeferredShippingMethodChooserPool.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\InstantPurchase\Model\ShippingMethodChoose;
  7. /**
  8. * Container to register available deferred shipping method choosers.
  9. * Use deferred shipping method code as a key for a deferred chooser.
  10. *
  11. * @api
  12. * @since 100.2.0
  13. */
  14. class DeferredShippingMethodChooserPool
  15. {
  16. private $choosers;
  17. /**
  18. * @param DeferredShippingMethodChooserInterface[] $choosers
  19. */
  20. public function __construct(array $choosers)
  21. {
  22. foreach ($choosers as $chooser) {
  23. if (!$chooser instanceof DeferredShippingMethodChooserInterface) {
  24. throw new \InvalidArgumentException(sprintf(
  25. 'Invalid configuration. Chooser should be instance of %s.',
  26. DeferredShippingMethodChooserInterface::class
  27. ));
  28. }
  29. }
  30. $this->choosers = $choosers;
  31. }
  32. /**
  33. * @param string $type
  34. * @return DeferredShippingMethodChooserInterface
  35. * @since 100.2.0
  36. */
  37. public function get($type) : DeferredShippingMethodChooserInterface
  38. {
  39. if (!isset($this->choosers[$type])) {
  40. throw new \InvalidArgumentException(sprintf(
  41. 'Deferred shipping method %s is not registered.',
  42. $type
  43. ));
  44. }
  45. return $this->choosers[$type];
  46. }
  47. }