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

namespace Vertex\Tax\Model\Api;

use Magento\Store\Model\ScopeInterface;
use Vertex\Data\ConfigurationInterface;
use Vertex\Data\ConfigurationInterfaceFactory;
use Vertex\Data\LoginInterface;
use Vertex\Data\LoginInterfaceFactory;
use Vertex\Tax\Model\Config as ModuleConfig;

/**
 * Creates a {@see ConfigurationInterface} for use with the Vertex API library
 */
class ConfigBuilder
{
    /** @var ConfigurationInterfaceFactory */
    private $configFactory;

    /** @var LoginInterfaceFactory */
    private $loginFactory;

    /** @var ModuleConfig */
    private $moduleConfig;

    /** @var string|null */
    private $scopeCode;

    /** @var string */
    private $scopeType = ScopeInterface::SCOPE_STORE;

    public function __construct(
        ModuleConfig $moduleConfig,
        ConfigurationInterfaceFactory $configFactory,
        LoginInterfaceFactory $loginFactory
    ) {
        $this->moduleConfig = $moduleConfig;
        $this->configFactory = $configFactory;
        $this->loginFactory = $loginFactory;
    }

    /**
     * Create a {@see ConfigurationInterface} object for use with the Vertex API
     *
     * @return ConfigurationInterface
     */
    public function build()
    {
        /** @var ConfigurationInterface $configuration */
        $configuration = $this->configFactory->create();

        /** @var LoginInterface $login */
        $login = $this->loginFactory->create();

        $login->setTrustedId($this->moduleConfig->getTrustedId($this->scopeCode, $this->scopeType));

        $configuration->setLogin($login);
        $configuration->setTaxAreaLookupWsdl($this->getTaxAreaLookupWsdl());
        $configuration->setTaxCalculationWsdl($this->getTaxCalculationWsdl());

        return $configuration;
    }

    /**
     * Set the Scope Code
     *
     * @param string|null $scopeCode
     * @return ConfigBuilder
     */
    public function setScopeCode($scopeCode)
    {
        $this->scopeCode = $scopeCode;
        return $this;
    }

    /**
     * Set the Scope Type
     *
     * @param string|null $scopeType
     * @return ConfigBuilder
     */
    public function setScopeType($scopeType)
    {
        $this->scopeType = $scopeType;
        return $this;
    }

    /**
     * Assemble a URL
     *
     * @param string[] $urlParts indexed as parse_url would index them
     * @return string
     */
    private function assembleUrl($urlParts)
    {
        $url = $urlParts['scheme'] . '://' . $urlParts['host'] . $urlParts['path'];
        if (isset($urlParts['query'])) {
            $url .= '?' . $urlParts['query'];
        }
        if (isset($urlParts['fragment'])) {
            $url .= '#' . $urlParts['fragment'];
        }
        return $url;
    }

    /**
     * Add a WSDL query parameter if one does not exist on the URL
     *
     * @param string $url
     * @return string
     */
    private function ensureWsdlQuery($url)
    {
        $urlParts = parse_url($url);
        $query = isset($urlParts['query']) ? $urlParts['query'] : null;
        $wsdlFound = false;

        if ($query !== null) {
            $queryParts = explode('&', $query);
            foreach ($queryParts as $parameter) {
                $parameterParts = explode('=', $parameter);
                $name = $parameterParts[0];
                if (strtolower($name) === 'wsdl') {
                    $wsdlFound = true;
                    break;
                }
            }
        }

        if (!$wsdlFound) {
            $urlParts['query'] = $query . (empty($query) ? 'wsdl' : '&wsdl');
        }

        return $this->assembleUrl($urlParts);
    }

    /**
     * Retrieve the Tax Area Lookup WSDL URL
     *
     * @return string
     */
    private function getTaxAreaLookupWsdl()
    {
        return $this->ensureWsdlQuery($this->moduleConfig->getVertexAddressHost($this->scopeCode, $this->scopeType));
    }

    /**
     * Retrieve the Tax Calculation WSDL URL
     *
     * @return string
     */
    private function getTaxCalculationWsdl()
    {
        return $this->ensureWsdlQuery($this->moduleConfig->getVertexHost($this->scopeCode, $this->scopeType));
    }
}