ModuleConfig.php 12.7 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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
<?php
/**
 * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
 */
namespace Temando\Shipping\Model\Config;

use Magento\Framework\DataObjectFactory;
use Magento\Framework\Module\ModuleList;
use Temando\Shipping\Webservice\Config\WsConfigInterface;

/**
 * Temando Config Values Handler
 *
 * @package Temando\Shipping\Model
 * @author  Sebastian Ertner <sebastian.ertner@netresearch.de>
 * @author  Christoph Aßmann <christoph.assmann@netresearch.de>
 * @author  Max Melzer <max.melzer@netresearch.de>
 * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 * @link    https://www.temando.com/
 */
class ModuleConfig implements ModuleConfigInterface, WsConfigInterface
{
    const CONFIG_XML_PATH_CHECKOUT_ENABLED = 'carriers/temando/active';
    const CONFIG_XML_PATH_LOGGING_ENABLED = 'carriers/temando/logging_enabled';

    const CONFIG_XML_PATH_SESSION_ENDPOINT = 'carriers/temando/session_endpoint';
    const CONFIG_XML_PATH_API_ENDPOINT = 'carriers/temando/sovereign_endpoint';
    const CONFIG_XML_PATH_API_VERSION = 'carriers/temando/api_version';
    const CONFIG_XML_PATH_ACCOUNT_ID = 'carriers/temando/account_id';
    const CONFIG_XML_PATH_BEARER_TOKEN = 'carriers/temando/bearer_token';

    const CONFIG_XML_PATH_BEARER_TOKEN_EXPIRY = 'carriers/temando/bearer_token_expiry';

    const CONFIG_XML_PATH_SYNC_ENABLED = 'carriers/temando/sync_enabled';
    const CONFIG_XML_PATH_SYNC_SHIPMENTS_ENABLED = 'carriers/temando/sync_shipments_enabled';
    const CONFIG_XML_PATH_SYNC_ORDERS_ENABLED = 'carriers/temando/sync_orders_enabled';
    const CONFIG_XML_PATH_SYNC_STREAM_ID = 'carriers/temando/sync_stream_id';

    const CONFIG_XML_PATH_CHECKOUT_FIELDS = 'carriers/temando/additional_checkout_fields';
    const CONFIG_XML_PATH_REGISTER_ACCOUNT_URL = 'carriers/temando/register_account_url';
    const CONFIG_XML_PATH_SHIPPING_PORTAL_URL = 'carriers/temando/shipping_portal_url';

    const CONFIG_XML_PATH_TEMANDO_RETURNS_ACTIVE = 'carriers/temando/rma_enabled';

    const CONFIG_XML_PATH_COLLECTION_POINTS_ENABLED = 'carriers/temando/collectionpoints_enabled';
    const CONFIG_XML_PATH_COLLECTION_POINTS_COUNTRIES = 'carriers/temando/collectionpoints_countries';

    const CONFIG_XML_PATH_CLICK_AND_COLLECT_ENABLED = 'carriers/temando/clickandcollect_enabled';
    const CONFIG_XML_PATH_CLICK_AND_COLLECT_COUNTRIES = 'carriers/temando/clickandcollect_countries';

    /**
     * @var DataObjectFactory
     */
    private $dataObjectFactory;

    /**
     * @var ConfigAccessor
     */
    private $configAccessor;

    /**
     * @var ModuleList
     */
    private $moduleList;

    /**
     * ModuleConfig constructor.
     *
     * @param DataObjectFactory $dataObjectFactory
     * @param ConfigAccessor $configAccessor
     * @param ModuleList $moduleList
     */
    public function __construct(
        DataObjectFactory $dataObjectFactory,
        ConfigAccessor $configAccessor,
        ModuleList $moduleList
    ) {
        $this->dataObjectFactory = $dataObjectFactory;
        $this->configAccessor = $configAccessor;
        $this->moduleList = $moduleList;
    }

