HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/structura.komma.pro/app/KommaApp/Kms/Transfer/TransferService.php
<?php


namespace App\KommaApp\Kms\Transfer;


use App\KommaApp\Kms\Core\Sections\SectionService;
use App\KommaApp\Shop\Products\Product\Transfer\ProductCsvExportService;
use App\KommaApp\Shop\Products\Product\Transfer\ProductCsvImportService;
use App\KommaApp\Shop\Products\Product\Transfer\ProductDocumentImporter;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\MessageBag;
use Symfony\Component\HttpFoundation\StreamedResponse;

/**
 * Class ImportExportService
 *
 * Directs Import and Export services to do their job.
 * And is responsible for loading and offering files for download
 *
 * @package App\KommaApp\Kms\Transfer
 */
class TransferService extends SectionService
{
    protected $sortable = true;

    /** @var $exportService AbstractCsvExportService */
    protected $exportService;

    /** @var $importService AbstractCsvImportService */
    protected $importService;

    /** @var ProductDocumentImporter $documentService */
    protected $documentService;

    /**
     * @var string The name of the file input field
     */
    public static $fileInputFieldName = 'File-file';

    /**
     * TransferService constructor.
     *
     * @param ProductCsvImportService $importService
     * @param ProductCsvExportService $exportService
     * @param ProductDocumentImporter $productDocumentImporter
     */
    public function __construct(ProductCsvImportService $importService, ProductCsvExportService $exportService, ProductDocumentImporter $productDocumentImporter)
    {
        if(self::$fileInputFieldName == '')
            throw new \RuntimeException('Please set the static $inputFieldName variable to the input field name the transfer service should import files from');

        $this->forModelName = null;
        $this->exportService = $exportService;
        $this->importService = $importService;
        $this->documentService = $productDocumentImporter;
    }

    /**
     * Imports a file and returns a message if the import was done.
     * Returns null if no file was present
     *
     * @param Request $request
     * @return MessageBag|null
     */
    public function import(Request $request):? MessageBag
    {
        return $this->importIfWasUploaded($request);
    }

    /**
     * Imports a file if one is uploaded or exports one if not
     *
     * @param Request $request
     * @return MessageBag|StreamedResponse
     */
    public function importExport(Request $request)
    {
        $messages = $this->importIfWasUploaded($request);
        if(!$messages) return $this->export();
        return $messages;
    }

    public function export():StreamedResponse
    {
        $path = 'importexport/importexport.csv';
        Storage::delete($path); //Delete old file if it exists.
        $csvData = $this->exportService->exportToCsvFileString();
        Storage::put($path, $csvData);
        return Storage::download($path);
    }

    /**
     * Imports from a csv file.
     * Returns
     *
     * @param Request $request
     * @return MessageBag|null depending on whether something was imported or not
     */
    public function importIfWasUploaded(Request $request):? MessageBag
    {
        if($request->hasFile(self::$fileInputFieldName)) {
            $file = $request->file(self::$fileInputFieldName)->store('importexport');
            $path = storage_path('app'.DIRECTORY_SEPARATOR.$file);
            $messageBag = $this->importService->importFromFile($path);

            unlink($path);

            return $messageBag;
        }
        return null;
    }

    /**
     * Imports documents via a document importer (files, images etc) and links them to models
     *
     * @return MessageBag;
     */
    public function importDocuments():? MessageBag
    {
        return $this->documentService->importDocuments();
    }
}