VertexUsageDeterminer.php 2.32 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
<?php
/**
 * @copyright  Vertex. All rights reserved.  https://www.vertexinc.com/
 * @author     Mediotype                     https://www.mediotype.com/
 */

namespace Vertex\Tax\Model;

use Magento\Customer\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\AddressInterface as QuoteAddressInterface;

/**
 * Business logic for determining if Vertex should be used for tax calculation
 */
class VertexUsageDeterminer
{
    /** @var AddressDeterminer */
    private $addressDeterminer;

    /** @var Config */
    private $config;

    /** @var CountryGuard */
    private $countryGuard;

    /**
     * @param Config $config
     * @param CountryGuard $countryGuard
     * @param AddressDeterminer $addressDeterminer
     */
    public function __construct(Config $config, CountryGuard $countryGuard, AddressDeterminer $addressDeterminer)
    {
        $this->config = $config;
        $this->countryGuard = $countryGuard;
        $this->addressDeterminer = $addressDeterminer;
    }

    /**
     * Determine whether or not to use Vertex to calculate taxes for an address
     *
     * @param string|null $storeCode
     * @param AddressInterface|null $address
     * @param int|null $customerId
     * @param bool $isVirtual
     * @param bool $checkCalculation
     * @return bool
     */
    public function shouldUseVertex(
        $storeCode = null,
        $address = null,
        $customerId = null,
        $isVirtual = false,
        $checkCalculation = false
    ) {
        if (!$this->config->isVertexActive($storeCode)
            || ($checkCalculation && !$this->config->isTaxCalculationEnabled($storeCode))
        ) {
            return false;
        }
        if ($address !== null && !($address instanceof AddressInterface || $address instanceof QuoteAddressInterface)) {
            throw new \InvalidArgumentException(
                '$address must be a Customer or Quote Address.  Is: '
                .(is_object($address) ? get_class($address) : gettype($address))
            );
        }
        $address = $this->addressDeterminer->determineAddress($address, $customerId, $isVirtual);

        return !$this->config->isDisplayPriceInCatalogEnabled($storeCode)
            && $address !== null
            && $address->getCountryId()
            && $this->countryGuard->isCountryIdServiceableByVertex($address->getCountryId());
    }
}