    /**
     * Check if shipping module is enabled in checkout.
     *
     * @param int $storeId
     *
     * @return bool
     */
    public function isEnabled($storeId = null)
    {
        return (bool) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_CHECKOUT_ENABLED, $storeId);
    }

    /**
     * Check if webservice communication logging is enabled.
     *
     * @return bool
     */
    public function isLoggingEnabled()
    {
        return (bool) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_LOGGING_ENABLED);
    }

    /**
     * @param int $storeId
     * @return \Magento\Framework\DataObject
     */
    public function getStoreInformation($storeId = null)
    {
        $storeInformation = $this->configAccessor->getConfigValue(
            'general/store_information',
            $storeId
        );
        $storeInfo = $this->dataObjectFactory->create(['data' => (array)$storeInformation]);

        return $storeInfo;
    }

    /**
     * @param int $storeId
     * @return \Magento\Framework\DataObject
     */
    public function getShippingOrigin($storeId = null)
    {
        $shippingOrigin = $this->configAccessor->getConfigValue(
            'shipping/origin',
            $storeId
        );
        $storeInfo = $this->dataObjectFactory->create(['data' => (array)$shippingOrigin]);

        return $storeInfo;
    }

    /**
     * @param int $storeId
     * @return string
     */
    public function getWeightUnit($storeId = null)
    {
        return $this->configAccessor->getConfigValue('general/locale/weight_unit', $storeId);
    }

    /**
     * Obtain Register Account Url.
     *
     * @return string
     */
    public function getRegisterAccountUrl()
    {
        return (string) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_REGISTER_ACCOUNT_URL);
    }

    /**
     * Obtain Shipping Portal Url.
     *
     * @return string
     */
    public function getShippingPortalUrl()
    {
        return (string) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_SHIPPING_PORTAL_URL);
    }

    /**
     * Check if merchant registered an account at Temando.
     *
     * @return bool
     */
    public function isRegistered()
    {
        return ($this->getAccountId() !== '') && ($this->getBearerToken() !== '');
    }

    /**
     * Read URL of Temando REST API.
     *
     * @return string
     */
    public function getSessionEndpoint()
    {
        return $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_SESSION_ENDPOINT);
    }

    /**
     * Read URL of Temando REST API.
     *
     * @return string
     */
    public function getApiEndpoint()
    {
        return $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_API_ENDPOINT);
    }

    /**
     * Save URL of Temando REST API.
     *
     * @param string $apiEndpoint
     * @return void
     */
    public function saveApiEndpoint($apiEndpoint)
    {
        $this->configAccessor->saveConfigValue(self::CONFIG_XML_PATH_API_ENDPOINT, $apiEndpoint);
    }

    /**
     * Obtain the API version to connect to.
     *
     * @return string
     */
    public function getApiVersion()
    {
        return (string) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_API_VERSION);
    }

    /**
     * Read Temando Account Id.
     *
     * @return string
     */
    public function getAccountId()
    {
        return (string) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_ACCOUNT_ID);
    }

    /**
     * Save Temando Account Id.
     *
     * @param string $accountId
     * @return void
     */
    public function saveAccountId($accountId)
    {
        $this->configAccessor->saveConfigValue(self::CONFIG_XML_PATH_ACCOUNT_ID, $accountId);
    }

    /**
     * Read Temando Authentication Token.
     *
     * @return string
     */
    public function getBearerToken()
    {
        return (string)$this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_BEARER_TOKEN);
    }

    /**
     * Save Temando Authentication Token.
     *
     * @param string $bearerToken
     * @return void
     */
    public function saveBearerToken($bearerToken)
    {
        $this->configAccessor->saveConfigValue(self::CONFIG_XML_PATH_BEARER_TOKEN, $bearerToken);
    }

    /**
     * Read Temando Authentication Token Expiry Timestamp.
     *
     * @return string
     */
    public function getBearerTokenExpiry()
    {
        return (string) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_BEARER_TOKEN_EXPIRY);
    }

    /**
     * Save Temando Authentication Token Expiry Timestamp.
     *
     * @param string $bearerTokenExpiry
     * @return void
     */
    public function saveBearerTokenExpiry($bearerTokenExpiry)
    {
        $this->configAccessor->saveConfigValue(self::CONFIG_XML_PATH_BEARER_TOKEN_EXPIRY, $bearerTokenExpiry);
    }

    /**
     * Check whether stream events should be processed or not.
     *
     * @return bool
     */
    public function isSyncEnabled()
    {
        return (bool) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_SYNC_ENABLED);
    }

    /**
     * Save new stream event processing configuration.
     *
     * @param string $value
     * @return void
     */
    public function saveSyncEnabled($value)
    {
        $this->configAccessor->saveConfigValue(self::CONFIG_XML_PATH_SYNC_ENABLED, $value);
    }

    /**
     * Check whether shipment events should be processed or not.
     *
     * @return bool
     */
    public function isSyncShipmentEnabled()
    {
        return (bool) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_SYNC_SHIPMENTS_ENABLED);
    }

    /**
     * Save new stream event processing configuration.
     *
     * @param string $value
     * @return void
     */
    public function saveSyncShipmentEnabled($value)
    {
        $this->configAccessor->saveConfigValue(self::CONFIG_XML_PATH_SYNC_SHIPMENTS_ENABLED, $value);
    }

    /**
     * Check whether order events should be processed or not.
     *
     * @return bool
     */
    public function isSyncOrderEnabled()
    {
        return (bool) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_SYNC_ORDERS_ENABLED);
    }

    /**
     * Save new stream event processing configuration.
     *
     * @param string $value
     * @return void
     */
    public function saveSyncOrderEnabled($value)
    {
        $this->configAccessor->saveConfigValue(self::CONFIG_XML_PATH_SYNC_ORDERS_ENABLED, $value);
    }

    /**
     * Multiple streams may exists. Obtain the stream ID to request events from.
     *
     * @return string
     */
    public function getStreamId()
    {
        return (string) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_SYNC_STREAM_ID);
    }

    /**
     * Get checkout field definitions. This is the plain serialized string as stored in config.
     *
     * @return string
     */
    public function getCheckoutFieldsDefinition()
    {
        $checkoutFields = (string) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_CHECKOUT_FIELDS);
        if (!$checkoutFields) {
            $checkoutFields = '[]';
        }

        return $checkoutFields;
    }

    /**
     * Check if RMA feature is enabled.
     *
     * @return bool
     */
    public function isRmaEnabled()
    {
        return (bool) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_TEMANDO_RETURNS_ACTIVE);
    }

    /**
     * Check if RMA module is installed
     *
     * @return bool
     */
    public function isRmaAvailable()
    {
        return (bool) $this->moduleList->has('Magento_Rma');
    }

    /**
     * Save checkout field definitions in config.
     *
     * @param string $fieldsDefinition
     * @return void
     */
    public function saveCheckoutFieldsDefinition($fieldsDefinition)
    {
        $this->configAccessor->saveConfigValue(self::CONFIG_XML_PATH_CHECKOUT_FIELDS, $fieldsDefinition);
    }

    /**
     * Check if collection points feature is enabled in config.
     *
     * @param int $storeId
     *
     * @return bool
     */
    public function isCollectionPointsEnabled($storeId = null)
    {
        return (bool) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_COLLECTION_POINTS_ENABLED, $storeId);
    }

    /**
     * Obtain country codes enabled for collection point deliveries.
     *
     * @param int $storeId
     *
     * @return string[]
     */
    public function getCollectionPointDeliveryCountries($storeId = null)
    {
        return $this->configAccessor->getConfigValue(
            self::CONFIG_XML_PATH_COLLECTION_POINTS_COUNTRIES,
            $storeId
        );
    }

    /**
     * Check if click and collect feature is enabled in config.
     *
     * @param int $storeId
     *
     * @return bool
     */
    public function isClickAndCollectEnabled($storeId = null)
    {
        return (bool) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_CLICK_AND_COLLECT_ENABLED, $storeId);
    }

    /**
     * Save new account data.
     *
     * @param string $accountId
     * @param string $bearerToken
     * @param string $bearerTokenExpiry
     * @return void
     */
    public function setAccount($accountId, $bearerToken, $bearerTokenExpiry)
    {
        $this->saveAccountId($accountId);
        $this->saveBearerToken($bearerToken);
        $this->saveBearerTokenExpiry($bearerTokenExpiry);
    }

    /**
     * Unset all account data.
     *
     * @return void
     */
    public function unsetAccount()
    {
        $this->configAccessor->deleteConfigValue(self::CONFIG_XML_PATH_ACCOUNT_ID);
        $this->configAccessor->deleteConfigValue(self::CONFIG_XML_PATH_BEARER_TOKEN);
        $this->configAccessor->deleteConfigValue(self::CONFIG_XML_PATH_BEARER_TOKEN_EXPIRY);
    }
}