2018-03-22 06:07:30 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Interfaces\ImportExport;
|
|
|
|
use App\Interfaces\Service;
|
|
|
|
use App\Repositories\FlightRepository;
|
2018-03-23 01:43:58 +08:00
|
|
|
use App\Services\ImportExport\FlightExporter;
|
2018-03-22 06:07:30 +08:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use League\Csv\CharsetConverter;
|
|
|
|
use League\Csv\Writer;
|
2018-03-23 01:43:58 +08:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
2018-03-22 06:07:30 +08:00
|
|
|
|
|
|
|
/**
|
2018-03-22 08:12:36 +08:00
|
|
|
* Class ExportService
|
2018-03-22 06:07:30 +08:00
|
|
|
* @package App\Services
|
|
|
|
*/
|
2018-03-22 08:12:36 +08:00
|
|
|
class ExportService extends Service
|
2018-03-22 06:07:30 +08:00
|
|
|
{
|
|
|
|
protected $flightRepo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ImporterService constructor.
|
|
|
|
* @param FlightRepository $flightRepo
|
|
|
|
*/
|
|
|
|
public function __construct(FlightRepository $flightRepo) {
|
|
|
|
$this->flightRepo = $flightRepo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-23 01:43:58 +08:00
|
|
|
* @param string $path
|
2018-03-22 06:07:30 +08:00
|
|
|
* @return Writer
|
|
|
|
*/
|
2018-03-23 01:43:58 +08:00
|
|
|
public function openCsv($path): Writer
|
2018-03-22 06:07:30 +08:00
|
|
|
{
|
2018-03-23 01:43:58 +08:00
|
|
|
$writer = Writer::createFromPath($path, 'w+');
|
2018-03-22 06:07:30 +08:00
|
|
|
CharsetConverter::addTo($writer, 'utf-8', 'iso-8859-15');
|
|
|
|
return $writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the actual importer
|
|
|
|
* @param Collection $collection
|
|
|
|
* @param ImportExport $exporter
|
2018-03-23 01:43:58 +08:00
|
|
|
* @return string
|
2018-03-22 06:07:30 +08:00
|
|
|
* @throws \League\Csv\CannotInsertRecord
|
|
|
|
*/
|
2018-03-23 01:43:58 +08:00
|
|
|
protected function runExport(Collection $collection, ImportExport $exporter): string
|
2018-03-22 06:07:30 +08:00
|
|
|
{
|
2018-03-23 01:43:58 +08:00
|
|
|
$filename = 'export_' . $exporter->assetType . '.csv';
|
|
|
|
Storage::makeDirectory(storage_path('app/import'));
|
|
|
|
$path = storage_path('app/import/'.$filename.'.csv');
|
|
|
|
|
|
|
|
$writer = $this->openCsv($path);
|
|
|
|
|
|
|
|
// Write out the header first
|
2018-03-22 06:07:30 +08:00
|
|
|
$writer->insertOne($exporter->getColumns());
|
2018-03-23 01:43:58 +08:00
|
|
|
|
|
|
|
// Write the rest of the rows
|
2018-03-22 06:07:30 +08:00
|
|
|
foreach ($collection as $row) {
|
|
|
|
$writer->insertOne($exporter->export($row));
|
|
|
|
}
|
|
|
|
|
2018-03-23 01:43:58 +08:00
|
|
|
return $path;
|
2018-03-22 06:07:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export all of the flights
|
|
|
|
* @param Collection $flights
|
|
|
|
* @param string $csv_file
|
|
|
|
* @return mixed
|
|
|
|
* @throws \League\Csv\Exception
|
|
|
|
*/
|
2018-03-23 01:43:58 +08:00
|
|
|
public function exportFlights($flights)
|
2018-03-22 06:07:30 +08:00
|
|
|
{
|
|
|
|
$exporter = new FlightExporter();
|
2018-03-23 01:43:58 +08:00
|
|
|
return $this->runExport($flights, $exporter);
|
2018-03-22 06:07:30 +08:00
|
|
|
}
|
|
|
|
}
|