DeferredShippingMethodChooserPool.php 1.39 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\InstantPurchase\Model\ShippingMethodChoose;

/**
 * Container to register available deferred shipping method choosers.
 * Use deferred shipping method code as a key for a deferred chooser.
 *
 * @api
 * @since 100.2.0
 */
class DeferredShippingMethodChooserPool
{
    private $choosers;

    /**
     * @param DeferredShippingMethodChooserInterface[] $choosers
     */
    public function __construct(array $choosers)
    {
        foreach ($choosers as $chooser) {
            if (!$chooser instanceof DeferredShippingMethodChooserInterface) {
                throw new \InvalidArgumentException(sprintf(
                    'Invalid configuration. Chooser should be instance of %s.',
                    DeferredShippingMethodChooserInterface::class
                ));
            }
        }
        $this->choosers = $choosers;
    }

    /**
     * @param string $type
     * @return DeferredShippingMethodChooserInterface
     * @since 100.2.0
     */
    public function get($type) : DeferredShippingMethodChooserInterface
    {
        if (!isset($this->choosers[$type])) {
            throw new \InvalidArgumentException(sprintf(
                'Deferred shipping method %s is not registered.',
                $type
            ));
        }

        return $this->choosers[$type];
    }
}