Skip to content
Snippets Groups Projects
WebsiteCode.php 1.99 KiB
Newer Older
Vadim Justus's avatar
Vadim Justus committed
<?php
/**
 * Copyright (c) 2020 TechDivision GmbH
 * All rights reserved
 *
 * This product includes proprietary software developed at TechDivision GmbH, Germany
 * For more information see http://www.techdivision.com/
 *
 * To obtain a valid license for using this software please contact us at
 * license@techdivision.com
 */
declare(strict_types=1);

namespace TechDivision\CsvSourceTransformationExample\Model\Mapping;

use Magento\Framework\Exception\LocalizedException;
use Magento\Store\Model\StoreManagerInterface;
use TechDivision\CsvSourceTransformationExample\Api\MappingInterface;

/**
 * @copyright  Copyright (c) 2020 TechDivision GmbH (http://www.techdivision.com)
 * @link       http://www.techdivision.com/
 * @author     Vadim Justus <v.justus@techdivision.com>
 */
class WebsiteCode implements MappingInterface
{
    /**
     * @var StoreManagerInterface
     */
    private $storeManager;

    /**
     * @var string
     */
    private $defaultWebsiteCode;

    /**
     * @param StoreManagerInterface $storeManager
     */
    public function __construct(StoreManagerInterface $storeManager)
    {
        $this->storeManager = $storeManager;
    }

    /**
     * @inheritDoc
     */
    public function header(array $row): array
    {
        return $row;
    }

    /**
     * @return string
     * @throws LocalizedException
     */
    private function getDefaultWebsiteCode(): string
    {
        if ($this->defaultWebsiteCode === null) {
            $defaultWebsiteId = $this->storeManager->getDefaultStoreView()->getWebsiteId();
            $this->defaultWebsiteCode = (string)$this->storeManager->getWebsite($defaultWebsiteId)->getCode();
        }
        return $this->defaultWebsiteCode;
    }

    /**
     * @inheritDoc
     * @throws LocalizedException
     */
    public function row(array $row): array
    {
        $value = $row['product_websites'];
        if ($value === '_DEFAULT_') {
            $row['product_websites'] = $this->getDefaultWebsiteCode();
        }
        return $row;
    }
}