phpvms/app/Services/ExportService.php

154 lines
3.5 KiB
PHP
Raw Normal View History

2018-03-22 06:07:30 +08:00
<?php
namespace App\Services;
use App\Interfaces\ImportExport;
use App\Interfaces\Service;
2018-03-23 01:55:56 +08:00
use App\Services\ImportExport\AircraftExporter;
2018-03-23 02:04:13 +08:00
use App\Services\ImportExport\AirportExporter;
2018-03-23 06:17:37 +08:00
use App\Services\ImportExport\ExpenseExporter;
2018-03-23 06:48:57 +08:00
use App\Services\ImportExport\FareExporter;
use App\Services\ImportExport\FlightExporter;
2018-03-23 06:59:10 +08:00
use App\Services\ImportExport\SubfleetExporter;
2018-03-22 06:07:30 +08:00
use Illuminate\Support\Collection;
2018-08-27 00:40:04 +08:00
use Illuminate\Support\Facades\Storage;
2018-03-22 06:07:30 +08:00
use League\Csv\CharsetConverter;
use League\Csv\Writer;
2018-03-23 01:48:27 +08:00
use Log;
2018-03-22 06:07:30 +08:00
/**
* Class ExportService
2018-03-22 06:07:30 +08:00
*/
class ExportService extends Service
2018-03-22 06:07:30 +08:00
{
/**
2018-08-27 00:40:04 +08:00
* @param string $path
*
2018-03-22 06:07:30 +08:00
* @return Writer
*/
public function openCsv($path): Writer
2018-03-22 06:07:30 +08:00
{
$writer = Writer::createFromPath($path, 'w+');
2018-03-22 06:07:30 +08:00
CharsetConverter::addTo($writer, 'utf-8', 'iso-8859-15');
2019-05-13 06:37:22 +08:00
2018-03-22 06:07:30 +08:00
return $writer;
}
/**
* Run the actual importer
2018-08-27 00:40:04 +08:00
*
2018-03-22 06:07:30 +08:00
* @param Collection $collection
* @param ImportExport $exporter
2018-08-27 00:40:04 +08:00
*
2018-03-22 06:07:30 +08:00
* @throws \League\Csv\CannotInsertRecord
2018-08-27 00:40:04 +08:00
*
2019-05-13 06:49:58 +08:00
* @return string
2018-03-22 06:07:30 +08:00
*/
protected function runExport(Collection $collection, ImportExport $exporter): string
2018-03-22 06:07:30 +08:00
{
2018-08-27 00:40:04 +08:00
$filename = 'export_'.$exporter->assetType.'.csv';
2018-03-23 01:48:27 +08:00
// Create the directory - makes it inside of storage/app
Storage::makeDirectory('import');
$path = storage_path('/app/import/export_'.$filename.'.csv');
2018-08-27 00:40:04 +08:00
Log::info('Exporting "'.$exporter->assetType.'" to '.$path);
2018-03-23 02:04:13 +08:00
$writer = $this->openCsv($path);
// Write out the header first
2018-03-31 09:57:30 +08:00
$writer->insertOne($exporter->getColumns());
// Write the rest of the rows
2018-03-22 06:07:30 +08:00
foreach ($collection as $row) {
$writer->insertOne($exporter->export($row));
}
return $path;
2018-03-22 06:07:30 +08:00
}
2018-03-23 01:55:56 +08:00
/**
* Export all of the aircraft
2018-08-27 00:40:04 +08:00
*
2018-03-23 01:55:56 +08:00
* @param Collection $aircraft
2018-08-27 00:40:04 +08:00
*
2018-03-23 01:55:56 +08:00
* @throws \League\Csv\CannotInsertRecord
2018-08-27 00:40:04 +08:00
*
2019-05-13 06:49:58 +08:00
* @return mixed
2018-03-23 01:55:56 +08:00
*/
public function exportAircraft($aircraft)
{
2019-05-13 06:37:22 +08:00
return $this->runExport($aircraft, new AircraftExporter());
2018-03-23 01:55:56 +08:00
}
2018-03-23 02:04:13 +08:00
/**
* Export all of the airports
2018-08-27 00:40:04 +08:00
*
2018-03-23 02:04:13 +08:00
* @param Collection $airports
2018-08-27 00:40:04 +08:00
*
2018-03-23 02:04:13 +08:00
* @throws \League\Csv\CannotInsertRecord
2018-08-27 00:40:04 +08:00
*
2019-05-13 06:49:58 +08:00
* @return mixed
2018-03-23 02:04:13 +08:00
*/
public function exportAirports($airports)
{
2019-05-13 06:37:22 +08:00
return $this->runExport($airports, new AirportExporter());
2018-03-23 02:04:13 +08:00
}
2018-03-23 06:17:37 +08:00
/**
* Export all of the airports
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:17:37 +08:00
* @param Collection $expenses
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:17:37 +08:00
* @throws \League\Csv\CannotInsertRecord
2018-08-27 00:40:04 +08:00
*
2019-05-13 06:49:58 +08:00
* @return mixed
2018-03-23 06:17:37 +08:00
*/
public function exportExpenses($expenses)
{
2019-05-13 06:37:22 +08:00
return $this->runExport($expenses, new ExpenseExporter());
2018-03-23 06:17:37 +08:00
}
2018-03-23 06:48:57 +08:00
/**
* Export all of the fares
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:48:57 +08:00
* @param Collection $fares
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:48:57 +08:00
* @throws \League\Csv\CannotInsertRecord
2018-08-27 00:40:04 +08:00
*
2019-05-13 06:49:58 +08:00
* @return mixed
2018-03-23 06:48:57 +08:00
*/
public function exportFares($fares)
{
2019-05-13 06:37:22 +08:00
return $this->runExport($fares, new FareExporter());
2018-03-23 06:48:57 +08:00
}
2018-03-22 06:07:30 +08:00
/**
* Export all of the flights
2018-08-27 00:40:04 +08:00
*
2018-03-23 01:55:56 +08:00
* @param Collection $flights
2018-08-27 00:40:04 +08:00
*
2018-03-23 01:55:56 +08:00
* @throws \League\Csv\CannotInsertRecord
2018-08-27 00:40:04 +08:00
*
2019-05-13 06:49:58 +08:00
* @return mixed
2018-03-22 06:07:30 +08:00
*/
public function exportFlights($flights)
2018-03-22 06:07:30 +08:00
{
2019-05-13 06:37:22 +08:00
return $this->runExport($flights, new FlightExporter());
2018-03-22 06:07:30 +08:00
}
2018-03-23 06:59:10 +08:00
/**
* Export all of the flights
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:59:10 +08:00
* @param Collection $subfleets
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:59:10 +08:00
* @throws \League\Csv\CannotInsertRecord
2018-08-27 00:40:04 +08:00
*
2019-05-13 06:49:58 +08:00
* @return mixed
2018-03-23 06:59:10 +08:00
*/
public function exportSubfleets($subfleets)
{
2019-05-13 06:37:22 +08:00
return $this->runExport($subfleets, new SubfleetExporter());
2018-03-23 06:59:10 +08:00
}
2018-03-22 06:07:30 +08:00
}