InstantPurchaseOption.php 4.45 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\InstantPurchase\Model;

use Magento\Customer\Model\Address;
use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Api\Data\ShippingMethodInterface;
use Magento\Vault\Api\Data\PaymentTokenInterface;
use InvalidArgumentException;

/**
 * Option to make instant purchase.
 *
 * @api
 * @since 100.2.0
 */
class InstantPurchaseOption
{
    /**
     * @var PaymentTokenInterface|null
     */
    private $paymentToken;

    /**
     * @var Address|null
     */
    private $shippingAddress;

    /**
     * @var Address|null
     */
    private $billingAddress;

    /**
     * @var ShippingMethodInterface|null
     */
    private $shippingMethod;

    /**
     * InstantPurchaseOption constructor.
     * @param PaymentTokenInterface|null $paymentToken
     * @param Address|null $shippingAddress
     * @param Address|null $billingAddress
     * @param ShippingMethodInterface|null $shippingMethod
     * @throws InvalidArgumentException if invalid data provided (implementation error)
     *
     * @SuppressWarnings(Magento.TypeDuplication)
     * Type duplication verified.
     * This is not a service class and should not be instantiated directly through Object Manager.
     * Use InstantPurchaseOptionFactory instead.
     */
    public function __construct(
        PaymentTokenInterface $paymentToken = null,
        Address $shippingAddress = null,
        Address $billingAddress = null,
        ShippingMethodInterface $shippingMethod = null
    ) {
        $customers = [];
        if ($paymentToken) {
            $customers[] = $paymentToken->getCustomerId();
        }
        if ($shippingAddress) {
            $customers[] = $shippingAddress->getCustomerId();
        }
        if ($billingAddress) {
            $customers[] = $billingAddress->getCustomerId();
        }
        if (count(array_unique($customers)) > 1) {
            throw new InvalidArgumentException('Provided data does not belong to same customer.');
        }

        $this->paymentToken = $paymentToken;
        $this->shippingAddress = $shippingAddress;
        $this->billingAddress = $billingAddress;
        $this->shippingMethod = $shippingMethod;
    }

    /**
     * Checks if option available
     *
     * @return bool
     * @since 100.2.0
     */
    public function isAvailable(): bool
    {
        return isset(
            $this->paymentToken,
            $this->shippingAddress,
            $this->billingAddress,
            $this->shippingMethod
        ) && $this->shippingMethod->getAvailable();
    }

    /**
     * Returns payment token for instant purchase.
     *
     * @return PaymentTokenInterface
     * @throws LocalizedException if payment token is not defined
     * @since 100.2.0
     */
    public function getPaymentToken(): PaymentTokenInterface
    {
        if (!isset($this->paymentToken)) {
            throw new LocalizedException(
                __("A payment method isn't defined for instance purchase. Verify and try again.")
            );
        }
        return $this->paymentToken;
    }

    /**
     * Returns shipping address for instant purchase.
     *
     * @return Address
     * @throws LocalizedException if shipping address is not defined
     * @since 100.2.0
     */
    public function getShippingAddress(): Address
    {
        if (!isset($this->shippingAddress)) {
            throw new LocalizedException(__('Shipping address is not defined for instance purchase.'));
        }
        return $this->shippingAddress;
    }

    /**
     * Returns billing address for instant purchase.
     *
     * @return Address
     * @throws LocalizedException if billing address is not defined
     * @since 100.2.0
     */
    public function getBillingAddress(): Address
    {
        if (!isset($this->billingAddress)) {
            throw new LocalizedException(__('Billing address is not defined for instance purchase.'));
        }
        return $this->billingAddress;
    }

    /**
     * Returns shipping method for instant purchase.
     *
     * @return ShippingMethodInterface
     * @throws LocalizedException if shipping method is not defined
     * @since 100.2.0
     */
    public function getShippingMethod(): ShippingMethodInterface
    {
        if (!isset($this->shippingMethod)) {
            throw new LocalizedException(__('Shipping method is not defined for instance purchase.'));
        }
        return $this->shippingMethod;
    }
}