Your IP : 127.0.0.1


Current Path : /home/dev2.destoffenstraat.com/app/Firebear/ImportExport/Model/Export/Adapter/
Upload File :
Current File : /home/dev2.destoffenstraat.com/app/Firebear/ImportExport/Model/Export/Adapter/Ods.php

<?php
/**
 * @copyright: Copyright © 2021 Firebear Studio. All rights reserved.
 * @author: Firebear Studio <fbeardev@gmail.com>
 */
namespace Firebear\ImportExport\Model\Export\Adapter;

use OpenSpout\Common\Exception\IOException;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\ValidatorException;
use Magento\Framework\Filesystem;
use OpenSpout\Common\Exception\UnsupportedTypeException;
use OpenSpout\Writer\Exception\WriterNotOpenedException;
use Psr\Log\LoggerInterface;
use OpenSpout\Common\Entity\Row;
use OpenSpout\Writer\WriterInterface;
use OpenSpout\Writer\Common\Creator\WriterFactory;

/**
 * Ods Export Adapter
 */
class Ods extends AbstractAdapter
{
    /**
     * Spreadsheet Writer
     *
     * @var WriterInterface
     */
    private $writer;

    /**
     * Ods constructor
     *
     * @param Filesystem $filesystem
     * @param LoggerInterface $logger
     * @param null $destination
     * @param string $destinationDirectoryCode
     * @param array $data
     * @throws LocalizedException
     */
    public function __construct(
        Filesystem $filesystem,
        LoggerInterface $logger,
        $destination = null,
        $destinationDirectoryCode = DirectoryList::VAR_DIR,
        array $data = []
    ) {
        if (empty($data['export_source']['file_path']) && empty($data['export_source']['request_url'])) {
            throw new LocalizedException(__('Export File Path is Empty.'));
        }

        parent::__construct(
            $filesystem,
            $logger,
            $destination,
            $destinationDirectoryCode,
            $data
        );
    }

    /**
     * Write row data to source file
     *
     * @param array $rowData
     * @return AbstractAdapter
     * @throws IOException
     * @throws LocalizedException
     * @throws WriterNotOpenedException
     */
    public function writeRow(array $rowData)
    {
        if (null === $this->_headerCols) {
            $this->setHeaderCols(array_keys($rowData));
        }

        $this->addRow(
            array_merge(
                $this->_headerCols,
                array_intersect_key($rowData, $this->_headerCols)
            )
        );
        return $this;
    }

    /**
     * Prepare Row Data
     *
     * @param array $rowData
     * @return Row $rowData
     * @throws LocalizedException
     */
    private function prepareRow(array $rowData)
    {
        $rowData = array_map(function ($value) {
            return (string)$value;
        }, $rowData);

        return $this->getSpoutRow($rowData);
    }

    /**
     * Add Row Data
     *
     * @param array $rowData
     * @return void
     * @throws IOException
     * @throws WriterNotOpenedException|LocalizedException
     */
    private function addRow(array $rowData)
    {
        $this->writer->addRow(
            $this->prepareRow($rowData)
        );
    }

    /**
     * Set column names
     *
     * @param array $headerColumns
     * @return AbstractAdapter
     * @throws IOException
     * @throws LocalizedException
     * @throws WriterNotOpenedException
     */
    public function setHeaderCols(array $headerColumns): AbstractAdapter
    {
        if (null !== $this->_headerCols) {
            throw new LocalizedException(__('The header column names are already set.'));
        }
        if ($headerColumns) {
            foreach ($headerColumns as $columnName) {
                $this->_headerCols[$columnName] = false;
            }
            $this->writer->addRow($this->getSpoutRow(array_keys($this->_headerCols)));
        }
        return $this;
    }

    /**
     * Get contents of export file
     *
     * @return string
     */
    public function getContents()
    {
        $this->writer->close();
        return parent::getContents();
    }

    /**
     * MIME-type for 'Content-Type' header
     *
     * @return string
     */
    public function getContentType()
    {
        return 'application/vnd.oasis.opendocument.spreadsheet';
    }

    /**
     * Return file extension for downloading
     *
     * @return string
     */
    public function getFileExtension()
    {
        return 'ods';
    }

    /**
     * Method called as last step of object instance creation
     *
     * @return AbstractAdapter
     * @throws IOException
     * @throws ValidatorException|UnsupportedTypeException
     */
    protected function _init()
    {
        if (empty($this->writer)) {
            $this->writer = WriterFactory::createFromFile('.' . $this->getFileExtension());
        }
        $file = $this->_directoryHandle->getAbsolutePath(
            $this->_destination
        );
        $this->writer->openToFile($file);
        return $this;
    }
}