Onboarding.php 3.03 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
<?php

/**
 * This file is part of the Klarna KP module
 *
 * (c) Klarna Bank AB (publ)
 *
 * For the full copyright and license information, please view the NOTICE
 * and LICENSE files that were distributed with this source code.
 */

namespace Klarna\Core\Model\System;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ProductMetadata;
use Magento\Framework\App\Request\Http;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
 * Class Onboarding
 *
 * @package Klarna\Core\Model\System
 */
class Onboarding
{

    /** @var ProductMetadata $productMetaData */
    private $productMetaData;

    /** @var ScopeConfigInterface $scopeConfig */
    private $scopeConfig;

    /** @var Http $http */
    private $http;

    /** @var StoreManagerInterface $storeManager */
    private $storeManager;

    /**
     * Onboarding Constructor
     *
     * @param ProductMetadata       $productMetadata
     * @param ScopeConfigInterface  $scopeConfig
     * @param Http                  $http
     * @param StoreManagerInterface $storeManager
     */
    public function __construct(
        ProductMetadata $productMetadata,
        ScopeConfigInterface $scopeConfig,
        Http $http,
        StoreManagerInterface $storeManager
    ) {
        $this->productMetaData = $productMetadata;
        $this->scopeConfig = $scopeConfig;
        $this->http = $http;
        $this->storeManager = $storeManager;
    }

    /**
     * Get Onboarding URL
     *
     * @param string $moduleKey
     * @param string $moduleVersion
     * @return string
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function getUrl($moduleKey, $moduleVersion)
    {
        $platform = 'magento';
        $platformVersion = $this->productMetaData->getVersion();

        $queryParameter = sprintf(
            '?plugin=%s&pluginVersion=%s&platform=%s&platformVersion=%sproducts=%s',
            $moduleKey,
            $moduleVersion,
            $platform,
            $platformVersion,
            $moduleKey
        );

        $websiteId = $this->http->getParam('website', 0);
        $website = $this->storeManager->getWebsite($websiteId);

        $scope = $this->getScope($website);
        $country = $this->scopeConfig->getValue('general/store_information/country_id', $scope, $website);

        if (!empty($country)) {
            $queryParameter .= '&country=' . $country;
        }

        $url = 'https://eu.portal.klarna.com/signup' . $queryParameter;
        if ($country === 'US') {
            $url = 'https://us.portal.klarna.com/signup' . $queryParameter;
        }

        return $url;
    }

    /**
     * Return either website scope or default scope depending on value of $website
     *
     * @param \Magento\Store\Api\Data\WebsiteInterface $website
     * @return string
     */
    private function getScope($website = null)
    {
        if ($website === null) {
            return ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
        }

        return ScopeInterface::SCOPE_WEBSITES;
    }
}